"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateInput = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const input_1 = require("../input");
const t_1 = require("../t");
const utils_1 = require("../utils");
const DateInput = (_a) => {
    var { initialDate, onChange, onValidate, setValidateStatus } = _a, rest = __rest(_a, ["initialDate", "onChange", "onValidate", "setValidateStatus"]);
    const formattedInitialDate = initialDate ? (0, utils_1.formatDate)(initialDate, 'YYYY-MM-DD', { timeZone: 'UTC' }) : '';
    const [dateInput, setDateInput] = (0, react_1.useState)(formattedInitialDate);
    const [dateInputValidated, setDateInputValidated] = (0, react_1.useState)(true);
    const [errorMessage, setErrorMessage] = (0, react_1.useState)('');
    const isValidDate = (dateString) => {
        if (!(0, utils_1.validateDateFormat)(dateString))
            return false;
        if (!(0, utils_1.parseIsoDate)(dateString))
            return false;
        return true;
    };
    const handleOnBlur = () => {
        setDateInputValidated(!dateInput);
        const parsedDate = (0, utils_1.parseIsoDate)(dateInput);
        if (parsedDate) {
            setDateInput((0, utils_1.formatDate)(parsedDate, 'YYYY-MM-DD', { timeZone: 'UTC' }));
            const validateMessage = onValidate(parsedDate);
            !validateMessage && onChange(parsedDate);
            setDateInputValidated(!validateMessage);
            setErrorMessage(validateMessage);
        }
    };
    (0, react_1.useEffect)(() => {
        setDateInput(formattedInitialDate);
        const validation = onValidate(initialDate);
        setDateInputValidated(!validation);
        setValidateStatus(!validation);
        setErrorMessage(validation);
    }, [initialDate]);
    (0, react_1.useEffect)(() => {
        if (dateInput) {
            const isValidDateString = isValidDate(dateInput);
            setValidateStatus(isValidDateString);
            if (!(0, utils_1.validateDateFormat)(dateInput))
                setErrorMessage('Supported format is YYYY-MM-DD');
            else if (!(0, utils_1.parseIsoDate)(dateInput))
                setErrorMessage('Invalid date');
            else {
                const parsedDate = (0, utils_1.parseIsoDate)(dateInput);
                const validateMessage = onValidate(parsedDate);
                !validateMessage && onChange(parsedDate);
                setDateInputValidated(!validateMessage);
                setErrorMessage(validateMessage);
            }
        }
    }, [dateInput]);
    return ((0, jsx_runtime_1.jsx)(input_1.Input, Object.assign({ borderColor: dateInputValidated ? 'sandstone.main' : 'energyRed.main', className: "dateInput", "data-testid": "test-date-input", errorText: !dateInputValidated && (0, jsx_runtime_1.jsx)(t_1.T, { size: "md", children: errorMessage }), onBlur: handleOnBlur, onChange: e => setDateInput(e.target.value), placeholder: "YYYY-MM-DD", size: "md", state: !dateInputValidated && !errorMessage ? 'error' : 'default', value: dateInput }, rest)));
};
exports.DateInput = DateInput;
//# sourceMappingURL=dateInput.js.map