import { type PropsWithChildren } from 'react';
import D from '../../i18n';
import { times } from '../../utils/array';
import { LunaticComponents } from '../LunaticComponents';
import type { LunaticComponentProps } from '../type';
import { blockedInLoopComponents } from './constant';
import { slottableComponent } from '../shared/HOC/slottableComponent';
import { Label } from '../shared/Label/Label';
import { Declarations } from '../shared/Declarations/Declarations';
import { Button } from '../shared/Button/Button';
import {
ComponentErrors,
getComponentErrors,
} from '../shared/ComponentErrors/ComponentErrors';
import type { LunaticError } from '../../use-lunatic/type';
import { useLoopUtils } from './utils';
/**
* Loop without specific markup (stack of subcomponents)
*/
export function Loop(props: LunaticComponentProps<'Loop'>) {
const { min, max, nbRows, addRow, removeRow } = useLoopUtils(props);
const { getComponents, errors } = props;
if (nbRows === 0) {
return null;
}
return (
{times(nbRows, (n) => (
({
...props,
...c,
iteration: n,
id: `${c.id}-${n}`,
errors,
})}
/>
))}
);
}
type CustomProps = Omit<
LunaticComponentProps<'RosterForLoop' | 'Loop'>,
| 'response'
| 'handleChanges'
| 'errors'
| 'lines'
| 'iterations'
| 'value'
| 'getComponents'
> &
PropsWithChildren<{
errors?: LunaticError[];
addRow?: () => void;
removeRow?: () => void;
canControlRows?: boolean;
disableDeleteRowButton?: boolean;
}>;
export const CustomLoop = slottableComponent('Loop', (props) => {
const {
declarations,
id,
label,
canControlRows,
children,
errors,
addRow,
removeRow,
disableDeleteRowButton = false,
} = props;
return (
<>
{children}
{canControlRows && (
<>
>
)}
>
);
});