"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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.prototype.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.Datepicker = exports.DatepickerWithUnform = exports.DatepickerWithoutUnform = void 0;
var react_1 = __importStar(require("react"));
var useClickOutside_1 = require("@devesharp/react-hooks/lib/useClickOutside");
var useDidUpdateEffect_1 = require("@devesharp/react-hooks/lib/useDidUpdateEffect");
var useMemoImmer_1 = require("@devesharp/react-hooks/lib/useMemoImmer");
var Utils_1 = require("@devesharp/react-utils/lib/Utils");
var Calendar_1 = require("@devesharp/react-utils/lib/Calendar");
var GetScrollParent_1 = require("@devesharp/react-utils/lib/JSUtils/GetScrollParent");
var faCalendarAlt_1 = require("@fortawesome/free-regular-svg-icons/faCalendarAlt");
var faAngleLeft_1 = require("@fortawesome/free-solid-svg-icons/faAngleLeft");
var faAngleRight_1 = require("@fortawesome/free-solid-svg-icons/faAngleRight");
var faMinusCircle_1 = require("@fortawesome/free-solid-svg-icons/faMinusCircle");
var faPlusCircle_1 = require("@fortawesome/free-solid-svg-icons/faPlusCircle");
var faTimes_1 = require("@fortawesome/free-solid-svg-icons/faTimes");
var react_fontawesome_1 = require("@fortawesome/react-fontawesome");
var unform_core_1 = require("@devesharp/unform-core");
var addHours_1 = __importDefault(require("date-fns/addHours"));
var addMinutes_1 = __importDefault(require("date-fns/addMinutes"));
var addMonths_1 = __importDefault(require("date-fns/addMonths"));
var differenceInDays_1 = __importDefault(require("date-fns/differenceInDays"));
var differenceInMinutes_1 = __importDefault(require("date-fns/differenceInMinutes"));
var format_1 = __importDefault(require("date-fns/format"));
var getDate_1 = __importDefault(require("date-fns/getDate"));
var getHours_1 = __importDefault(require("date-fns/getHours"));
var getMinutes_1 = __importDefault(require("date-fns/getMinutes"));
var getMonth_1 = __importDefault(require("date-fns/getMonth"));
var getYear_1 = __importDefault(require("date-fns/getYear"));
var isSameDay_1 = __importDefault(require("date-fns/isSameDay"));
var pt_BR_1 = __importDefault(require("date-fns/locale/pt-BR"));
var parse_1 = __importDefault(require("date-fns/parse"));
var parseISO_1 = __importDefault(require("date-fns/parseISO"));
var setDate_1 = __importDefault(require("date-fns/setDate"));
var setHours_1 = __importDefault(require("date-fns/setHours"));
var setMilliseconds_1 = __importDefault(require("date-fns/setMilliseconds"));
var setMinutes_1 = __importDefault(require("date-fns/setMinutes"));
var setMonth_1 = __importDefault(require("date-fns/setMonth"));
var setSeconds_1 = __importDefault(require("date-fns/setSeconds"));
var subHours_1 = __importDefault(require("date-fns/subHours"));
var subMinutes_1 = __importDefault(require("date-fns/subMinutes"));
var subMonths_1 = __importDefault(require("date-fns/subMonths"));
var styled_components_1 = require("styled-components");
var styles_1 = require("../styles");
var Switch_1 = require("../Switch");
var Datepicker_style_1 = __importDefault(require("./Datepicker.style"));
function InputDatePicker(_a) {
    var date = _a.date, error = _a.error, setOpen = _a.setOpen, withTimePicker = _a.withTimePicker, removeDate = _a.removeDate, allDay = _a.allDay, calendarIcon = _a.calendarIcon, formatShowDate = _a.formatShowDate, formatTimeDate = _a.formatTimeDate;
    var viewDateStringView = date ? (0, format_1.default)(date, formatShowDate, { locale: pt_BR_1.default }) : '';
    var viewTimeStringView = date ? (0, format_1.default)(date, formatTimeDate, { locale: pt_BR_1.default }) : '';
    return (react_1.default.createElement(react_1.default.Fragment, null,
        !date && (react_1.default.createElement("div", { className: "input-border ".concat(error && 'has-error'), "data-testid": "datepicker-input" },
            react_1.default.createElement("div", { className: "prefix calendar-icon" }, calendarIcon || react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faCalendarAlt_1.faCalendarAlt })),
            react_1.default.createElement("div", { className: "input pl-3" }, "Selecione um data"),
            react_1.default.createElement("div", { className: "container-cursor", "data-testid": "open-popup", onClick: function () { return setOpen(true); } }))),
        !!date && (react_1.default.createElement("div", { className: "input-border", "data-testid": "view-date" },
            react_1.default.createElement("div", { className: "table calendar-input" },
                react_1.default.createElement("div", { className: "text-primary calendar-icon" },
                    react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faCalendarAlt_1.faCalendarAlt })),
                react_1.default.createElement("div", { className: "pl-3 text-primary font-weight-bold flex-fill date-input-container", "data-testid": "datepicker-input" },
                    react_1.default.createElement("div", { className: "date", "data-testid": "date-string" }, viewDateStringView),
                    withTimePicker && !allDay && react_1.default.createElement("div", { className: "time" }, viewTimeStringView),
                    withTimePicker && allDay && react_1.default.createElement("div", { className: "time" }, "O dia todo")),
                react_1.default.createElement("div", { className: "font-weight-bold align-self-center remove-date", "data-testid": "datepicker-remove-date", onClick: removeDate },
                    react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faTimes_1.faTimes }))),
            react_1.default.createElement("div", { className: "container-cursor", "data-testid": "open-popup", onClick: function () { return setOpen(true); } })))));
}
function DatePickerCalendar(_a) {
    var calendarDate = _a.calendarDate, setCalendarDate = _a.setCalendarDate, selectedDate = _a.selectedDate, setSelectedDate = _a.setSelectedDate, disablePastDates = _a.disablePastDates;
    var monthName = (0, useMemoImmer_1.useMemoImmer)(function () { return Calendar_1.Calendar.monthsName[(0, getMonth_1.default)(calendarDate)]; }, [calendarDate]);
    var calendarYear = (0, getYear_1.default)(calendarDate);
    var weekNames = (0, react_1.useMemo)(function () { return Calendar_1.Calendar.weeksName; }, []);
    var month = (0, react_1.useMemo)(function () { return Calendar_1.Calendar.getMonth((0, getMonth_1.default)(calendarDate), (0, getYear_1.default)(calendarDate)); }, [calendarDate]);
    var NextMonth = (0, react_1.useCallback)(function NextMonth() {
        setCalendarDate(function (d) { return (0, addMonths_1.default)(d, 1); });
    }, []);
    var PreviousMonth = (0, react_1.useCallback)(function PreviousMonth() {
        setCalendarDate(function (d) { return (0, subMonths_1.default)(d, 1); });
    }, []);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "month" },
            react_1.default.createElement("span", { className: "right", onClick: NextMonth },
                react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faAngleRight_1.faAngleRight, "data-testid": "datepicker-next-month" })),
            react_1.default.createElement("span", { className: "left", onClick: PreviousMonth },
                react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faAngleLeft_1.faAngleLeft, "data-testid": "datepicker-previous-month" })),
            react_1.default.createElement("div", { className: "month-name", "data-testid": "datepicker-month-name" },
                monthName,
                ", ",
                calendarYear)),
        weekNames.map(function (nameWeek, i) { return (react_1.default.createElement("div", { key: "".concat(nameWeek, "-").concat(i), className: "weekName" }, nameWeek)); }),
        month.map(function (week, weekI) { return (react_1.default.createElement("div", { key: weekI, className: "week" }, week.map(function (day) { return (react_1.default.createElement("div", { key: day.dateYYYYMMDD, "data-testid": day.dateYYYYMMDD, className: "day ".concat(day.today && 'today', " ").concat(disablePastDates && day.past && 'disable-past', " ").concat((0, isSameDay_1.default)(day.dateInstance, selectedDate) && 'select', " ").concat(!day.currentMonth && 'other-month'), onClick: function () { return setSelectedDate(day.dateInstance); } }, day.day)); }))); })));
}
function TimePicker(_a) {
    var date = _a.date, setDate = _a.setDate, allDayValue = _a.allDayValue, onAllDayChange = _a.onAllDayChange, enableAllDay = _a.enableAllDay, TimerPickerPlusComponent = _a.TimerPickerPlusComponent, TimerPickerMinusComponent = _a.TimerPickerMinusComponent;
    var theme = (0, styled_components_1.useTheme)();
    var _b = (theme !== null && theme !== void 0 ? theme : {}).datePicker, datePicker = _b === void 0 ? {} : _b;
    var defaultColor = datePicker.defaultColor;
    var hourInputRef = (0, react_1.useRef)();
    var minuteInputRef = (0, react_1.useRef)();
    (0, react_1.useEffect)(function () {
        hourInputRef.current.value = (0, format_1.default)(date !== null && date !== void 0 ? date : new Date(), 'HH');
        minuteInputRef.current.value = (0, format_1.default)(date !== null && date !== void 0 ? date : new Date(), 'mm');
    }, [date]);
    var SubHour = (0, react_1.useCallback)(function SubHour() {
        setDate((0, subHours_1.default)(date !== null && date !== void 0 ? date : new Date(), 1));
    }, [date]);
    var AddHour = (0, react_1.useCallback)(function AddHour() {
        setDate((0, addHours_1.default)(date !== null && date !== void 0 ? date : new Date(), 1));
    }, [date]);
    var SubMinute = (0, react_1.useCallback)(function SubMinute() {
        setDate((0, setHours_1.default)((0, subMinutes_1.default)(date !== null && date !== void 0 ? date : new Date(), 1), (0, getHours_1.default)(date !== null && date !== void 0 ? date : new Date())));
    }, [date]);
    var AddMinute = (0, react_1.useCallback)(function AddMinute() {
        setDate((0, setHours_1.default)((0, addMinutes_1.default)(date !== null && date !== void 0 ? date : new Date(), 1), (0, getHours_1.default)(date !== null && date !== void 0 ? date : new Date())));
    }, [date]);
    var OnChangeHour = (0, react_1.useCallback)(function OnChangeHour() {
        if (hourInputRef.current.value.length >= 2) {
            minuteInputRef.current.focus();
            minuteInputRef.current.select();
        }
    }, [date]);
    var OnBlurHours = (0, react_1.useCallback)(function OnBlurHours() {
        var newDate = (0, setHours_1.default)(date !== null && date !== void 0 ? date : new Date(), Number((0, Utils_1.OnlyNumbers)(hourInputRef.current.value)));
        newDate = (0, setDate_1.default)(newDate, (0, getDate_1.default)(date !== null && date !== void 0 ? date : new Date()));
        newDate = (0, setMonth_1.default)(newDate, (0, getMonth_1.default)(date !== null && date !== void 0 ? date : new Date()));
        setDate(newDate);
    }, [date]);
    var OnChangeMinutes = (0, react_1.useCallback)(function OnChangeMinutes() { }, [date]);
    var OnBlurMinutes = (0, react_1.useCallback)(function OnBlurMinutes() {
        var newDate = (0, setMinutes_1.default)(date !== null && date !== void 0 ? date : new Date(), Number((0, Utils_1.OnlyNumbers)(minuteInputRef.current.value)));
        newDate = (0, setHours_1.default)(newDate, (0, getHours_1.default)(date !== null && date !== void 0 ? date : new Date()));
        newDate = (0, setDate_1.default)(newDate, (0, getDate_1.default)(date !== null && date !== void 0 ? date : new Date()));
        newDate = (0, setMonth_1.default)(newDate, (0, getMonth_1.default)(date !== null && date !== void 0 ? date : new Date()));
        setDate(newDate);
    }, [date]);
    return (react_1.default.createElement("div", null,
        react_1.default.createElement("hr", null),
        enableAllDay && (react_1.default.createElement(Switch_1.Switch, { size: 18, title: "Dia todo", checked: allDayValue, onChange: onAllDayChange, "data-testid": "datepicker-all-day", color: defaultColor })),
        react_1.default.createElement("div", { className: "timepicker" },
            react_1.default.createElement("div", { className: "d-flex w-100" },
                react_1.default.createElement("div", { className: "d-flex flex-fill" },
                    react_1.default.createElement("div", { className: "plus flex-fill align-self-center", onClick: SubHour }, TimerPickerMinusComponent || react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faMinusCircle_1.faMinusCircle })),
                    react_1.default.createElement("div", { className: "input-time" },
                        react_1.default.createElement("input", { type: "text", defaultValue: "00", ref: hourInputRef, onFocus: function () { return hourInputRef.current.select(); }, onChange: OnChangeHour, onBlur: OnBlurHours, maxLength: 3, "data-testid": "datepicker-input-hour" })),
                    react_1.default.createElement("div", { className: "plus flex-fill align-self-center", onClick: AddHour }, TimerPickerMinusComponent ? TimerPickerPlusComponent : react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faPlusCircle_1.faPlusCircle }))),
                react_1.default.createElement("span", { className: "division" }, ":"),
                react_1.default.createElement("div", { className: "d-flex flex-fill" },
                    react_1.default.createElement("div", { className: "plus flex-fill align-self-center", onClick: SubMinute }, TimerPickerMinusComponent || react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faMinusCircle_1.faMinusCircle })),
                    react_1.default.createElement("div", { className: "input-time" },
                        react_1.default.createElement("input", { type: "text", defaultValue: "00", ref: minuteInputRef, onFocus: function () { return minuteInputRef.current.select(); }, onChange: OnChangeMinutes, onBlur: OnBlurMinutes, maxLength: 2, "data-testid": "datepicker-input-minute" })),
                    react_1.default.createElement("div", { className: "plus flex-fill align-self-center", onClick: AddMinute }, TimerPickerMinusComponent ? TimerPickerPlusComponent : react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: faPlusCircle_1.faPlusCircle })))))));
}
function DatePickerPopup(_a) {
    var enableAllDay = _a.enableAllDay, date = _a.date, setDate = _a.setDate, allDayValue = _a.allDayValue, onAllDayChange = _a.onAllDayChange, calendarDate = _a.calendarDate, setCalendarDate = _a.setCalendarDate, withTimePicker = _a.withTimePicker, applyOnSelect = _a.applyOnSelect, cleanDatePicker = _a.cleanDatePicker, disablePastDates = _a.disablePastDates, onApply = _a.onApply, TimerPickerPlusComponent = _a.TimerPickerPlusComponent, TimerPickerMinusComponent = _a.TimerPickerMinusComponent;
    var datePickerPopupRef = (0, react_1.useRef)(null);
    (0, useClickOutside_1.useClickOutside)(function () {
        cleanDatePicker();
    }, datePickerPopupRef);
    return (react_1.default.createElement("div", { className: "datepicker-popup", ref: datePickerPopupRef, "data-testid": "datepicker-popup" },
        react_1.default.createElement(DatePickerCalendar, { calendarDate: calendarDate, selectedDate: date, setSelectedDate: setDate, setCalendarDate: setCalendarDate, disablePastDates: disablePastDates }),
        withTimePicker && (react_1.default.createElement(TimePicker, { date: date, setDate: setDate, allDayValue: allDayValue, onAllDayChange: onAllDayChange, enableAllDay: enableAllDay, TimerPickerPlusComponent: TimerPickerPlusComponent, TimerPickerMinusComponent: TimerPickerMinusComponent })),
        applyOnSelect && (react_1.default.createElement("div", { className: "actions" },
            react_1.default.createElement("div", { className: "btn-cancel", "data-testid": "datepicker-cancel", onClick: cleanDatePicker }, "Cancelar"),
            react_1.default.createElement("div", { className: "btn-apply", "data-testid": "datepicker-apply", onClick: onApply }, "Aplicar")))));
}
var DatepickerWithoutUnform = function (_a) {
    var name = _a.name, title = _a.title, _b = _a.withTimePicker, withTimePicker = _b === void 0 ? false : _b, _c = _a.value, value = _c === void 0 ? null : _c, onValueChange = _a.onValueChange, _d = _a.allDay, allDayValue = _d === void 0 ? false : _d, _e = _a.onAllDayChange, onAllDayChange = _e === void 0 ? function () { } : _e, error = _a.error, _f = _a.applyOnSelect, applyOnSelect = _f === void 0 ? false : _f, formattedString = _a.formattedString, _g = _a.formatShowDate, formatShowDate = _g === void 0 ? 'd MMM yyyy' : _g, _h = _a.formatTimeDate, formatTimeDate = _h === void 0 ? "'Ás' HH:mm" : _h, _j = _a.enableAllDay, enableAllDay = _j === void 0 ? false : _j, disablePastDates = _a.disablePastDates, asPopup = _a.asPopup, children = _a.children, TimerPickerPlusComponent = _a.TimerPickerPlusComponent, TimerPickerMinusComponent = _a.TimerPickerMinusComponent, calendarIcon = _a.calendarIcon, rest = __rest(_a, ["name", "title", "withTimePicker", "value", "onValueChange", "allDay", "onAllDayChange", "error", "applyOnSelect", "formattedString", "formatShowDate", "formatTimeDate", "enableAllDay", "disablePastDates", "asPopup", "children", "TimerPickerPlusComponent", "TimerPickerMinusComponent", "calendarIcon"]);
    var datepickerRef = (0, react_1.useRef)();
    var _k = (0, react_1.useState)(value !== null && value !== void 0 ? value : new Date()), calendarDate = _k[0], setCalendarDate = _k[1];
    var _l = (0, react_1.useState)(value), dateTmp = _l[0], setDateTmp = _l[1];
    var _m = (0, react_1.useState)(allDayValue), allDay = _m[0], setAllDay = _m[1];
    var _o = (0, react_1.useState)(false), openDatePicker = _o[0], setOpenDatePicker = _o[1];
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        setAllDay(allDayValue);
    }, [allDayValue]);
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        setDateTmp(value);
    }, [value]);
    var OpenDatePicker = (0, react_1.useCallback)(function OpenDatePicker() {
        setOpenDatePicker(true);
    }, []);
    var CloseDatePicker = (0, react_1.useCallback)(function CloseDatePicker() {
        setOpenDatePicker(false);
    }, []);
    var CleanDatePicker = (0, react_1.useCallback)(function CleanDatePicker() {
        setDateTmp(value);
        setCalendarDate(value !== null && value !== void 0 ? value : new Date());
        setOpenDatePicker(false);
        setAllDay(allDayValue);
    }, [value, allDayValue]);
    var RemoveDateInput = (0, react_1.useCallback)(function RemoveDateInput() {
        if (onValueChange)
            onValueChange(null, null);
        setCalendarDate(new Date());
        if (onAllDayChange)
            onAllDayChange(false);
        setOpenDatePicker(false);
    }, [onValueChange]);
    var SetSelectedDate = (0, react_1.useCallback)(function SetSelectedDate(d) {
        if (disablePastDates && (0, differenceInDays_1.default)(d, new Date()) < 0) {
            return;
        }
        if (disablePastDates && (0, differenceInMinutes_1.default)(d, new Date()) < 0) {
            d = (0, setMinutes_1.default)(d, (0, getMinutes_1.default)(new Date()));
            d = (0, setHours_1.default)(d, (0, getHours_1.default)(new Date()));
        }
        if (!withTimePicker) {
            d = (0, setMinutes_1.default)(d, 0);
            d = (0, setHours_1.default)(d, 0);
        }
        d = (0, setSeconds_1.default)(d, 0);
        d = (0, setMilliseconds_1.default)(d, 0);
        if (!applyOnSelect) {
            if (onValueChange)
                onValueChange(d, formattedString ? (0, format_1.default)(d, formattedString, { locale: pt_BR_1.default }) : null);
            CloseDatePicker();
        }
        else {
            setDateTmp(d);
        }
    }, [onValueChange]);
    var OnApply = (0, react_1.useCallback)(function OnApply() {
        if (onValueChange)
            onValueChange(dateTmp, dateTmp && formattedString ? (0, format_1.default)(dateTmp, formattedString) : null);
        if (onAllDayChange)
            onAllDayChange(allDay);
        CloseDatePicker();
    }, [dateTmp, onValueChange, allDay]);
    (0, react_1.useEffect)(function () {
        if (openDatePicker) {
            var el = document.getElementsByClassName('datepicker-popup')[0];
            var scrollParent = (0, GetScrollParent_1.GetScrollParent)(datepickerRef.current.parentNode);
            var _a = datepickerRef.current.getBoundingClientRect(), topMain = _a.top, heightMain = _a.height;
            var heightScroll = scrollParent.getBoundingClientRect().height;
            if (topMain + heightMain + el.getBoundingClientRect().height > heightScroll) {
                if (el.scrollIntoView)
                    el.scrollIntoView({ block: 'end', behavior: 'smooth' });
            }
        }
    }, [openDatePicker]);
    if (withTimePicker)
        applyOnSelect = true;
    return (react_1.default.createElement(styles_1.FormStyle, { className: "date-picker-container" },
        react_1.default.createElement(Datepicker_style_1.default, { "data-testid": "datepicker-".concat(name), ref: datepickerRef },
            title && react_1.default.createElement("div", { className: "input-title" }, title),
            !asPopup && (react_1.default.createElement(InputDatePicker, { date: value, error: !!error, allDay: allDayValue, setOpen: OpenDatePicker, withTimePicker: withTimePicker, removeDate: RemoveDateInput, calendarIcon: calendarIcon, formatShowDate: formatShowDate, formatTimeDate: formatTimeDate })),
            asPopup && (react_1.default.createElement("div", { className: "container-cursor", "data-testid": "open-popup", onClick: OpenDatePicker }, children)),
            openDatePicker && (react_1.default.createElement(DatePickerPopup, { enableAllDay: enableAllDay, date: dateTmp, setDate: SetSelectedDate, calendarDate: calendarDate, setCalendarDate: setCalendarDate, withTimePicker: withTimePicker, TimerPickerPlusComponent: TimerPickerPlusComponent, TimerPickerMinusComponent: TimerPickerMinusComponent, applyOnSelect: applyOnSelect, onApply: OnApply, cleanDatePicker: CleanDatePicker, allDayValue: allDay, onAllDayChange: setAllDay, disablePastDates: disablePastDates })),
            error && (react_1.default.createElement("div", { className: "error-input", "data-testid": "input-error" }, error)))));
};
exports.DatepickerWithoutUnform = DatepickerWithoutUnform;
function AllDayUnform(_a) {
    var name = _a.name, allDay = _a.allDay, onChange = _a.onChange;
    var _b = (0, unform_core_1.useField)(name), fieldName = _b.fieldName, registerField = _b.registerField, _c = _b.defaultValue, defaultValue = _c === void 0 ? false : _c;
    var inputRef = (0, react_1.useRef)(defaultValue);
    (0, react_1.useLayoutEffect)(function () {
        onChange(defaultValue);
    }, []);
    (0, react_1.useEffect)(function () {
        inputRef.current = allDay;
    }, [allDay]);
    (0, react_1.useEffect)(function () {
        registerField({
            name: fieldName,
            ref: inputRef,
            path: 'current',
            clearValue: function (ref) {
                ref.current = false;
                onChange(false);
            },
            setValue: function (ref, v) {
                ref.current = v;
                onChange(v);
            },
            getValue: function (ref) {
                return ref.current;
            },
        });
    }, [fieldName, registerField, onChange]);
    return null;
}
var DatepickerWithUnform = function (props) {
    var field = (0, unform_core_1.useField)(props.name);
    var _a = field.defaultValue, defaultValue = _a === void 0 ? null : _a;
    var fieldName = field.fieldName, registerField = field.registerField, error = field.error, clearError = field.clearError;
    if (props.formattedString && typeof (defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.getMonth) === 'function') {
        throw new Error("Se formattedString for definido 'value' tem ser string");
    }
    if (!props.formattedString && typeof (defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.getMonth) !== 'function' && defaultValue != null) {
        defaultValue = (0, parseISO_1.default)(defaultValue);
        if (defaultValue.getTime() !== defaultValue.getTime()) {
            throw new Error("'value' deve ser um Date ou null");
        }
    }
    var inputDateRef = (0, react_1.useRef)(defaultValue && props.formattedString ? (0, parse_1.default)(defaultValue, props.formattedString, new Date()) : defaultValue);
    var inputFormattedRef = (0, react_1.useRef)(typeof defaultValue === 'string' ? defaultValue : null);
    var _b = (0, react_1.useState)(inputDateRef.current), value = _b[0], setValue = _b[1];
    var _c = (0, react_1.useState)(), allDay = _c[0], setAllDay = _c[1];
    (0, react_1.useEffect)(function () {
        registerField({
            name: fieldName,
            ref: inputDateRef,
            path: 'current',
            clearValue: function (ref) {
                if (props.formattedString) {
                    inputFormattedRef.current = null;
                }
                ref.current = null;
                setValue(null);
            },
            setValue: function (ref, v) {
                if (props.formattedString && typeof (v === null || v === void 0 ? void 0 : v.getMonth) === 'function') {
                    throw new Error("Se formattedString for definido 'value' tem ser string");
                }
                if (!props.formattedString && typeof (v === null || v === void 0 ? void 0 : v.getMonth) !== 'function' && v != null) {
                    v = (0, parseISO_1.default)(v);
                    if (v.getTime() !== v.getTime()) {
                        throw new Error("'value' deve ser um Date ou null");
                    }
                }
                if (typeof v === 'string') {
                    ref.current = v && props.formattedString ? (0, parse_1.default)(v, props.formattedString, new Date()) : v;
                    inputFormattedRef.current = v;
                }
                else {
                    inputFormattedRef.current = null;
                    ref.current = v;
                    setValue(v);
                }
            },
            getValue: function (ref) {
                if (props.formattedString)
                    return inputFormattedRef.current;
                return ref.current;
            },
        });
    }, [fieldName, registerField, props.formattedString]);
    var onValueChange = (0, react_1.useCallback)(function onSelectItem(d, f) {
        if (error)
            clearError();
        setValue(d);
        inputDateRef.current = d;
        inputFormattedRef.current = f;
        if (props.onValueChange)
            props.onValueChange(d, f);
    }, [error]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        props.nameAllDay && react_1.default.createElement(AllDayUnform, { name: props.nameAllDay, allDay: allDay, onChange: setAllDay }),
        react_1.default.createElement(exports.DatepickerWithoutUnform, __assign({}, props, { value: value, onValueChange: onValueChange, allDay: allDay, onAllDayChange: setAllDay, error: error }))));
};
exports.DatepickerWithUnform = DatepickerWithUnform;
var Datepicker = function (props) {
    if (props.name) {
        return react_1.default.createElement(exports.DatepickerWithUnform, __assign({}, props));
    }
    return react_1.default.createElement(exports.DatepickerWithoutUnform, __assign({}, props));
};
exports.Datepicker = Datepicker;
//# sourceMappingURL=Datepicker.js.map