(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", "@greenatom/icons", "../../InputMask", "../constants"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importDefault)(require("react"));
    var icons_1 = require("@greenatom/icons");
    var InputMask_1 = (0, tslib_1.__importDefault)(require("../../InputMask"));
    var constants_1 = require("../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 <= constants_1.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 ? constants_1.PlaceholderChar : _b, onKeyDown = _a.onKeyDown, isShowClearIcon = _a.isShowClearIcon, isReadOnly = _a.isReadOnly, onClear = _a.onClear, other = (0, tslib_1.__rest)(_a, ["id", "placeholder", "toggleCalendar", "onDateChange", "value", "isRangeMode", "error", "onBlur", "onFocus", "pipe", "placeholderChar", "onKeyDown", "isShowClearIcon", "isReadOnly", "onClear"]);
        var mask = isRangeMode ? constants_1.dateRangeMask : constants_1.dateMask;
        var handleKeyPress = function (event) {
            if (isRangeMode &&
                value &&
                !isCorrectedDatePipe(value.replace(new RegExp(placeholderChar, ""), event.key), placeholderChar)) {
                event.preventDefault();
            }
        };
        var isNumeric = function (_value) { return constants_1.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_1.default.createElement(InputMask_1.default, (0, tslib_1.__assign)({}, other, { id: id, isReadOnly: isReadOnly, pipe: pipe, error: error, type: "tel", placeholder: placeholder, mask: mask, value: value, iconRight: react_1.default.createElement(icons_1.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 })));
    };
    exports.default = InputDate;
});
