"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 } = props, inputProps = tslib_1.__rest(props, ["value", "defaultValue", "onChange", "required", "disabled"]);
    const [value, setValue] = (0, useProperty_1.default)(props, 'value', undefined, {
        onChange: (dateString) => {
            var _a;
            // wrap date value in FormEvent in order to keep the external API unchanged
            return (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, {
                target: {
                    // ALWAYS trigger onChange with the ISO format
                    value: dateString,
                },
            });
        },
    });
    const dateValue = value ? (0, DateHelper_1.parseDateValue)(value) : null;
    const datepickerProps = Object.assign({ 
        // this is OK as long as we do not support range datepicker
        value: dateValue, onChange: (dateValue) => { var _a; 
        // convert to ISO8601 string format
        // see https://developer.mozilla.org/en-US/docs/Web/HTML/Date_and_time_formats#date_strings
        return setValue((_a = (0, DateHelper_1.dateToISO)(dateValue)) !== null && _a !== void 0 ? _a : ''); }, required,
        disabled, dateProps: {
            format: dateInputOptions.dateFormat,
            locale: dateInputOptions.locale,
        }, showWeekNumber: dateInputOptions.showWeekNumber, showOutsideDays: dateInputOptions.showOutsideDays, datepickerButtons: dateInputOptions.datepickerButtons }, inputProps);
    return React.createElement(Datepicker_1.Datepicker, Object.assign({ ref: ref }, datepickerProps));
});
exports.default = AdaptableDateInput;
