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