import type { LunaticControl, LunaticError, LunaticReducerState, } from '../type'; import { fillComponentExpressions } from './fill-components/fill-component-expressions'; import { checkRoundaboutControl } from '../reducer/controls/check-roundabout-control'; import { checkBaseControl } from '../reducer/controls/check-base-control'; import { getComponentsFromState } from './get-components-from-state'; import { InterpretedLoopComponent, InterpretedRoundaboutComponent, isLoopComponent, isQuestionComponent, isRoundaboutComponent, type ComponentDefinition, type InterpretedComponent, } from './component'; export type StateForControls = Pick< LunaticReducerState, 'pager' | 'pages' | 'isInLoop' | 'executeExpression' >; /** * Check if components of the current page have errors, and return a map of * errors (indexed by component ID). */ function checkComponents( state: StateForControls, components: (ComponentDefinition | InterpretedComponent)[], currentErrors?: Record ): Record { let errors = currentErrors ?? ({} as Record); for (const component of components) { // The component has global level controls if ('controls' in component && Array.isArray(component.controls)) { const componentErrors = checkControls( component.controls.filter((c) => c.type !== 'ROW'), state.executeExpression, state.pager ); if (componentErrors.length > 0) { errors[component.id] = componentErrors; } } // For Loop and RosterForLoop, inspect iterations for row controls if (isRoundaboutComponent(component)) errors = checkRoundabout(state, component, errors); // For Loop and RosterForLoop, inspect children if (isLoopComponent(component)) errors = checkLoop(state, component, errors); // For Question, loop over children if (isQuestionComponent(component)) errors = checkComponents(state, component.components, errors); } return errors; } function checkRoundabout( state: StateForControls, component: InterpretedRoundaboutComponent, errors: Record ) { const rowControls = component.controls?.filter((c) => c.type === 'ROW'); if (rowControls?.length) { errors = checkComponentInLoop( state, { ...component, controls: rowControls }, errors ); } return errors; } function checkLoop( state: StateForControls, component: InterpretedLoopComponent, errors: Record ) { const rowControls = component.controls?.filter((c) => c.type === 'ROW'); if (rowControls?.length) { errors = checkComponentInLoop( state, { ...component, controls: rowControls }, errors ); } for (const child of component.components) { errors = checkComponentInLoop(state, child, errors); } return errors; } function checkControls( controls: LunaticControl[], executeExpression: LunaticReducerState['executeExpression'], pager: LunaticReducerState['pager'] ): LunaticError[] { return controls .map((control) => { if (control.type === 'roundabout') { return checkRoundaboutControl(control, executeExpression); } return checkBaseControl(control, executeExpression, pager); }) .filter((error) => error !== undefined); } /** * Figure out the number of iterations of a component. */ export function computeIterations( component: InterpretedComponent | ComponentDefinition, executeExpression: LunaticReducerState['executeExpression'] ): number { // iterations is present and it's already computed, return the actual value (case of InterpretedComponent) if ('iterations' in component && typeof component.iterations === 'number') return component.iterations; // iteratons is present and it's an expression, compute it if ( 'iterations' in component && component.iterations && typeof component.iterations === 'object' && 'value' in component.iterations ) { return executeExpression(component.iterations); } // Fallback to variable collectes by this component, return it's length if ('response' in component) { const value = executeExpression({ type: 'VTL', value: component.response.name, }); if (Array.isArray(value)) { return value.length; } } // Look for the component with the biggest size if ('components' in component) { return Math.max( ...component.components.map((c) => computeIterations(c, executeExpression) ) ); } return 0; } /** * Check controls on a component for each iteration * Errors are returned using a map of id suffixed with the iteration index (ex: {prenom-1: [], prenom-3: []}) */ function checkComponentInLoop( state: StateForControls, component: | ComponentDefinition | InterpretedLoopComponent | InterpretedRoundaboutComponent, errors: Record ): Record { // For Question, loop over children if (component.componentType === 'Question') { for (const child of component.components) { errors = checkComponentInLoop(state, child, errors); } return errors; } // The component has no controls, skip it if ('controls' in component && !Array.isArray(component.controls)) { return errors; } // Execute control for each iteration const iterations = computeIterations(component, state.executeExpression); for (let i = 0; i < iterations; i++) { // Create a pager representing the iteration we want to check const iterationPager = { ...state.pager, iteration: i, nbIterations: iterations, }; // There is no controls on this component if (!('controls' in component) || !component.controls) { continue; } // For Roundabout, we don't check controls for iterations that are disabled if (isRoundaboutComponent(component) && component.item.disabled) { const isIterationDisabled = state.executeExpression( { value: component.item.disabled?.value, type: 'VTL' }, iterationPager ); if (isIterationDisabled) { continue; } } // The component is filtered on this iteration, skip it if ( // conditionFilter can be the interpreted expression, or the object representing the expression ('conditionFilter' in component && component.conditionFilter && typeof component.conditionFilter == 'object' && 'value' in component.conditionFilter && !state.executeExpression( { value: component.conditionFilter.value, type: 'VTL' }, iterationPager )) || // @ts-expect-error TS doesn't understand that conditionFilter is a boolean here component.conditionFilter === false ) { continue; } const componentErrors = checkControls( component.controls, state.executeExpression, iterationPager ); if (componentErrors.length > 0) { errors[`${component.id}-${i}`] = componentErrors; } } return errors; } /** * Check if there is a critical error (type: "Error" and criticality: "Format") */ function hasCriticalError(errors?: Record): boolean { if (!errors) { return false; } // Look for at least one critical error in the list const criticalError = Object.values(errors) .flat() .find( (error) => error.criticality.startsWith('ERROR') || error.typeOfControl === 'FORMAT' ); return criticalError !== undefined; } /** * Check controls for currently visible components and output errors. */ export function compileControls(state: StateForControls) { const components = getComponentsFromState(state); const componentFiltered = components .map((component) => fillComponentExpressions(component, state)) .filter((component) => { if ('conditionFilter' in component) { return component.conditionFilter ?? true; } return true; }); const errors = checkComponents(state, componentFiltered); const currentErrors = Object.keys(errors).length > 0 ? errors : undefined; return { currentErrors, isCritical: hasCriticalError(currentErrors), }; }