import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import InputMask from "@mona-health/react-input-mask";
import Text from "../../../../Text";
import { useOneUIConfig } from "../../../../../context/OneUIProvider";
import Input from "../../../../Input";
import { isValidated } from "../../../Form.hook";
/**
 * Allows inputing a date
 **/
export default function dateFieldFactory(dateFormat) {
    return function DateField({ onAnswer, question, value, error, ...input }) {
        const { titleVariant } = useOneUIConfig("component.form");
        return (_jsxs(_Fragment, { children: [_jsx(Text, { type: titleVariant, children: question.title }), _jsx(InputMask, { mask: dateFormat, value: value, onChange: ({ target: { value } }) => {
                        onAnswer("date", question.id, value);
                    }, children: _jsx(Input, { placeholder: input.placeholder ?? question.title, error: error, "data-testid": input["data-testid"] }) })] }));
    };
}
const parseDate = (formattedDate, format) => {
    const howManyParts = format.split("/");
    const providedDate = formattedDate;
    const dateParts = providedDate.split("/").map(Number).reverse();
    if (dateParts.length < howManyParts.length)
        return false;
    const [year, month, day = 1] = dateParts;
    if (month > 12)
        return false;
    if (day > 31)
        return false;
    const parsedDate = new Date(year, month - 1, day);
    return parsedDate;
};
export const dateFieldValidatorFactory = (invalidDateLabel, requiredFieldLabel, format) => (answer) => {
    const providedDate = answer || "";
    const parsedDate = parseDate(providedDate, format);
    if (!parsedDate)
        return {
            isValid: false,
            error: invalidDateLabel,
        };
    const dateValidation = isValidated(providedDate, false, undefined, requiredFieldLabel);
    return dateValidation;
};
//# sourceMappingURL=DateField.js.map