import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { padZero } from "../../utils/pad-zero";
import { isDate } from "../../utils/is-date";
/**
 * 获取指定年份和月份的最后一天
 * @param year - 年份
 * @param month - 月份（1 到 12）
 * @returns 返回该月份的最后一天
 */ export function getLastDayOfMonth(year, month) {
    return new Date(year, month, 0).getDate();
}
/**
 * 根据类型和日期值，计算并返回日期边界值（年、月、日、时、分、秒）
 * @param type 边界类型：'min' 表示最小值，'max' 表示最大值
 * @param value 传入的日期值
 * @param startDate 传入的开始时间
 * @param endDate 传入的结束时间
 * @returns 返回包含边界值的对象
 */ export var calculateDateBoundary = function(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 = {}, _define_property(_obj, "".concat(type, "Year"), year), _define_property(_obj, "".concat(type, "Month"), month), _define_property(_obj, "".concat(type, "Date"), date), _define_property(_obj, "".concat(type, "Hour"), hour), _define_property(_obj, "".concat(type, "Minute"), minute), _define_property(_obj, "".concat(type, "Seconds"), minute), _obj;
};
/**
 * 根据选中的日期和类型，生成日期选择器的范围配置
 * @returns {Array} 返回日期选择器的范围配置数组
 */ export var generateDatePickerRanges = function(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;
    }
};
/**
 * 根据类型获取日期对象中对应的值
 * @param type 需要获取的日期部分（如 'year', 'month', 'day' 等）
 * @param selectedDate 选中的日期时间戳
 * @returns 返回日期对象中对应部分的值，如果类型无效或日期无效，返回 0
 */ export var getDatePartValue = function(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;
    }
};
/**
 * 生成 Picker 的列数据，并触发回调函数返回选中索引
 * @param min 最小值
 * @param max 最大值
 * @param currentValue 当前选中的值
 * @param type 列的类型（如 'year', 'month', 'minute' 等）
 * @param minuteStep 分钟步长（仅当类型为 'minute' 时生效）
 * @param callback 回调函数，用于返回选中索引
 * @returns 返回生成的 Picker 列数据
 */ export var generatePickerColumnWithCallback = function(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;
};
/**
 * 格式化 Picker 选项
 * @param type 选项类型（如 'year', 'month', 'minute' 等）
 * @param value 选项的值
 * @param showChinese 是否显示中文文本
 * @param zhCNType 中文文本映射对象
 * @param formatter 自定义格式化函数
 * @returns 返回格式化后的 Picker 选项
 */ export var formatPickerOption = function(type, value, showChinese, zhCNType, formatter) {
    if (formatter) {
        return formatter(type, {
            label: padZero(value, 2),
            value: padZero(value, 2)
        });
    }
    var paddedValue = padZero(value, 2);
    var chineseText = showChinese ? zhCNType[type] : '';
    return {
        label: paddedValue + chineseText,
        value: paddedValue
    };
};
/**
 * 格式化日期值，确保其在 startDate 和 endDate 之间
 */ export var formatValue = function(value, startDate, endDate) {
    if (!value || value && !isDate(value)) {
        value = startDate;
    }
    return Math.min(Math.max(value.getTime(), startDate.getTime()), endDate.getTime());
};
/**
 * 处理 Picker 值变化的逻辑
 * @param selectedOptions 选中的选项数组
 * @param selectedValue 选中的值数组
 * @param index 当前列的索引
 */ export var handlePickerValueChange = function(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);
};
