import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
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 } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import Popup from "../popup/index";
import TimeDetail from "../timedetail/index";
import { ComponentDefaults } from "../../utils/typings";
import { useConfig } from "../configprovider/index";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    visible: false,
    multiple: false,
    defaultValue: [],
    options: [],
    optionKey: {
        valueKey: 'value',
        textKey: 'text',
        childrenKey: 'children'
    }
});
export var TimeSelect = function(props) {
    var locale = useConfig().locale;
    var _ref = _object_spread({}, defaultProps, props), visible = _ref.visible, className = _ref.className, style = _ref.style, title = _ref.title, defaultValue = _ref.defaultValue, options = _ref.options, optionKey = _ref.optionKey, multiple = _ref.multiple, onSelect = _ref.onSelect, onDateChange = _ref.onDateChange, onTimeChange = _ref.onTimeChange, rest = _object_without_properties(_ref, [
        "visible",
        "className",
        "style",
        "title",
        "defaultValue",
        "options",
        "optionKey",
        "multiple",
        "onSelect",
        "onDateChange",
        "onTimeChange"
    ]);
    var _useState = _sliced_to_array(useState(function() {
        // 设置 defaultValue 时，默认展示选中的第一个 date
        if (defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.length) {
            return defaultValue[0][optionKey.valueKey];
        }
        // 否则展示 options 中第一个 date
        if (options === null || options === void 0 ? void 0 : options.length) {
            return options[0][optionKey.valueKey];
        }
        return '';
    }), 2), activeDate = _useState[0], setActiveDate = _useState[1];
    var _useState1 = _sliced_to_array(useState(function() {
        return defaultValue || [];
    }), 2), activeTime = _useState1[0], setActiveTime = _useState1[1];
    var classPrefix = 'nut-timeselect';
    var closeFun = function() {
        onSelect && onSelect(activeTime);
    };
    var handleSelectTime = function(selectTime) {
        var newActiveTime = _to_consumable_array(activeTime);
        var date = newActiveTime.find(function(item) {
            return item[optionKey.valueKey] === activeDate;
        });
        if (date) {
            var timeIndex = date[optionKey.childrenKey].findIndex(function(time) {
                return time[optionKey.valueKey] === selectTime[optionKey.valueKey];
            });
            if (timeIndex > -1) {
                if (multiple) {
                    date[optionKey.childrenKey].splice(timeIndex, 1);
                } else {
                    newActiveTime = [];
                }
            } else if (multiple) {
                date[optionKey.childrenKey].push(_object_spread({}, selectTime));
            } else {
                var _obj;
                newActiveTime = [
                    (_obj = {}, _define_property(_obj, optionKey.valueKey, activeDate), _define_property(_obj, optionKey.childrenKey, [
                        _object_spread({}, selectTime)
                    ]), _obj)
                ];
            }
        } else if (multiple) {
            var _obj1;
            newActiveTime.push((_obj1 = {}, _define_property(_obj1, optionKey.valueKey, activeDate), _define_property(_obj1, optionKey.childrenKey, [
                _object_spread({}, selectTime)
            ]), _obj1));
        } else {
            var _obj2;
            newActiveTime = [
                (_obj2 = {}, _define_property(_obj2, optionKey.valueKey, activeDate), _define_property(_obj2, optionKey.childrenKey, [
                    _object_spread({}, selectTime)
                ]), _obj2)
            ];
        }
        newActiveTime = newActiveTime.filter(function(item) {
            var _item_optionKey_childrenKey;
            return ((_item_optionKey_childrenKey = item[optionKey.childrenKey]) === null || _item_optionKey_childrenKey === void 0 ? void 0 : _item_optionKey_childrenKey.length) > 0;
        });
        setActiveTime(newActiveTime);
        onTimeChange && onTimeChange(selectTime, newActiveTime);
    };
    var handleChange = function(date) {
        setActiveDate(date[optionKey.valueKey]);
        onDateChange && onDateChange(date, activeTime);
    };
    return /*#__PURE__*/ React.createElement(Popup, _object_spread({
        closeable: true,
        round: true,
        visible: visible,
        position: "bottom",
        title: title || locale.timeselect.pickupTime,
        style: _object_spread({}, style),
        onClose: closeFun
    }, rest), /*#__PURE__*/ React.createElement(View, {
        className: classNames(classPrefix, className)
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-content")
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-content-left")
    }, options.map(function(item) {
        return /*#__PURE__*/ React.createElement(View, {
            key: item[optionKey.valueKey],
            className: classNames('nut-timepannel', {
                active: item[optionKey.valueKey] === activeDate
            }),
            onClick: function() {
                return handleChange(item);
            }
        }, item[optionKey.textKey]);
    })), /*#__PURE__*/ React.createElement(TimeDetail, {
        options: options,
        optionKey: optionKey,
        activeDate: activeDate,
        activeTime: activeTime,
        onSelect: handleSelectTime
    }))));
};
TimeSelect.displayName = 'NutTimeSelect';
