import type { ReactNode } from 'react'; import type { LunaticComponentDefinition, LunaticExpression, LunaticReducerState, } from '../../type'; import { isObject } from '../../../utils/object'; import { castBool, castNumber, castString } from '../../../utils/cast'; const VTL_ATTRIBUTES = [ ['label', null], ['unit', null], ['options.label', null], ['declarations.label', null], ['description', null], ['iterations', castNumber], ['responses.label', null], ['responses.description', null], ['responses.detail.label', null], ['options.description', null], ['options.detail.label', null], ['controls.iterations', castNumber], ['items.label', null], ['items.body', null], ['lines.min', castNumber], ['lines.max', castNumber], ['xAxisIterations', castNumber], ['yAxisIterations', castNumber], ['conditionFilter', castBool], ['header.label', null], ['disabled', castBool], ['conditionReadOnly', castBool], // For suggesters ['arbitrary.label', castString], ['arbitrary.inputLabel', castString], ] as const; // List of property that can be simple string instead of expressions const allowString = new Set(['unit']); // Utility type to replace all expression from an object into a translated version type UntranslatedProperties = | 'expressions' | 'sections' | 'body' | 'item' | 'controls' | 'conditionFilter' | 'conditionReadOnly' | 'components' | 'optionFilter'; export type DeepTranslateExpression = T extends LunaticExpression ? ReactNode : T extends (infer ElementType)[] ? DeepTranslateExpression[] : T extends { [k: string | number]: unknown } ? { [key in keyof T]: key extends UntranslatedProperties ? T[key] : DeepTranslateExpression; } : T; /** * Interpret every VTL expression inside a component * * ## Example * * For instance a component like this : * * ```json * { * "label": {"value": "\"Hello world\"", "type": "VTL"} * } * ``` * * Would see its expression interpreted like this * * ```json * { * "label": "Hello world" * } * ``` */ export function fillComponentExpressions( component: LunaticComponentDefinition, state: { executeExpression: LunaticReducerState['executeExpression']; pager: Pick; } ): DeepTranslateExpression { let filledComponent: any = component; // This is too dynamic to be typed correctly, allow any here for (const attribute of VTL_ATTRIBUTES) { const propertyPath = attribute[0].split('.'); const caster = attribute[1]; // Interpret an expression const convert = (expression: unknown) => { // For backward compatibility, some properties can be both a string and an expression if (typeof expression === 'string' && allowString.has(propertyPath[0])) { return expression; } if (!isValidExpression(expression)) { throw new Error( `Expression expected at "${attribute[0]}", got ${expression}` ); } const result = state.executeExpression(expression, { iteration: state.pager.linksIterations ?? state.pager.iteration, }); try { return caster ? caster(result) : result; } catch (e) { throw new Error(`Cannot fill property ${propertyPath.join('.')}, ${e}`); } }; filledComponent = interpretPropertyAtPath( filledComponent, propertyPath, convert ); } return filledComponent; } function isValidExpression( expression: unknown ): expression is LunaticExpression { return Boolean( expression && typeof expression === 'object' && 'type' in expression && 'value' in expression ); } /** * Interpret a property at the specified path * - if the value is an array, keep explorer for each item * - if the property does not exist, the object is not modified */ function interpretPropertyAtPath( obj: unknown, path: string[], interpreter: (s: unknown) => unknown ): unknown { // For arrays, we need to keep exploring for each item if (Array.isArray(obj)) { return obj.map((item) => interpretPropertyAtPath(item, path, interpreter)); } // We reached the property we want to translate if (path.length === 0) { return interpreter(obj); } const [property, ...rest] = path; if (!isObject(obj) || !(property in obj)) { return obj; } return { ...obj, [property]: interpretPropertyAtPath(obj[property], rest, interpreter), }; }