"use strict";
/* eslint-disable react/display-name */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateInput = void 0;
const react_1 = __importStar(require("react"));
const DayPickerInput_1 = __importDefault(require("react-day-picker/DayPickerInput"));
require("react-day-picker/lib/style.css");
const moment_1 = __importDefault(require("moment"));
const TextInput_1 = require("../TextInput");
const Button_1 = require("../Button");
const Flex_1 = require("../Flex");
const Box_1 = require("../Box");
const Icon_1 = require("../Icon");
const icons = __importStar(require("../../assets/images/icons"));
const sc = __importStar(require("./styles"));
const formatDate = (date, dateFormat) => moment_1.default(date).format(dateFormat);
function DateInput(_a) {
    var { value, onChange, onBlur, autoFocus, placeholder = 'Date', dateFormat = 'MM/DD/YYYY', invalid, noExpiryOption, noExpiryCopy = 'No expiry', inputProps, dayPickerProps, minDate, maxDate, hasIcon } = _a, props = __rest(_a, ["value", "onChange", "onBlur", "autoFocus", "placeholder", "dateFormat", "invalid", "noExpiryOption", "noExpiryCopy", "inputProps", "dayPickerProps", "minDate", "maxDate", "hasIcon"]);
    const isNoExpiry = noExpiryOption &&
        (value === undefined || value === null || value === noExpiryCopy);
    const [currentSelected, setCurrentSelected] = react_1.useState(isNoExpiry
        ? noExpiryCopy
        : value === undefined
            ? undefined
            : formatDate(value, dateFormat));
    const pickerProps = dayPickerProps || {};
    if (minDate || maxDate) {
        if (minDate)
            pickerProps.fromMonth = minDate;
        if (maxDate)
            pickerProps.toMonth = maxDate;
        pickerProps.disabledDays = [
            ...(minDate ? [{ before: minDate }] : []),
            ...(maxDate ? [{ after: maxDate }] : []),
        ];
    }
    const handleChange = (selectedDate, modifiers) => {
        if (modifiers.disabled)
            return;
        const formattedDate = formatDate(selectedDate, dateFormat);
        setCurrentSelected(formattedDate);
        if (onChange)
            onChange(selectedDate);
    };
    const handleNoExpiry = () => {
        setCurrentSelected(noExpiryCopy);
        if (onChange)
            onChange(null);
    };
    return (react_1.default.createElement(DayPickerInput_1.default, Object.assign({ component: react_1.forwardRef((componentProps, ref) => (react_1.default.createElement(TextInput_1.TextInput, Object.assign({}, componentProps, { onBlur, autoFocus, placeholder, invalid }, { 
            // eslint-disable-next-line
            // @ts-ignore
            inputRef: ref, value: currentSelected }, (hasIcon
            ? {
                unit: (react_1.default.createElement(Icon_1.Icon, { Asset: icons.SvgCalendar, width: 16, height: 16, color: "gray.3" })),
            }
            : {}), inputProps)))), overlayComponent: (_a) => {
            var { children } = _a, overlayProps = __rest(_a, ["children"]);
            return (react_1.default.createElement(sc.Overlay, Object.assign({ borderRadius: "3", boxShadow: "2", borderColor: "gray.4", border: "1px solid", p: "m" }, overlayProps),
                react_1.default.createElement(Flex_1.Flex, { flexDirection: "column" },
                    children,
                    noExpiryOption && (react_1.default.createElement(Box_1.Box, { mt: "s" },
                        react_1.default.createElement(Button_1.Button, { variant: "ghost", onClick: handleNoExpiry }, noExpiryCopy))))));
        }, dayPickerProps: Object.assign({ onDayClick: handleChange, month: currentSelected && currentSelected !== noExpiryCopy
                ? new Date(currentSelected)
                : undefined, selectedDays: currentSelected && currentSelected !== noExpiryCopy
                ? [new Date(currentSelected)]
                : undefined }, pickerProps) }, props)));
}
exports.DateInput = DateInput;
DateInput.displayName = 'DateInput';
//# sourceMappingURL=DateInput.js.map