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, { useEffect, useImperativeHandle, useRef, useState } from "react";
import { View } from "@tarojs/components";
import classNames from "classnames";
import isEqual from "react-fast-compare";
import PickerView from "../pickerview/index";
import Popup from "../popup/index";
import SafeArea from "../safearea/index";
import { useRefs } from "../../hooks/use-refs";
import { useConfig } from "../configprovider/index";
import { usePropsValue } from "../../hooks/use-props-value";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    title: '',
    options: [],
    value: undefined,
    defaultValue: [],
    closeOnOverlayClick: true
});
var InternalPicker = function(props, ref) {
    var locale = useConfig().locale;
    var _ref = _object_spread({}, defaultProps, props), children = _ref.children, visible = _ref.visible, title = _ref.title, _ref_options = _ref.options, options = _ref_options === void 0 ? [] : _ref_options, closeOnOverlayClick = _ref.closeOnOverlayClick, _ref_popupProps = _ref.popupProps, popupProps = _ref_popupProps === void 0 ? {} : _ref_popupProps, _ref_defaultValue = _ref.defaultValue, defaultValue = _ref_defaultValue === void 0 ? [] : _ref_defaultValue, className = _ref.className, style = _ref.style, threeDimensional = _ref.threeDimensional, duration = _ref.duration, onConfirm = _ref.onConfirm, onCancel = _ref.onCancel, onClose = _ref.onClose, onChange = _ref.onChange, rest = _object_without_properties(_ref, [
        "children",
        "visible",
        "title",
        "options",
        "closeOnOverlayClick",
        "popupProps",
        "defaultValue",
        "className",
        "style",
        "threeDimensional",
        "duration",
        "onConfirm",
        "onCancel",
        "onClose",
        "onChange"
    ]);
    var classPrefix = 'nut-picker';
    var classes = classNames(classPrefix, className);
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: props.value,
        defaultValue: _to_consumable_array(defaultValue),
        finalValue: _to_consumable_array(defaultValue),
        onChange: function(value) {
            var _props_onConfirm;
            (_props_onConfirm = props.onConfirm) === null || _props_onConfirm === void 0 ? void 0 : _props_onConfirm.call(props, selectedOptionsRef.current, value);
        }
    }), 2), selectedValue = _usePropsValue[0], setSelectedValue = _usePropsValue[1];
    var _usePropsValue1 = _sliced_to_array(usePropsValue({
        value: props.visible,
        defaultValue: false,
        finalValue: false,
        onChange: function(v) {
            if (!v) {
                var _props_onClose;
                (_props_onClose = props.onClose) === null || _props_onClose === void 0 ? void 0 : _props_onClose.call(props, selectedOptionsRef.current, innerValue);
            }
        }
    }), 2), innerVisible = _usePropsValue1[0], setInnerVisible = _usePropsValue1[1];
    var actions = {
        open: function() {
            setInnerVisible(true);
        },
        close: function() {
            setInnerVisible(false);
        }
    };
    useImperativeHandle(ref, function() {
        return actions;
    });
    var _useState = _sliced_to_array(useState(_to_consumable_array(selectedValue)), 2), innerValue = _useState[0], setInnerValue = _useState[1];
    var innerValueRef = useRef(innerValue);
    var _useState1 = _sliced_to_array(useState([]), 2), innerOptions = _useState1[0], setInnerOptions = _useState1[1];
    var selectedOptionsRef = useRef([]);
    var _useRefs = _sliced_to_array(useRefs(), 2), refs = _useRefs[0], setRefs = _useRefs[1];
    useEffect(function() {
        if (innerVisible) {
            setInnerValue(selectedValue);
            setInnerOptions(options);
        }
    }, [
        selectedValue,
        innerOptions,
        innerVisible
    ]);
    var onChangeItem = function(param) {
        var value = param.value, index = param.index, selectedOptions = param.selectedOptions;
        if (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.length) {
            selectedOptionsRef.current = selectedOptions;
        }
        if (isEqual(value, innerValueRef.current)) return;
        innerValueRef.current = value;
        setInnerValue(value);
        innerVisible && (onChange === null || onChange === void 0 ? void 0 : onChange({
            selectedOptions: selectedOptions,
            value: value,
            index: index
        }));
    };
    var onConfirmEvent = function() {
        var moving = false;
        refs.forEach(function(ref) {
            if (ref.moving) moving = true;
            ref.stopMomentum();
        });
        if (!moving) {
            setSelectedValue(innerValue, true);
            setInnerVisible(false);
        }
    };
    var onCancelEvent = function() {
        setInnerValue(selectedValue);
        onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        setInnerVisible(false);
    };
    var renderTitleBar = function() {
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-control")
        }, /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-cancel-btn"),
            onClick: function(e) {
                e.stopPropagation();
                onCancelEvent();
            }
        }, locale === null || locale === void 0 ? void 0 : locale.cancel), /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-title")
        }, title || ''), /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-confirm-btn"),
            onClick: function(e) {
                e.stopPropagation();
                onConfirmEvent();
            }
        }, locale.confirm));
    };
    var renderPickerElement = function() {
        return /*#__PURE__*/ React.createElement(View, _object_spread({
            className: classes,
            style: style
        }, rest), renderTitleBar(), typeof children !== 'function' && children, /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-panel")
        }, /*#__PURE__*/ React.createElement(PickerView, {
            setRefs: setRefs,
            value: innerValue,
            options: props.options,
            threeDimensional: threeDimensional,
            duration: duration,
            onChange: function(param) {
                var value = param.value, index = param.index, selectedOptions = param.selectedOptions;
                onChangeItem({
                    value: value,
                    index: index,
                    selectedOptions: selectedOptions
                });
            }
        })));
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, typeof children === 'function' && children(selectedValue), /*#__PURE__*/ React.createElement(Popup, _object_spread_props(_object_spread({}, popupProps), {
        visible: innerVisible,
        position: "bottom",
        onOverlayClick: function() {
            if (!closeOnOverlayClick) return;
            onCancelEvent();
        }
    }), innerVisible ? /*#__PURE__*/ React.createElement(React.Fragment, null, renderPickerElement(), " ") : null, /*#__PURE__*/ React.createElement(SafeArea, {
        position: "bottom"
    })));
};
var Picker = /*#__PURE__*/ React.forwardRef(InternalPicker);
export default Picker;
