(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../Calendar/Calendar", "../utils/date", "../hooks/useOnClickOutside", "./createAutoCorrectedDatePipe", "./interfaces", "./constants", "./styles"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var Calendar_1 = (0, tslib_1.__importDefault)(require("../Calendar/Calendar"));
    var date_1 = require("../utils/date");
    var useOnClickOutside_1 = require("../hooks/useOnClickOutside");
    var createAutoCorrectedDatePipe_1 = (0, tslib_1.__importDefault)(require("./createAutoCorrectedDatePipe"));
    var interfaces_1 = require("./interfaces");
    var constants_1 = require("./constants");
    var styles_1 = require("./styles");
    var InputDate = function (_a) {
        var id = _a.id, idCalendar = _a.idCalendar, _b = _a.locale, locale = _b === void 0 ? "ru" : _b, _c = _a.isRangeMode, isRangeMode = _c === void 0 ? false : _c, onChange = _a.onChange, placeholder = _a.placeholder, _d = _a.maxDate, maxDate = _d === void 0 ? "31.12.2050" : _d, _e = _a.minDate, minDate = _e === void 0 ? "01.01.1920" : _e, onKeyDown = _a.onKeyDown, _f = _a.isShowMask, isShowMaskProps = _f === void 0 ? false : _f, className = _a.className, _g = _a.value, value = _g === void 0 ? "" : _g, onCalendarClose = _a.onCalendarClose, onCalendarOpen = _a.onCalendarOpen, onDateSelected = _a.onDateSelected, _h = _a.placeholderChar, placeholderChar = _h === void 0 ? constants_1.PlaceholderChar : _h, onBlur = _a.onBlur, onFocus = _a.onFocus, isDisabled = _a.isDisabled, inputError = _a.error, _j = _a.testId, testId = _j === void 0 ? "inputDate" : _j, width = _a.width, status = _a.status, size = _a.size, statusText = _a.statusText, isRequired = _a.isRequired, isShowClearIcon = _a.isShowClearIcon, iconLeft = _a.iconLeft, iconRight = _a.iconRight, isReadOnly = _a.isReadOnly, viewType = _a.viewType, onClear = _a.onClear;
        var datePickerRef = (0, react_1.useRef)(null);
        var _k = (0, tslib_1.__read)((0, react_1.useState)(false), 2), isCalendarShown = _k[0], setCalendarShown = _k[1];
        var _l = (0, tslib_1.__read)((0, react_1.useState)(value), 2), currentDate = _l[0], setCurrentDate = _l[1];
        var _m = (0, tslib_1.__read)((0, react_1.useState)(inputError), 2), error = _m[0], setError = _m[1];
        var _o = (0, tslib_1.__read)((0, react_1.useState)(isShowMaskProps), 2), isShowMask = _o[0], setShowMask = _o[1];
        var minimalDate = (0, react_1.useMemo)(function () { return (0, date_1.formatISO8601ToDate)(minDate); }, [minDate]);
        var maximalDate = (0, react_1.useMemo)(function () { return (0, date_1.formatISO8601ToDate)(maxDate); }, [maxDate]);
        var autoCorrectedDatePipe = (0, react_1.useMemo)(function () { return (0, createAutoCorrectedDatePipe_1.default)(date_1.ISO8601, {}); }, []);
        var mounted = (0, react_1.useMemo)(function () { return ({ current: interfaces_1.MountedState.BEFORE }); }, []);
        var mountedState = mounted.current;
        var onDateChange = function (_value) {
            setCurrentDate(_value);
            onChange === null || onChange === void 0 ? void 0 : onChange(_value);
        };
        var closeCalendarAndResetDate = function () {
            setCalendarShown(false);
            handleSelected();
        };
        var checkErrors = function (date) {
            if (isRangeMode && !checkValidDateRange(date)) {
                setError(constants_1.ErrorRangeDateMsg);
            }
            else if (!isRangeMode && !checkValidDate(date)) {
                setError(constants_1.ErrorValidateDateMsg);
            }
            else {
                setError(undefined);
            }
        };
        var handleSettingDateToInputFromCalendar = function (date) {
            if (isRangeMode) {
                setDateRangeToInput(date);
            }
            else if (date !== currentDate) {
                checkErrors(date);
                setCurrentDate(date);
                onChange === null || onChange === void 0 ? void 0 : onChange(date);
                checkValidDate(date) && (onDateSelected === null || onDateSelected === void 0 ? void 0 : onDateSelected(date));
            }
        };
        var setDateRangeToInput = function (date) {
            var _a = (0, tslib_1.__read)(currentDate.split(" / "), 2), rangeDateStart = _a[0], rangeDateEnd = _a[1];
            var dateStart = rangeDateStart || constants_1.DateStartPlaceholder;
            var dateEnd = rangeDateEnd || constants_1.DateStartPlaceholder;
            if (constants_1.datePattern.test(dateStart) && constants_1.datePattern.test(dateEnd)) {
                setCurrentDate(date + constants_1.DateEndPlaceholder);
            }
            else if (constants_1.datePattern.test(dateStart)) {
                var dateRange = dateStart + " / " + date;
                var currentDateRange = checkValidDateRange(dateRange)
                    ? dateRange
                    : date + " / " + dateStart;
                checkErrors(currentDateRange);
                setCurrentDate(currentDateRange);
                onChange === null || onChange === void 0 ? void 0 : onChange(currentDateRange);
            }
            else {
                setCurrentDate(date + " / " + dateEnd);
                onChange === null || onChange === void 0 ? void 0 : onChange(date + " / " + dateEnd);
            }
        };
        var checkValidDate = function (date) {
            if (constants_1.datePattern.test(date)) {
                var dateInISO = (0, date_1.formatISO8601ToDate)(date);
                var isMinimalDateValid = minimalDate === undefined || minimalDate <= dateInISO;
                var isMaximalDateValid = maximalDate === undefined || maximalDate >= dateInISO;
                return isMinimalDateValid && isMaximalDateValid;
            }
            return false;
        };
        var checkValidDateRange = function (date) {
            var _a = (0, tslib_1.__read)(date.split(" / "), 2), dateStart = _a[0], dateEnd = _a[1];
            if (!constants_1.datePattern.test(dateStart) && !constants_1.datePattern.test(dateEnd)) {
                return false;
            }
            var dateStartISO = (0, date_1.formatISO8601ToDate)(dateStart);
            var dateEndISO = (0, date_1.formatISO8601ToDate)(dateEnd);
            if (!checkValidDate(dateStart) || !checkValidDate(dateEnd)) {
                setError(constants_1.ErrorValidateDateMsg);
                return false;
            }
            else {
                setError(undefined);
            }
            if (dateStartISO > dateEndISO) {
                setError(constants_1.ErrorRangeDateMsg);
            }
            return dateStartISO <= dateEndISO;
        };
        var handleKeyDown = function (event) {
            if (event.key === "Escape") {
                closeCalendarAndResetDate();
            }
            if (event.key === "Enter") {
                if ((isRangeMode && checkValidDateRange(currentDate)) ||
                    (!isRangeMode && checkValidDate(currentDate))) {
                    checkErrors(currentDate);
                    value !== currentDate && (onDateSelected === null || onDateSelected === void 0 ? void 0 : onDateSelected(currentDate));
                }
            }
            onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(event);
        };
        var handleFocus = function (event) {
            setShowMask(true);
            onFocus === null || onFocus === void 0 ? void 0 : onFocus(event);
        };
        var handleBlur = function () {
            setShowMask(false);
            handleSelected();
            onBlur === null || onBlur === void 0 ? void 0 : onBlur();
        };
        var handleCloseCalendar = function () {
            setCalendarShown(false);
            value !== currentDate && handleSelected();
        };
        var handleSelected = function () {
            if (isRangeMode) {
                !checkValidDateRange(currentDate) && setCurrentDate(value);
                !checkValidDateRange(currentDate) && (onDateSelected === null || onDateSelected === void 0 ? void 0 : onDateSelected(value));
            }
            else {
                checkErrors(currentDate);
                !checkValidDate(currentDate) && setCurrentDate(value);
                !checkValidDate(currentDate) && (onDateSelected === null || onDateSelected === void 0 ? void 0 : onDateSelected(value));
            }
        };
        (0, react_1.useEffect)(function () {
            if (mountedState === interfaces_1.MountedState.BEFORE) {
                mounted.current = interfaces_1.MountedState.CURRENT;
            }
            else {
                mounted.current = interfaces_1.MountedState.AFTER;
            }
        });
        (0, react_1.useEffect)(function () {
            if (isCalendarShown) {
                onCalendarOpen === null || onCalendarOpen === void 0 ? void 0 : onCalendarOpen();
            }
            else {
                onCalendarClose === null || onCalendarClose === void 0 ? void 0 : onCalendarClose();
            }
        }, [isCalendarShown]);
        (0, react_1.useEffect)(function () {
            setCurrentDate(value);
        }, [value]);
        (0, react_1.useEffect)(function () {
            setShowMask(isShowMask);
        }, [isShowMask]);
        (0, react_1.useEffect)(function () {
            var _a = (0, tslib_1.__read)(currentDate.split(" / "), 2), dateStart = _a[0], dateEnd = _a[1];
            if (constants_1.datePattern.test(dateStart) && constants_1.datePattern.test(dateEnd)) {
                handleCloseCalendar();
            }
        }, [currentDate]);
        (0, useOnClickOutside_1.useOnClickOutside)(datePickerRef, closeCalendarAndResetDate, isCalendarShown);
        return (react_1.default.createElement(styles_1.DatePickerWrapper, { width: width, className: className, ref: datePickerRef },
            react_1.default.createElement(styles_1.InputDateWrapper, null,
                react_1.default.createElement(styles_1.StyledInputDate, { className: className, testId: testId, id: id, isCalendarShown: isCalendarShown, pipe: autoCorrectedDatePipe, placeholder: placeholder, isRangeMode: isRangeMode, toggleCalendar: function () { return setCalendarShown(!isCalendarShown); }, onDateChange: onDateChange, value: currentDate, isDisabled: isDisabled, onKeyDown: handleKeyDown, onBlur: handleBlur, onFocus: handleFocus, isGuide: true, isShowMask: isShowMask, isKeepCharPositions: true, type: "tel", placeholderChar: placeholderChar, width: width, statusText: statusText, isRequired: isRequired, isShowClearIcon: isShowClearIcon, status: status, size: size, iconLeft: iconLeft, iconRight: iconRight, isReadOnly: isReadOnly, viewType: viewType, onClear: onClear })),
            isCalendarShown && (react_1.default.createElement(styles_1.CalendarWrapper, null,
                react_1.default.createElement(Calendar_1.default, { id: idCalendar, minDate: minDate, maxDate: maxDate, locale: locale, isRangeMode: isRangeMode, closeCalendar: handleCloseCalendar, date: value, setDateToInput: handleSettingDateToInputFromCalendar }))),
            typeof error === "string" && !!error.trim() && (react_1.default.createElement(styles_1.Error, { type: "pavo", "data-element": "input-error", "data-testid": testId + "_errorCheck" }, error))));
    };
    exports.default = InputDate;
});
