All files / src/components/script/events customEventArgs.ts

100% Statements 20/20
93.33% Branches 14/15
100% Functions 2/2
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  1x         1x           4x   4x 8x 8x 1x     7x   7x     5x         7x         7x 1x   6x           4x 4x 4x 4x       4x    
import type { ScriptNormalized } from "shared/lib/entities/entitiesTypes";
import {
  defaultVariableValueForType,
  type VariableFieldCandidate,
} from "components/script/fields/fieldHelpers";
 
export const applyCustomEventArgDefaults = (
  customEvent: ScriptNormalized,
  args: Record<string, unknown>,
  variableCandidates: VariableFieldCandidate[],
  preferredVariableId?: string,
): Record<string, unknown> => {
  const nextArgs = { ...args };
 
  for (const variable of Object.values(customEvent.variables)) {
    const key = `$variable[${variable.id}]$`;
    if (nextArgs[key] !== undefined) {
      continue;
    }
    const variableType =
      variable.passByReference === "array" ? "arrayReference" : "any";
    const compatibleVariableCandidates =
      variable.passByReference === "array"
        ? variableCandidates.filter(
            (candidate) =>
              candidate.type === "array" &&
              candidate.length !== undefined &&
              candidate.length >= variable.length,
          )
        : variableCandidates;
    const defaultValue = defaultVariableValueForType(
      variableType,
      compatibleVariableCandidates,
      preferredVariableId,
    );
    if (defaultValue === undefined) {
      continue;
    }
    nextArgs[key] =
      typeof defaultValue === "string"
        ? { type: "variable", value: defaultValue }
        : defaultValue;
  }
 
  for (const actor of Object.values(customEvent.actors)) {
    const key = `$actor[${actor.id}]$`;
    Eif (nextArgs[key] === undefined) {
      nextArgs[key] = "player";
    }
  }
 
  return nextArgs;
};