import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import React, { useState, useEffect } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import isEqual from "react-fast-compare";
import { padZero } from "../../utils/pad-zero";
import PickerView from "../pickerview/index";
import { useConfig } from "../configprovider";
import { usePropsValue } from "../../hooks/use-props-value";
import { ComponentDefaults } from "../../utils/typings";
import { isDate } from "../../utils/is-date";
import { formatValue, generateDatePickerRanges, generatePickerColumnWithCallback, getDatePartValue, handlePickerValueChange } from "../datepicker/utils";
var currentYear = new Date().getFullYear();
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    type: 'date',
    showChinese: false,
    threeDimensional: true,
    minuteStep: 1,
    startDate: new Date(currentYear - 10, 0, 1),
    endDate: new Date(currentYear + 10, 11, 31)
});
export var DatePickerView = function(props) {
    var _$_object_spread = _object_spread({}, defaultProps, props), startDate = _$_object_spread.startDate, endDate = _$_object_spread.endDate, type = _$_object_spread.type, showChinese = _$_object_spread.showChinese, minuteStep = _$_object_spread.minuteStep, defaultValue = _$_object_spread.defaultValue, formatter = _$_object_spread.formatter, filter = _$_object_spread.filter, onChange = _$_object_spread.onChange, threeDimensional = _$_object_spread.threeDimensional, className = _$_object_spread.className, style = _$_object_spread.style;
    var classPrefix = 'nut-datepickerview';
    var cls = classNames(classPrefix, className);
    var locale = useConfig().locale;
    var lang = locale.datepicker;
    var zhCNType = {
        day: lang.day,
        year: lang.year,
        month: lang.month,
        hour: lang.hour,
        minute: lang.min,
        seconds: lang.seconds
    };
    var _useState = _sliced_to_array(useState([]), 2), pickerValue = _useState[0], setPickerValue = _useState[1];
    var _useState1 = _sliced_to_array(useState([]), 2), pickerOptions = _useState1[0], setPickerOptions = _useState1[1];
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: props.value && formatValue(props.value, startDate, endDate),
        defaultValue: defaultValue && formatValue(defaultValue, startDate, endDate),
        finalValue: 0
    }), 2), selectedDate = _usePropsValue[0], setSelectedDate = _usePropsValue[1];
    var handleDateComparison = function(newDate, selectedOptions, index) {
        if (newDate && isDate(newDate)) {
            var _this;
            if (!isEqual((_this = new Date(selectedDate)) === null || _this === void 0 ? void 0 : _this.getTime(), newDate === null || newDate === void 0 ? void 0 : newDate.getTime())) {
                setSelectedDate(formatValue(newDate, startDate, endDate));
                onChange === null || onChange === void 0 ? void 0 : onChange(selectedOptions, [
                    String(newDate.getFullYear()),
                    padZero(newDate.getMonth() + 1),
                    padZero(newDate.getDate())
                ], index);
            }
        }
    };
    var handleChange = function(selectedOptions, selectedValue, index) {
        handlePickerValueChange(selectedOptions, selectedValue, index, type, defaultValue || startDate || endDate, handleDateComparison);
    };
    var generatePickerColumns = function() {
        var dateRanges = generateDatePickerRanges(type, selectedDate, startDate, endDate);
        var columns = dateRanges.map(function(rangeConfig, columnIndex) {
            var columnType = rangeConfig.type, range = rangeConfig.range;
            var selectedValue = getDatePartValue(columnType, selectedDate);
            var pickerColumn = generatePickerColumnWithCallback(range[0], range[1], selectedValue, columnType, minuteStep, function(selectedIndex, options) {
                var _options_selectedIndex;
                pickerValue[columnIndex] = (_options_selectedIndex = options[selectedIndex]) === null || _options_selectedIndex === void 0 ? void 0 : _options_selectedIndex.value;
                setPickerValue(_to_consumable_array(pickerValue));
            }, showChinese, zhCNType, formatter);
            if (filter === null || filter === void 0 ? void 0 : filter(columnType, pickerColumn)) {
                return filter(columnType, pickerColumn);
            }
            return pickerColumn;
        });
        return columns || [];
    };
    useEffect(function() {
        var _this, _this1;
        if (!isEqual((_this = new Date(selectedDate)) === null || _this === void 0 ? void 0 : _this.getTime(), (_this1 = new Date(selectedDate)) === null || _this1 === void 0 ? void 0 : _this1.getTime())) {
            setSelectedDate(selectedDate);
        }
    }, [
        selectedDate
    ]);
    useEffect(function() {
        setPickerOptions(generatePickerColumns());
    }, [
        selectedDate,
        startDate,
        endDate
    ]);
    return /*#__PURE__*/ React.createElement(View, {
        className: cls,
        style: style
    }, pickerOptions.length && /*#__PURE__*/ React.createElement(PickerView, {
        value: pickerValue,
        options: pickerOptions,
        onChange: function(param) {
            var selectedOptions = param.selectedOptions, value = param.value, index = param.index;
            handleChange(selectedOptions, value, index);
        },
        threeDimensional: threeDimensional
    }));
};
DatePickerView.displayName = 'NutDatePickerView';
