"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const Datepicker_1 = require("../../../components/Datepicker");
const AdaptableContext_1 = require("../../AdaptableContext");
const useProperty_1 = tslib_1.__importDefault(require("../../../components/utils/useProperty"));
const DateHelper_1 = require("../../../Utilities/Helpers/DateHelper");
const AdaptableDateInput = React.forwardRef((props, ref) => {
    const dateInputOptions = (0, AdaptableContext_1.useAdaptable)().adaptableOptions.userInterfaceOptions.dateInputOptions;
    const { value: _, defaultValue: __, onChange, required, disabled, showClearButton, ...inputProps } = props;
    const [value, setValue] = (0, useProperty_1.default)(props, 'value', undefined, {
        onChange: (dateString) => 
        // wrap date value in FormEvent in order to keep the external API unchanged
        props.onChange?.({
            target: {
                // ALWAYS trigger onChange with the ISO format
                value: dateString,
            },
        }),
    });
    const dateValue = value ? (0, DateHelper_1.parseDateValue)(value) : null;
    const datepickerProps = {
        // this is OK as long as we do not support range datepicker
        value: dateValue,
        onChange: (dateValue) => 
        // convert to ISO8601 string format
        // see https://developer.mozilla.org/en-US/docs/Web/HTML/Date_and_time_formats#date_strings
        setValue((0, DateHelper_1.dateToISO)(dateValue) ?? ''),
        required,
        disabled,
        dateProps: {
            format: dateInputOptions.dateFormat,
            locale: dateInputOptions.locale,
        },
        showWeekNumber: dateInputOptions.showWeekNumber,
        showOutsideDays: dateInputOptions.showOutsideDays,
        datepickerButtons: dateInputOptions.datepickerButtons,
        showClearButton,
        ...inputProps,
    };
    return React.createElement(Datepicker_1.Datepicker, { ref: ref, ...datepickerProps });
});
exports.default = AdaptableDateInput;
