"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalAdaptableDateEditor = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const date_fns_1 = require("date-fns");
const useProperty_1 = tslib_1.__importDefault(require("../../../components/utils/useProperty"));
const AdaptableInput_1 = tslib_1.__importDefault(require("../../../View/Components/AdaptableInput"));
const DatepickerContext_1 = require("../../../components/Datepicker/DatepickerContext");
const DateHelper_1 = require("../../../Utilities/Helpers/DateHelper");
const fillStyle = {
    position: 'absolute',
    left: 0,
    top: 0,
    right: 0,
    bottom: 0,
};
const hostStyle = {
    ...fillStyle,
    boxSizing: 'border-box',
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
};
const inputStyle = {
    position: 'relative',
    background: 'transparent',
    boxSizing: 'border-box',
    minWidth: 0,
    outline: 'none',
    width: '100%',
    border: 'none',
};
exports.InternalAdaptableDateEditor = React.forwardRef((props, ref) => {
    const inputRef = React.useRef(null);
    const focus = () => {
        inputRef.current?.focus();
    };
    const [value, setValue] = (0, useProperty_1.default)(props, 'value', props.defaultValue, {
        onChange: (value) => {
            if (value === '' || value == undefined) {
                props.onValueChange?.(null);
            }
            else {
                const date = typeof value === 'string' ? (0, date_fns_1.parseISO)(value) : new Date(value);
                props.onValueChange?.(date);
            }
        },
    });
    React.useImperativeHandle(ref, () => {
        return {
            focus,
        };
    });
    const stringValue = (0, DateHelper_1.parseToISO)(value, props.dateFormat);
    const onChange = React.useCallback((event) => {
        setValue(event.target.value);
    }, []);
    return (React.createElement("div", { style: hostStyle },
        React.createElement(DatepickerContext_1.DatepickerContext.Provider, { value: {
                onHide: (keyboardEventKey) => {
                    props.onStopEdit?.(keyboardEventKey);
                },
                onShow: () => { },
            } },
            React.createElement(AdaptableInput_1.default, { type: 'date', value: stringValue, onChange: onChange, style: inputStyle, ref: inputRef }))));
});
