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 React, { useImperativeHandle } from "react";
import { View } from "@tarojs/components";
import Taro from "@tarojs/taro";
import classNames from "classnames";
import { Close } from "@nutui/icons-react-taro";
import Popup from "../popup/index";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
import Badge from "../badge/index";
import { useConfig } from "../configprovider";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    options: [],
    visible: false,
    closeOnOverlayClick: true,
    safeAreaInsetTop: true
});
var InternalQuickEnter = function(props, ref) {
    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, className = _ref.className, style = _ref.style, onClose = _ref.onClose, onChange = _ref.onChange, closeIcon = _ref.closeIcon, safeAreaInsetTop = _ref.safeAreaInsetTop, rest = _object_without_properties(_ref, [
        "children",
        "visible",
        "title",
        "options",
        "closeOnOverlayClick",
        "popupProps",
        "className",
        "style",
        "onClose",
        "onChange",
        "closeIcon",
        "safeAreaInsetTop"
    ]);
    var locale = useConfig().locale;
    var mergedTitle = title || locale.quickenter.title;
    var classPrefix = 'nut-quickenter';
    var classes = classNames(classPrefix, className, {
        'safe-area-top': safeAreaInsetTop
    });
    var _usePropsValue = _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);
            }
        }
    }), 2), innerVisible = _usePropsValue[0], setInnerVisible = _usePropsValue[1];
    var actions = {
        open: function() {
            setInnerVisible(true);
        },
        close: function() {
            setInnerVisible(false);
        }
    };
    useImperativeHandle(ref, function() {
        return actions;
    });
    var onCancelEvent = function() {
        setInnerVisible(false);
    };
    var handleItemClick = function(item, index) {
        if (item.url) {
            Taro.navigateTo({
                url: item.url
            });
        }
        onChange === null || onChange === void 0 ? void 0 : onChange(item, index);
    };
    var renderTitleBar = function() {
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-control")
        }, /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-title")
        }, mergedTitle), /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-close-btn"),
            onClick: function(e) {
                e.stopPropagation();
                onCancelEvent();
            }
        }, closeIcon || /*#__PURE__*/ React.createElement(Close, {
            ariaLabel: locale.close
        })));
    };
    var renderContent = function() {
        return /*#__PURE__*/ React.createElement(View, {
            className: classNames("".concat(classPrefix, "-content"), _define_property({}, "".concat(classPrefix, "-content-scrollable"), options.length > 8))
        }, /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-list")
        }, options.map(function(item, index) {
            var _item_badgeProps, _item_badgeProps1;
            var _item_badge, _item_badge1;
            return /*#__PURE__*/ React.createElement(View, {
                key: index,
                className: "".concat(classPrefix, "-item"),
                ariaLabel: "".concat(item.title, " ").concat((_item_badge = item.badge) !== null && _item_badge !== void 0 ? _item_badge : (_item_badgeProps = item.badgeProps) === null || _item_badgeProps === void 0 ? void 0 : _item_badgeProps.value),
                onClick: function() {
                    return handleItemClick(item, index);
                }
            }, /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-item-icon"),
                ariaHidden: true
            }, /*#__PURE__*/ React.createElement(Badge, _object_spread_props(_object_spread({}, item.badgeProps), {
                value: (_item_badge1 = item.badge) !== null && _item_badge1 !== void 0 ? _item_badge1 : (_item_badgeProps1 = item.badgeProps) === null || _item_badgeProps1 === void 0 ? void 0 : _item_badgeProps1.value
            }), item.icon)), /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-item-title"),
                ariaHidden: true
            }, item.title));
        })));
    };
    var renderQuickEnterElement = function() {
        return /*#__PURE__*/ React.createElement(View, _object_spread({
            className: classes,
            style: style
        }, rest), renderTitleBar(), children, renderContent());
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Popup, _object_spread_props(_object_spread({
        style: {
            background: 'transparent'
        }
    }, popupProps), {
        overlayStyle: {
            background: 'transparent'
        },
        visible: innerVisible,
        position: "top",
        onOverlayClick: function() {
            if (!closeOnOverlayClick) return;
            onCancelEvent();
        },
        onClose: onCancelEvent
    }), renderQuickEnterElement()));
};
var QuickEnter = /*#__PURE__*/ React.forwardRef(InternalQuickEnter);
export default QuickEnter;
