"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get calculateDateBoundary () {
        return calculateDateBoundary;
    },
    get formatPickerOption () {
        return formatPickerOption;
    },
    get formatValue () {
        return formatValue;
    },
    get generateDatePickerRanges () {
        return generateDatePickerRanges;
    },
    get generatePickerColumnWithCallback () {
        return generatePickerColumnWithCallback;
    },
    get getDatePartValue () {
        return getDatePartValue;
    },
    get getLastDayOfMonth () {
        return getLastDayOfMonth;
    },
    get handlePickerValueChange () {
        return handlePickerValueChange;
    }
});
var _define_property = require("@swc/helpers/_/_define_property");
var _padzero = require("../../utils/pad-zero");
var _isdate = require("../../utils/is-date");
function getLastDayOfMonth(year, month) {
    return new Date(year, month, 0).getDate();
}
var calculateDateBoundary = function calculateDateBoundary(type, value, startDate, endDate) {
    var boundary = type === 'min' ? startDate : endDate;
    var year = boundary.getFullYear();
    var isMax = type === 'max';
    var month = isMax ? 12 : 1;
    var date = isMax ? getLastDayOfMonth(value.getFullYear(), value.getMonth() + 1) : 1;
    var hour = isMax ? 23 : 0;
    var minute = isMax ? 59 : 0;
    if (value.getFullYear() === year) {
        month = boundary.getMonth() + 1;
        if (value.getMonth() + 1 === month) {
            date = boundary.getDate();
            if (value.getDate() === date) {
                hour = boundary.getHours();
                if (value.getHours() === hour) {
                    minute = boundary.getMinutes();
                }
            }
        }
    }
    var _obj;
    // 返回边界值的对象
    return _obj = {}, (0, _define_property._)(_obj, "".concat(type, "Year"), year), (0, _define_property._)(_obj, "".concat(type, "Month"), month), (0, _define_property._)(_obj, "".concat(type, "Date"), date), (0, _define_property._)(_obj, "".concat(type, "Hour"), hour), (0, _define_property._)(_obj, "".concat(type, "Minute"), minute), (0, _define_property._)(_obj, "".concat(type, "Seconds"), minute), _obj;
};
var generateDatePickerRanges = function generateDatePickerRanges(type, selectedDate, startDate, endDate) {
    var selected = selectedDate ? new Date(selectedDate) : new Date(startDate);
    if (!selected) return [];
    // 获取最大和最小边界值
    var _calculateDateBoundary = calculateDateBoundary('max', selected, startDate, endDate), maxYear = _calculateDateBoundary.maxYear, maxDate = _calculateDateBoundary.maxDate, maxMonth = _calculateDateBoundary.maxMonth, maxHour = _calculateDateBoundary.maxHour, maxMinute = _calculateDateBoundary.maxMinute, maxSeconds = _calculateDateBoundary.maxSeconds;
    var _calculateDateBoundary1 = calculateDateBoundary('min', selected, startDate, endDate), minYear = _calculateDateBoundary1.minYear, minDate = _calculateDateBoundary1.minDate, minMonth = _calculateDateBoundary1.minMonth, minHour = _calculateDateBoundary1.minHour, minMinute = _calculateDateBoundary1.minMinute, minSeconds = _calculateDateBoundary1.minSeconds;
    var fullRanges = [
        {
            type: 'year',
            range: [
                minYear,
                maxYear
            ]
        },
        {
            type: 'month',
            range: [
                minMonth,
                maxMonth
            ]
        },
        {
            type: 'day',
            range: [
                minDate,
                maxDate
            ]
        },
        {
            type: 'hour',
            range: [
                minHour,
                maxHour
            ]
        },
        {
            type: 'minute',
            range: [
                minMinute,
                maxMinute
            ]
        },
        {
            type: 'seconds',
            range: [
                minSeconds,
                maxSeconds
            ]
        }
    ];
    // 根据类型返回对应的范围配置
    switch(type.toLocaleLowerCase()){
        case 'date':
            return fullRanges.slice(0, 3);
        case 'datetime':
            return fullRanges.slice(0, 5);
        case 'time':
            return fullRanges.slice(3, 6);
        case 'year-month':
            return fullRanges.slice(0, 2);
        case 'hour-minutes':
            return fullRanges.slice(3, 5);
        case 'month-day':
            return fullRanges.slice(1, 3);
        case 'datehour':
            return fullRanges.slice(0, 4);
        default:
            return fullRanges;
    }
};
var getDatePartValue = function getDatePartValue(type, selectedDate) {
    var date = new Date(selectedDate);
    if (!selectedDate) return 0;
    switch(type){
        case 'year':
            return date.getFullYear();
        case 'month':
            return date.getMonth() + 1;
        case 'day':
            return date.getDate();
        case 'hour':
            return date.getHours();
        case 'minute':
            return date.getMinutes();
        case 'seconds':
            return date.getSeconds();
        default:
            return 0;
    }
};
var generatePickerColumnWithCallback = function generatePickerColumnWithCallback(min, max, currentValue, type, minuteStep, callback, showChinese, zhCNType, formatter) {
    var currentMin = min;
    var options = [];
    var selectedIndex = 0;
    // 遍历从最小值到最大值的范围
    while(currentMin <= max){
        // 将当前值格式化为 PickerOption 并添加到数组中
        options.push(formatPickerOption(type, currentMin, showChinese, zhCNType, formatter));
        // 根据类型决定步长：如果是分钟，使用 minuteStep，否则步长为 1
        if (type === 'minute') {
            currentMin += minuteStep;
        } else {
            currentMin++;
        }
        // 如果当前值小于等于选中的值，更新选中索引
        if (currentMin <= Number(currentValue)) {
            selectedIndex++;
        }
    }
    callback(selectedIndex, options);
    return options;
};
var formatPickerOption = function formatPickerOption(type, value, showChinese, zhCNType, formatter) {
    if (formatter) {
        return formatter(type, {
            label: (0, _padzero.padZero)(value, 2),
            value: (0, _padzero.padZero)(value, 2)
        });
    }
    var paddedValue = (0, _padzero.padZero)(value, 2);
    var chineseText = showChinese ? zhCNType[type] : '';
    return {
        label: paddedValue + chineseText,
        value: paddedValue
    };
};
var formatValue = function formatValue(value, startDate, endDate) {
    if (!value || value && !(0, _isdate.isDate)(value)) {
        value = startDate;
    }
    return Math.min(Math.max(value.getTime(), startDate.getTime()), endDate.getTime());
};
var handlePickerValueChange = function handlePickerValueChange(selectedOptions, selectedValue, index, type, defaultDate, handleDateComparison) {
    var rangeType = type.toLocaleLowerCase();
    var formattedDate = [];
    selectedValue.forEach(function(item) {
        formattedDate.push(item);
    });
    if (rangeType === 'month-day' && formattedDate.length < 3) {
        formattedDate.unshift(new Date(defaultDate).getFullYear());
    }
    if (rangeType === 'year-month' && formattedDate.length < 3) {
        formattedDate.push(new Date(defaultDate).getDate());
    }
    if ((rangeType === 'time' || rangeType === 'hour-minutes') && formattedDate.length < 6) {
        var defaultDateObj = new Date(defaultDate);
        formattedDate.unshift(defaultDateObj.getFullYear(), defaultDateObj.getMonth() + 1, defaultDateObj.getDate());
        if (rangeType === 'hour-minutes') {
            formattedDate.push(0);
        }
    }
    var year = Number(formattedDate[0]);
    var month = Number(formattedDate[1]) - 1;
    var day = Math.min(Number(formattedDate[2]), getLastDayOfMonth(year, month + 1));
    var date = null;
    if (rangeType === 'date' || rangeType === 'month-day' || rangeType === 'year-month') {
        date = new Date(year, month, day);
    } else if (rangeType === 'datetime') {
        date = new Date(year, month, day, Number(formattedDate[3]), Number(formattedDate[4]));
    } else if (rangeType === 'datehour') {
        date = new Date(year, month, day, Number(formattedDate[3]));
    } else {
        date = new Date(year, month, day, Number(formattedDate[3]), Number(formattedDate[4]), Number(formattedDate[5]));
    }
    handleDateComparison(date, selectedOptions, index);
};
