"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "default", {
    enumerable: true,
    get: function() {
        return _default;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _to_consumable_array = require("@swc/helpers/_/_to_consumable_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _reactfastcompare = /*#__PURE__*/ _interop_require_default._(require("react-fast-compare"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _index = /*#__PURE__*/ _interop_require_default._(require("../picker/index"));
var _index1 = require("../configprovider/index");
var _usepropsvalue = require("../../hooks/use-props-value");
var _typings = require("../../utils/typings");
var _isdate = require("../../utils/is-date");
var _utils = require("./utils");
var currentYear = new Date().getFullYear();
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    visible: false,
    title: '',
    type: 'date',
    showChinese: false,
    threeDimensional: true,
    minuteStep: 1,
    startDate: new Date(currentYear - 10, 0, 1),
    endDate: new Date(currentYear + 10, 11, 31)
});
var InternalPicker = function InternalPicker(props, ref) {
    var _ref = (0, _object_spread._)({}, defaultProps, props), startDate = _ref.startDate, endDate = _ref.endDate, type = _ref.type, showChinese = _ref.showChinese, minuteStep = _ref.minuteStep, visible = _ref.visible, title = _ref.title, defaultValue = _ref.defaultValue, _ref_pickerProps = _ref.pickerProps, pickerProps = _ref_pickerProps === void 0 ? {} : _ref_pickerProps, formatter = _ref.formatter, onClose = _ref.onClose, onCancel = _ref.onCancel, onConfirm = _ref.onConfirm, filter = _ref.filter, onChange = _ref.onChange, threeDimensional = _ref.threeDimensional, className = _ref.className, style = _ref.style, children = _ref.children, rest = (0, _object_without_properties._)(_ref, [
        "startDate",
        "endDate",
        "type",
        "showChinese",
        "minuteStep",
        "visible",
        "title",
        "defaultValue",
        "pickerProps",
        "formatter",
        "onClose",
        "onCancel",
        "onConfirm",
        "filter",
        "onChange",
        "threeDimensional",
        "className",
        "style",
        "children"
    ]);
    var locale = (0, _index1.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 classPrefix = 'nut-datepicker';
    var cls = (0, _classnames.default)(classPrefix, className);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)([]), 2), pickerValue = _useState[0], setPickerValue = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)([]), 2), pickerOptions = _useState1[0], setPickerOptions = _useState1[1];
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: props.value && (0, _utils.formatValue)(props.value, startDate, endDate),
        defaultValue: props.defaultValue && (0, _utils.formatValue)(props.defaultValue, startDate, endDate),
        finalValue: 0
    }), 2), selectedDate = _usePropsValue[0], setSelectedDate = _usePropsValue[1];
    var _useState2 = (0, _sliced_to_array._)((0, _react.useState)(selectedDate), 2), innerDate = _useState2[0], setInnerDate = _useState2[1];
    var _usePropsValue1 = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: props.visible,
        defaultValue: false,
        finalValue: false
    }), 2), innerVisible = _usePropsValue1[0], setInnerVisible = _usePropsValue1[1];
    var actions = {
        open: function open() {
            setInnerVisible(true);
        },
        close: function close() {
            setInnerVisible(false);
        }
    };
    (0, _react.useImperativeHandle)(ref, function() {
        return actions;
    });
    var handleDateComparison = function handleDateComparison(newDate, selectedOptions, index) {
        if (newDate && (0, _isdate.isDate)(newDate)) {
            var _this;
            if (!(0, _reactfastcompare.default)((_this = new Date(selectedDate)) === null || _this === void 0 ? void 0 : _this.getTime(), newDate === null || newDate === void 0 ? void 0 : newDate.getTime())) {
                setInnerDate((0, _utils.formatValue)(newDate, startDate, endDate));
            }
            onChange === null || onChange === void 0 ? void 0 : onChange(selectedOptions, selectedOptions.map(function(val) {
                return val.value;
            }), index);
        }
    };
    var handleConfirmDateComparison = function handleConfirmDateComparison(newDate) {
        if (newDate && (0, _isdate.isDate)(newDate)) {
            var _this;
            if (!(0, _reactfastcompare.default)((_this = new Date(selectedDate)) === null || _this === void 0 ? void 0 : _this.getTime(), newDate === null || newDate === void 0 ? void 0 : newDate.getTime())) {
                setSelectedDate((0, _utils.formatValue)(newDate, startDate, endDate));
            }
        }
    };
    var handleCancel = function handleCancel() {
        setInnerDate(selectedDate);
        onCancel === null || onCancel === void 0 ? void 0 : onCancel();
    };
    var handleClose = function handleClose() {
        setInnerVisible(false);
        onClose === null || onClose === void 0 ? void 0 : onClose();
    };
    var handleConfirm = function handleConfirm(options, value) {
        (0, _utils.handlePickerValueChange)(options, value, 0, type, defaultValue || startDate || endDate, handleConfirmDateComparison);
        onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(options, value);
    };
    var handleChange = function handleChange(selectedOptions, selectedValue, index) {
        innerVisible && (0, _utils.handlePickerValueChange)(selectedOptions, selectedValue, index, type, defaultValue || startDate || endDate, handleDateComparison);
    };
    var generatePickerColumns = function generatePickerColumns() {
        var dateRanges = (0, _utils.generateDatePickerRanges)(type, innerDate, startDate, endDate);
        var columns = dateRanges.map(function(rangeConfig, columnIndex) {
            var columnType = rangeConfig.type, range = rangeConfig.range;
            var selectedValue = (0, _utils.getDatePartValue)(columnType, innerDate);
            var pickerColumn = (0, _utils.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((0, _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 || [];
    };
    (0, _react.useEffect)(function() {
        setInnerDate(selectedDate);
    }, [
        selectedDate
    ]);
    (0, _react.useEffect)(function() {
        setPickerOptions(generatePickerColumns());
    }, [
        innerDate,
        startDate,
        endDate
    ]);
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, typeof children === 'function' && children(selectedDate), /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({
        className: cls,
        style: style
    }, rest), pickerOptions.length && /*#__PURE__*/ _react.default.createElement(_index.default, (0, _object_spread_props._)((0, _object_spread._)({}, pickerProps), {
        title: title,
        visible: innerVisible,
        value: pickerValue,
        options: pickerOptions,
        onClose: handleClose,
        onCancel: handleCancel,
        onConfirm: handleConfirm,
        onChange: function onChange(param) {
            var value = param.value, index = param.index, selectedOptions = param.selectedOptions;
            handleChange(selectedOptions, value, index);
        },
        threeDimensional: threeDimensional
    }))));
};
var DatePicker = /*#__PURE__*/ _react.default.forwardRef(InternalPicker);
var _default = DatePicker;
