import { __assign, __rest } from "tslib";
import React from "react";
import { IconBrokenCalendar } from "@greenatom/icons";
import InputMask from "../../InputMask";
import { dateMask, dateRangeMask, PlaceholderChar, MaxDateTemplate, numericPattern } from "../constants";
var isCorrectedDatePipe = function (value, placeholderChar) {
    return value
        .split(" / ")[1]
        .replace(new RegExp(placeholderChar, "gi"), "")
        .split(".")
        .map(Number)
        .every(function (date, idx) { return isNaN(date) || date <= MaxDateTemplate[idx]; });
};
var InputDate = function (_a) {
    var id = _a.id, placeholder = _a.placeholder, toggleCalendar = _a.toggleCalendar, onDateChange = _a.onDateChange, value = _a.value, isRangeMode = _a.isRangeMode, error = _a.error, onBlur = _a.onBlur, onFocus = _a.onFocus, pipe = _a.pipe, _b = _a.placeholderChar, placeholderChar = _b === void 0 ? PlaceholderChar : _b, onKeyDown = _a.onKeyDown, isShowClearIcon = _a.isShowClearIcon, isReadOnly = _a.isReadOnly, onClear = _a.onClear, other = __rest(_a, ["id", "placeholder", "toggleCalendar", "onDateChange", "value", "isRangeMode", "error", "onBlur", "onFocus", "pipe", "placeholderChar", "onKeyDown", "isShowClearIcon", "isReadOnly", "onClear"]);
    var mask = isRangeMode ? dateRangeMask : dateMask;
    var handleKeyPress = function (event) {
        if (isRangeMode &&
            value &&
            !isCorrectedDatePipe(value.replace(new RegExp(placeholderChar, ""), event.key), placeholderChar)) {
            event.preventDefault();
        }
    };
    var isNumeric = function (_value) { return numericPattern.test(_value); };
    var handleKeyDown = function (event) {
        var target = event.target;
        var cursorPos = Number(target.selectionStart);
        var _value = target.value || "";
        if (event.key === "Delete" && cursorPos < _value.length) {
            event.preventDefault();
            if (isNumeric(value.charAt(cursorPos))) {
                target.value = "" + _value.substring(0, cursorPos) + placeholderChar + _value.substring(cursorPos + 1, _value.length);
            }
            target.setSelectionRange(cursorPos + 1, cursorPos + 1);
            onDateChange === null || onDateChange === void 0 ? void 0 : onDateChange(target.value);
        }
        onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(event);
    };
    var handleFocus = function (event) {
        var target = event.target;
        setTimeout(function () { return target.setSelectionRange(0, 0); }, 0);
        onFocus === null || onFocus === void 0 ? void 0 : onFocus(event);
    };
    return (React.createElement(InputMask, __assign({}, other, { id: id, isReadOnly: isReadOnly, pipe: pipe, error: error, type: "tel", placeholder: placeholder, mask: mask, value: value, iconRight: React.createElement(IconBrokenCalendar, null), isGuide: true, onFocus: handleFocus, onBlur: onBlur, onIconRightClick: toggleCalendar, onChange: function (_, _value) { return onDateChange === null || onDateChange === void 0 ? void 0 : onDateChange(_value); }, onKeyPress: handleKeyPress, onKeyDown: handleKeyDown, isShowClearIcon: isShowClearIcon, onClear: onClear })));
};
export default InputDate;
