"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCalendar = void 0;
const vue_1 = require("vue");
const date_fns_1 = require("date-fns");
const utils_1 = require("../utils");
const use_panel_common_1 = require("./use-panel-common");
const interface_1 = require("../interface");
const useCalendarProps = Object.assign(Object.assign({}, use_panel_common_1.usePanelCommon.props), { actions: {
        type: Array,
        default: () => ['now', 'clear', 'confirm']
    } });
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function useCalendar(props, type) {
    const panelCommon = use_panel_common_1.usePanelCommon(props);
    const { isValueInvalidRef, isDateDisabledRef, isDateInvalidRef, isTimeInvalidRef, isDateTimeInvalidRef, isHourDisabledRef, isMinuteDisabledRef, isSecondDisabledRef, localeRef, datePickerSlots
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     } = vue_1.inject(interface_1.datePickerInjectionKey);
    const validation = {
        isValueInvalid: isValueInvalidRef,
        isDateDisabled: isDateDisabledRef,
        isDateInvalid: isDateInvalidRef,
        isTimeInvalid: isTimeInvalidRef,
        isDateTimeInvalid: isDateTimeInvalidRef,
        isHourDisabled: isHourDisabledRef,
        isMinuteDisabled: isMinuteDisabledRef,
        isSecondDisabled: isSecondDisabledRef
    };
    const dateInputValueRef = vue_1.ref(props.value === null || Array.isArray(props.value)
        ? ''
        : date_fns_1.format(props.value, props.dateFormat));
    const calendarValueRef = vue_1.ref(props.value === null || Array.isArray(props.value)
        ? Date.now()
        : props.value);
    const nowRef = vue_1.ref(Date.now());
    const dateArrayRef = vue_1.computed(() => {
        return utils_1.dateArray(calendarValueRef.value, props.value, nowRef.value, localeRef.value.firstDayOfWeek);
    });
    const weekdaysRef = vue_1.computed(() => {
        return dateArrayRef.value.slice(0, 7).map((dateItem) => {
            const { ts } = dateItem;
            return date_fns_1.format(ts, localeRef.value.dayFormat, panelCommon.dateFnsOptions.value);
        });
    });
    const calendarMonthRef = vue_1.computed(() => {
        return date_fns_1.format(calendarValueRef.value, localeRef.value.monthFormat, panelCommon.dateFnsOptions.value);
    });
    const calendarYearRef = vue_1.computed(() => {
        return date_fns_1.format(calendarValueRef.value, localeRef.value.yearFormat, panelCommon.dateFnsOptions.value);
    });
    vue_1.watch(calendarValueRef, (value, oldValue) => {
        if (!date_fns_1.isSameMonth(value, oldValue)) {
            panelCommon.disableTransitionOneTick();
        }
    });
    vue_1.watch(vue_1.computed(() => props.value), (value) => {
        if (value !== null && !Array.isArray(value)) {
            dateInputValueRef.value = date_fns_1.format(value, props.dateFormat, panelCommon.dateFnsOptions.value);
            calendarValueRef.value = value;
        }
        else {
            dateInputValueRef.value = '';
        }
    });
    function sanitizeValue(value) {
        if (type === 'datetime')
            return date_fns_1.getTime(date_fns_1.startOfSecond(value));
        return date_fns_1.getTime(date_fns_1.startOfDay(value));
    }
    function mergedIsDateDisabled(ts) {
        const { isDateDisabled: { value: isDateDisabled } } = validation;
        if (!isDateDisabled)
            return false;
        return isDateDisabled(ts);
    }
    function handleDateInput(value) {
        const date = utils_1.strictParse(value, props.dateFormat, new Date(), panelCommon.dateFnsOptions.value);
        if (date_fns_1.isValid(date)) {
            if (props.value === null) {
                panelCommon.doUpdateValue(date_fns_1.getTime(sanitizeValue(Date.now())), false);
            }
            else if (!Array.isArray(props.value)) {
                const newDateTime = date_fns_1.set(props.value, {
                    year: date_fns_1.getYear(date),
                    month: date_fns_1.getMonth(date),
                    date: date_fns_1.getDate(date)
                });
                panelCommon.doUpdateValue(date_fns_1.getTime(sanitizeValue(date_fns_1.getTime(newDateTime))), false);
            }
        }
        else {
            dateInputValueRef.value = value;
        }
    }
    function handleDateInputBlur() {
        const date = utils_1.strictParse(dateInputValueRef.value, props.dateFormat, new Date(), panelCommon.dateFnsOptions.value);
        if (date_fns_1.isValid(date)) {
            if (props.value === null) {
                panelCommon.doUpdateValue(date_fns_1.getTime(sanitizeValue(Date.now())), false);
            }
            else if (!Array.isArray(props.value)) {
                const newDateTime = date_fns_1.set(props.value, {
                    year: date_fns_1.getYear(date),
                    month: date_fns_1.getMonth(date),
                    date: date_fns_1.getDate(date)
                });
                panelCommon.doUpdateValue(date_fns_1.getTime(sanitizeValue(date_fns_1.getTime(newDateTime))), false);
            }
        }
        else {
            deriveDateInputValue();
        }
    }
    function clearSelectedDateTime() {
        panelCommon.doUpdateValue(null, true);
        dateInputValueRef.value = '';
        panelCommon.doClose();
    }
    function handleNowClick() {
        panelCommon.doUpdateValue(date_fns_1.getTime(sanitizeValue(Date.now())), true);
        calendarValueRef.value = Date.now();
        panelCommon.doClose();
    }
    function handleDateClick(dateItem) {
        if (mergedIsDateDisabled(dateItem.ts)) {
            return;
        }
        let newValue;
        if (props.value !== null && !Array.isArray(props.value)) {
            newValue = props.value;
        }
        else {
            newValue = Date.now();
        }
        newValue = date_fns_1.getTime(date_fns_1.set(newValue, dateItem.dateObject));
        panelCommon.doUpdateValue(date_fns_1.getTime(sanitizeValue(newValue)), type === 'date');
        if (type === 'date') {
            panelCommon.doClose();
        }
    }
    function deriveDateInputValue(time) {
        // If not selected, display nothing,
        // else update datetime related string
        if (props.value === null || Array.isArray(props.value)) {
            dateInputValueRef.value = '';
            return;
        }
        if (time === undefined) {
            time = props.value;
        }
        dateInputValueRef.value = date_fns_1.format(time, props.dateFormat, panelCommon.dateFnsOptions.value);
    }
    function handleConfirmClick() {
        if (validation.isDateInvalid.value || validation.isTimeInvalid.value) {
            return;
        }
        panelCommon.doConfirm();
        closeCalendar();
    }
    function closeCalendar() {
        if (props.active) {
            panelCommon.doClose();
        }
    }
    function nextYear() {
        calendarValueRef.value = date_fns_1.getTime(date_fns_1.addYears(calendarValueRef.value, 1));
    }
    function prevYear() {
        calendarValueRef.value = date_fns_1.getTime(date_fns_1.addYears(calendarValueRef.value, -1));
    }
    function nextMonth() {
        calendarValueRef.value = date_fns_1.getTime(date_fns_1.addMonths(calendarValueRef.value, 1));
    }
    function prevMonth() {
        calendarValueRef.value = date_fns_1.getTime(date_fns_1.addMonths(calendarValueRef.value, -1));
    }
    function handleTimePickerChange(value) {
        panelCommon.doUpdateValue(value, false);
    }
    return Object.assign(Object.assign(Object.assign({ dateArray: dateArrayRef, calendarYear: calendarYearRef, calendarMonth: calendarMonthRef, weekdays: weekdaysRef, mergedIsDateDisabled,
        nextYear,
        prevYear,
        nextMonth,
        prevMonth,
        handleNowClick,
        handleConfirmClick }, validation), panelCommon), { 
        // datetime only
        handleDateClick,
        handleDateInputBlur,
        handleDateInput,
        handleTimePickerChange,
        clearSelectedDateTime, timePickerSize: panelCommon.timePickerSize, dateInputValue: dateInputValueRef, datePickerSlots });
}
exports.useCalendar = useCalendar;
useCalendar.props = useCalendarProps;
