import type {HintCode, HintModel} from '../server/ReactFlightServerConfigDOM';
import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
import {getCrossOriginString} from './crossOriginStrings';
export function dispatchHint<Code: HintCode>(
code: Code,
model: HintModel<Code>,
): void {
const dispatcher = ReactDOMSharedInternals.d;
switch (code) {
case 'D': {
const refined = refineModel(code, model);
const href = refined;
dispatcher.D( href);
return;
}
case 'C': {
const refined = refineModel(code, model);
if (typeof refined === 'string') {
const href = refined;
dispatcher.C( href);
} else {
const href = refined[0];
const crossOrigin = refined[1];
dispatcher.C( href, crossOrigin);
}
return;
}
case 'L': {
const refined = refineModel(code, model);
const href = refined[0];
const as = refined[1];
if (refined.length === 3) {
const options = refined[2];
dispatcher.L( href, as, options);
} else {
dispatcher.L( href, as);
}
return;
}
case 'm': {
const refined = refineModel(code, model);
if (typeof refined === 'string') {
const href = refined;
dispatcher.m( href);
} else {
const href = refined[0];
const options = refined[1];
dispatcher.m( href, options);
}
return;
}
case 'X': {
const refined = refineModel(code, model);
if (typeof refined === 'string') {
const href = refined;
dispatcher.X( href);
} else {
const href = refined[0];
const options = refined[1];
dispatcher.X( href, options);
}
return;
}
case 'S': {
const refined = refineModel(code, model);
if (typeof refined === 'string') {
const href = refined;
dispatcher.S( href);
} else {
const href = refined[0];
const precedence = refined[1] === 0 ? undefined : refined[1];
const options = refined.length === 3 ? refined[2] : undefined;
dispatcher.S( href, precedence, options);
}
return;
}
case 'M': {
const refined = refineModel(code, model);
if (typeof refined === 'string') {
const href = refined;
dispatcher.M( href);
} else {
const href = refined[0];
const options = refined[1];
dispatcher.M( href, options);
}
return;
}
}
}
function refineModel<T>(code: T, model: HintModel<any>): HintModel<T> {
return model;
}
export function preinitModuleForSSR(
href: string,
nonce: ?string,
crossOrigin: ?string,
) {
ReactDOMSharedInternals.d
.M( href, {
crossOrigin: getCrossOriginString(crossOrigin),
nonce,
});
}
export function preinitScriptForSSR(
href: string,
nonce: ?string,
crossOrigin: ?string,
) {
ReactDOMSharedInternals.d
.X( href, {
crossOrigin: getCrossOriginString(crossOrigin),
nonce,
});
}