"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 _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _reactfastcompare = /*#__PURE__*/ _interop_require_default._(require("react-fast-compare"));
var _index = /*#__PURE__*/ _interop_require_default._(require("../pickerview/index"));
var _index1 = /*#__PURE__*/ _interop_require_default._(require("../popup/index"));
var _index2 = /*#__PURE__*/ _interop_require_default._(require("../safearea/index"));
var _userefs = require("../../hooks/use-refs");
var _index3 = require("../configprovider/index");
var _usepropsvalue = require("../../hooks/use-props-value");
var _typings = require("../../utils/typings");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    title: '',
    options: [],
    value: undefined,
    defaultValue: [],
    closeOnOverlayClick: true
});
var InternalPicker = function InternalPicker(props, ref) {
    var locale = (0, _index3.useConfig)().locale;
    var _ref = (0, _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 = (0, _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 = (0, _classnames.default)(classPrefix, className);
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: props.value,
        defaultValue: (0, _to_consumable_array._)(defaultValue),
        finalValue: (0, _to_consumable_array._)(defaultValue),
        onChange: function onChange(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 = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: props.visible,
        defaultValue: false,
        finalValue: false,
        onChange: function onChange(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 open() {
            setInnerVisible(true);
        },
        close: function close() {
            setInnerVisible(false);
        }
    };
    (0, _react.useImperativeHandle)(ref, function() {
        return actions;
    });
    var _useState = (0, _sliced_to_array._)((0, _react.useState)((0, _to_consumable_array._)(selectedValue)), 2), innerValue = _useState[0], setInnerValue = _useState[1];
    var innerValueRef = (0, _react.useRef)(innerValue);
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)([]), 2), innerOptions = _useState1[0], setInnerOptions = _useState1[1];
    var selectedOptionsRef = (0, _react.useRef)([]);
    var _useRefs = (0, _sliced_to_array._)((0, _userefs.useRefs)(), 2), refs = _useRefs[0], setRefs = _useRefs[1];
    (0, _react.useEffect)(function() {
        if (innerVisible) {
            setInnerValue(selectedValue);
            setInnerOptions(options);
        }
    }, [
        selectedValue,
        innerOptions,
        innerVisible
    ]);
    var onChangeItem = function onChangeItem(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 ((0, _reactfastcompare.default)(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 onConfirmEvent() {
        var moving = false;
        refs.forEach(function(ref) {
            if (ref.moving) moving = true;
            ref.stopMomentum();
        });
        if (!moving) {
            setSelectedValue(innerValue, true);
            setInnerVisible(false);
        }
    };
    var onCancelEvent = function onCancelEvent() {
        setInnerValue(selectedValue);
        onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        setInnerVisible(false);
    };
    var renderTitleBar = function renderTitleBar() {
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-control")
        }, /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-cancel-btn"),
            onClick: function onClick(e) {
                e.stopPropagation();
                onCancelEvent();
            }
        }, locale === null || locale === void 0 ? void 0 : locale.cancel), /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-title")
        }, title || ''), /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-confirm-btn"),
            onClick: function onClick(e) {
                e.stopPropagation();
                onConfirmEvent();
            }
        }, locale.confirm));
    };
    var renderPickerElement = function renderPickerElement() {
        return /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({
            className: classes,
            style: style
        }, rest), renderTitleBar(), typeof children !== 'function' && children, /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-panel")
        }, /*#__PURE__*/ _react.default.createElement(_index.default, {
            setRefs: setRefs,
            value: innerValue,
            options: props.options,
            threeDimensional: threeDimensional,
            duration: duration,
            onChange: function onChange(param) {
                var value = param.value, index = param.index, selectedOptions = param.selectedOptions;
                onChangeItem({
                    value: value,
                    index: index,
                    selectedOptions: selectedOptions
                });
            }
        })));
    };
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, typeof children === 'function' && children(selectedValue), /*#__PURE__*/ _react.default.createElement(_index1.default, (0, _object_spread_props._)((0, _object_spread._)({}, popupProps), {
        visible: innerVisible,
        position: "bottom",
        onOverlayClick: function onOverlayClick() {
            if (!closeOnOverlayClick) return;
            onCancelEvent();
        }
    }), innerVisible ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, renderPickerElement(), " ") : null, /*#__PURE__*/ _react.default.createElement(_index2.default, {
        position: "bottom"
    })));
};
var Picker = /*#__PURE__*/ _react.default.forwardRef(InternalPicker);
var _default = Picker;
