import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
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 _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useEffect, useRef, useState } from "react";
import classNames from "classnames";
import { nextTick } from "@tarojs/taro";
import { Text, View } from "@tarojs/components";
import { ArrowRadius } from "@nutui/icons-react-taro";
import Popup from "../popup/index";
import { getRectInMultiPlatform } from "../../utils/taro/get-rect";
import { ComponentDefaults } from "../../utils/typings";
import { useRtl } from "../configprovider/index";
import { pxTransform } from "../../utils/taro/px-transform";
import { useUuid } from "../../hooks/use-uuid";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    list: [],
    theme: 'light',
    location: 'bottom',
    visible: false,
    offset: [
        0,
        8
    ],
    arrowOffset: 0,
    targetId: '',
    showArrow: true,
    closeOnOutsideClick: true,
    closeOnActionClick: true,
    overlay: false,
    onClick: function() {},
    onOpen: function() {},
    onClose: function() {}
});
var classPrefix = "nut-popover";
export var Popover = function(props) {
    var rtl = useRtl();
    var _ref = _object_spread({}, defaultProps, props), children = _ref.children, list = _ref.list, theme = _ref.theme, location = _ref.location, visible = _ref.visible, offset = _ref.offset, arrowOffset = _ref.arrowOffset, targetId = _ref.targetId, overlay = _ref.overlay, closeOnOutsideClick = _ref.closeOnOutsideClick, closeOnActionClick = _ref.closeOnActionClick, className = _ref.className, showArrow = _ref.showArrow, style = _ref.style, onClick = _ref.onClick, onOpen = _ref.onOpen, onClose = _ref.onClose, onSelect = _ref.onSelect, rest = _object_without_properties(_ref, [
        "children",
        "list",
        "theme",
        "location",
        "visible",
        "offset",
        "arrowOffset",
        "targetId",
        "overlay",
        "closeOnOutsideClick",
        "closeOnActionClick",
        "className",
        "showArrow",
        "style",
        "onClick",
        "onOpen",
        "onClose",
        "onSelect"
    ]);
    var popoverRef = useRef(null);
    var popoverContentRef = useRef(null);
    var _useState = _sliced_to_array(useState(false), 2), showPopup = _useState[0], setShowPopup = _useState[1];
    var _useState1 = _sliced_to_array(useState(0), 2), popWidth = _useState1[0], setPopWidth = _useState1[1];
    var _useState2 = _sliced_to_array(useState(0), 2), popHeight = _useState2[0], setPopHeight = _useState2[1];
    var _useState3 = _sliced_to_array(useState(), 2), wrapperPosition = _useState3[0], setWrapperPosition = _useState3[1];
    useEffect(function() {
        setShowPopup(visible);
        if (visible) {
            getWrapperPosition();
        }
    }, [
        visible
    ]);
    var uid = useUuid();
    var popoverId = "popover-".concat(uid);
    var getWrapperPosition = function() {
        return _async_to_generator(function() {
            return _ts_generator(this, function(_state) {
                nextTick(function() {
                    return _async_to_generator(function() {
                        var rect, _tmp, width, height, right, left, top;
                        return _ts_generator(this, function(_state) {
                            switch(_state.label){
                                case 0:
                                    if (!targetId) return [
                                        3,
                                        2
                                    ];
                                    return [
                                        4,
                                        getRectInMultiPlatform(document.querySelector("#".concat(targetId)), targetId)
                                    ];
                                case 1:
                                    _tmp = _state.sent();
                                    return [
                                        3,
                                        4
                                    ];
                                case 2:
                                    return [
                                        4,
                                        getRectInMultiPlatform(popoverRef.current, popoverId)
                                    ];
                                case 3:
                                    _tmp = _state.sent();
                                    _state.label = 4;
                                case 4:
                                    rect = _tmp;
                                    width = rect.width, height = rect.height, right = rect.right, left = rect.left, top = rect.top;
                                    setWrapperPosition({
                                        width: width,
                                        height: height,
                                        left: rtl ? right : left,
                                        top: top,
                                        right: rtl ? left : right
                                    });
                                    getPopoverContentW();
                                    return [
                                        2
                                    ];
                            }
                        });
                    })();
                });
                return [
                    2
                ];
            });
        })();
    };
    var getPopoverContentW = function() {
        return _async_to_generator(function() {
            return _ts_generator(this, function(_state) {
                nextTick(function() {
                    return _async_to_generator(function() {
                        var rectContent;
                        return _ts_generator(this, function(_state) {
                            switch(_state.label){
                                case 0:
                                    return [
                                        4,
                                        getRectInMultiPlatform(popoverContentRef.current)
                                    ];
                                case 1:
                                    rectContent = _state.sent();
                                    setPopWidth(rectContent.width);
                                    setPopHeight(rectContent.height);
                                    return [
                                        2
                                    ];
                            }
                        });
                    })();
                });
                return [
                    2
                ];
            });
        })();
    };
    var clickAway = function() {
        if (closeOnOutsideClick) {
            onClick === null || onClick === void 0 ? void 0 : onClick();
            onClose === null || onClose === void 0 ? void 0 : onClose();
        }
    };
    var classes = classNames(classPrefix, _define_property({}, "".concat(classPrefix, "-").concat(theme), theme === 'dark'), className);
    var popoverArrow = function() {
        var prefixCls = 'nut-popover-arrow';
        var direction = location.split('-')[0];
        return "".concat(prefixCls, " ").concat(prefixCls, "-").concat(direction, " ").concat(prefixCls, "-").concat(location);
    };
    var getPopoverPosition = function() {
        var styles = {};
        if (!wrapperPosition) {
            styles.visibility = 'hidden';
            return styles;
        }
        var width = wrapperPosition.width, height = wrapperPosition.height, left = wrapperPosition.left, top = wrapperPosition.top, right = wrapperPosition.right;
        var direction = location.split('-')[0];
        var skew = location.split('-')[1];
        var cross = 0;
        var parallel = 0;
        if (Array.isArray(offset) && offset.length === 2) {
            var rtloffset = rtl ? -offset[0] : offset[0];
            cross += +offset[1];
            parallel += +rtloffset;
        }
        if (width) {
            var dir = rtl ? 'right' : 'left';
            if ([
                'bottom',
                'top'
            ].includes(direction)) {
                var h = direction === 'bottom' ? height + cross : -(popHeight + cross);
                styles.top = pxTransform(top + h);
                if (!skew) {
                    styles[dir] = pxTransform(-(popWidth - width) / 2 + wrapperPosition[dir] + parallel);
                }
                if (skew === 'left') {
                    styles.left = pxTransform(left + parallel);
                }
                if (skew === 'right') {
                    styles.left = pxTransform(right + parallel);
                }
            }
            if ([
                'left',
                'right'
            ].includes(direction)) {
                var contentW = direction === 'left' ? -(popWidth + cross) : width + cross;
                styles.left = pxTransform(left + contentW);
                if (!skew) {
                    styles.top = pxTransform(top - popHeight / 2 + height / 2 - 4 + parallel);
                }
                if (skew === 'top') {
                    styles.top = pxTransform(top + parallel);
                }
                if (skew === 'bottom') {
                    styles.top = pxTransform(top + height + parallel);
                }
            }
        }
        styles.visibility = popWidth === 0 ? 'hidden' : 'initial';
        return styles;
    };
    var popoverArrowStyle = function() {
        var styles = {};
        var direction = location.split('-')[0];
        var skew = location.split('-')[1];
        var base = 16;
        if (arrowOffset !== 0) {
            var dir = rtl ? 'right' : 'left';
            var dir2 = rtl ? 'left' : 'right';
            if ([
                'bottom',
                'top'
            ].includes(direction)) {
                if (!skew) {
                    styles[dir] = "calc(50% + ".concat(arrowOffset, "px)");
                }
                if (skew === 'left') {
                    styles[dir] = "".concat(base + arrowOffset, "px");
                }
                if (skew === 'right') {
                    styles[dir2] = "".concat(base - arrowOffset, "px");
                }
            }
            if ([
                'left',
                'right'
            ].includes(direction)) {
                if (!skew) {
                    styles.top = "calc(50% - ".concat(arrowOffset, "px)");
                }
                if (skew === 'top') {
                    styles.top = "".concat(base - arrowOffset, "px");
                }
                if (skew === 'bottom') {
                    styles.bottom = "".concat(base + arrowOffset, "px");
                }
            }
        }
        return styles;
    };
    var handleSelect = function(item, index) {
        if (!item.disabled) {
            onSelect === null || onSelect === void 0 ? void 0 : onSelect(item, index);
        }
        if (closeOnActionClick) {
            onClick === null || onClick === void 0 ? void 0 : onClick();
            onClose === null || onClose === void 0 ? void 0 : onClose();
        }
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, !targetId && /*#__PURE__*/ React.createElement(View, {
        className: "nut-popover-wrapper",
        ref: popoverRef,
        id: popoverId,
        onClick: function() {
            var _props_onClick;
            props === null || props === void 0 ? void 0 : (_props_onClick = props.onClick) === null || _props_onClick === void 0 ? void 0 : _props_onClick.call(props);
            if (!visible) {
                onOpen === null || onOpen === void 0 ? void 0 : onOpen();
            } else {
                onClose === null || onClose === void 0 ? void 0 : onClose();
            }
        },
        style: style
    }, Array.isArray(children) ? children[0] : children), /*#__PURE__*/ React.createElement(View, {
        className: classes,
        style: _object_spread({}, getPopoverPosition(), style)
    }, /*#__PURE__*/ React.createElement(Popup, _object_spread({
        className: "nut-popover-content nut-popover-content-".concat(location),
        position: "none",
        overlay: overlay,
        visible: showPopup
    }, rest), /*#__PURE__*/ React.createElement(View, {
        className: "nut-popover-content-group",
        ref: popoverContentRef
    }, showArrow && /*#__PURE__*/ React.createElement(View, {
        className: popoverArrow(),
        style: popoverArrowStyle()
    }, /*#__PURE__*/ React.createElement(ArrowRadius, null)), Array.isArray(children) ? children[1] : null, list.map(function(item, index) {
        var _item_action;
        return /*#__PURE__*/ React.createElement(View, {
            className: classNames({
                'nut-popover-item': true,
                'nut-popover-item-disabled': item.disabled
            }, item.className),
            key: item.key || index,
            onClick: function() {
                return handleSelect(item, index);
            }
        }, item.icon && /*#__PURE__*/ React.createElement(View, {
            className: "nut-popover-item-icon"
        }, item.icon), /*#__PURE__*/ React.createElement(Text, {
            className: "nut-popover-item-name"
        }, item.name), ((_item_action = item.action) === null || _item_action === void 0 ? void 0 : _item_action.icon) && /*#__PURE__*/ React.createElement(View, {
            className: "nut-popover-item-action-icon",
            onClick: function(e) {
                var _item_action_onClick, _item_action;
                return (_item_action = item.action) === null || _item_action === void 0 ? void 0 : (_item_action_onClick = _item_action.onClick) === null || _item_action_onClick === void 0 ? void 0 : _item_action_onClick.call(_item_action, e);
            }
        }, item.action.icon));
    }))), showPopup && closeOnOutsideClick && /*#__PURE__*/ React.createElement(View, {
        className: "nut-popover-content-bg",
        onClick: clickAway,
        onTouchMove: clickAway
    })));
};
Popover.displayName = 'NutPopover';
