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 _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useState, useEffect, useRef } from "react";
import { nextTick } from "@tarojs/taro";
import { createPortal } from "react-dom";
import { CSSTransition } from "react-transition-group";
import classNames from "classnames";
import { Close } from "@nutui/icons-react-taro";
import { View } from "@tarojs/components";
import { getRectInMultiPlatformWithoutCache } from "../../utils/taro/get-rect";
import { defaultOverlayProps } from "../overlay/overlay";
import Overlay from "../overlay/index";
import { useLockScrollTaro } from "../../hooks/taro/use-lock-scoll";
import { harmony } from "../../utils/taro/platform";
import { pxTransform } from "../../utils/taro/px-transform";
import { useConfig } from "../configprovider/configprovider";
var defaultProps = _object_spread_props(_object_spread({}, defaultOverlayProps), {
    position: 'center',
    transition: '',
    overlayStyle: {},
    overlayClassName: '',
    closeable: false,
    closeIconPosition: 'top-right',
    closeIcon: 'close',
    destroyOnClose: false,
    portal: null,
    overlay: true,
    round: false,
    resizable: false,
    minHeight: '',
    onOpen: function() {},
    onClose: function() {},
    onOverlayClick: function() {
        return true;
    },
    onCloseIconClick: function() {
        return true;
    },
    onTouchStart: function() {},
    onTouchMove: function() {},
    onTouchEnd: function() {}
});
// 默认1000，参看variables
var _zIndex = 1100;
export var Popup = function(props) {
    var _$_object_spread = _object_spread({}, defaultProps, props), children = _$_object_spread.children, visible = _$_object_spread.visible, overlay = _$_object_spread.overlay, closeOnOverlayClick = _$_object_spread.closeOnOverlayClick, overlayStyle = _$_object_spread.overlayStyle, overlayClassName = _$_object_spread.overlayClassName, zIndex = _$_object_spread.zIndex, lockScroll = _$_object_spread.lockScroll, duration = _$_object_spread.duration, closeable = _$_object_spread.closeable, closeIconPosition = _$_object_spread.closeIconPosition, closeIcon = _$_object_spread.closeIcon, left = _$_object_spread.left, title = _$_object_spread.title, top = _$_object_spread.top, description = _$_object_spread.description, style = _$_object_spread.style, transition = _$_object_spread.transition, round = _$_object_spread.round, position = _$_object_spread.position, className = _$_object_spread.className, destroyOnClose = _$_object_spread.destroyOnClose, portal = _$_object_spread.portal, resizable = _$_object_spread.resizable, minHeight = _$_object_spread.minHeight, onOpen = _$_object_spread.onOpen, onClose = _$_object_spread.onClose, onOverlayClick = _$_object_spread.onOverlayClick, onCloseIconClick = _$_object_spread.onCloseIconClick, afterShow = _$_object_spread.afterShow, afterClose = _$_object_spread.afterClose, onClick = _$_object_spread.onClick, onTouchStart = _$_object_spread.onTouchStart, onTouchMove = _$_object_spread.onTouchMove, onTouchEnd = _$_object_spread.onTouchEnd, closeAriaLabel = _$_object_spread.closeAriaLabel;
    var innerIndex = zIndex || _zIndex;
    var _useState = _sliced_to_array(useState(innerIndex), 2), index = _useState[0], setIndex = _useState[1];
    var _useState1 = _sliced_to_array(useState(visible), 2), innerVisible = _useState1[0], setInnerVisible = _useState1[1];
    var _useState2 = _sliced_to_array(useState(true), 2), showChildren = _useState2[0], setShowChildren = _useState2[1];
    var _useState3 = _sliced_to_array(useState(''), 2), transitionName = _useState3[0], setTransitionName = _useState3[1];
    var nodeRef = useLockScrollTaro(innerVisible && lockScroll);
    var topNodeRef = React.useRef(null);
    var rootRect = useRef(null);
    var touchStartRef = useRef(0);
    var touchMoveDistanceRef = useRef(0);
    var heightRef = useRef(0);
    var defaultHeightRef = useRef(0);
    var isTouching = useRef(false);
    var locale = useConfig().locale;
    var classPrefix = 'nut-popup';
    var overlayStyles = _object_spread({}, overlayStyle);
    var contentZIndex = harmony() ? index + 1 : index // 解决harmony层级问题
    ;
    var popStyles = _object_spread({
        zIndex: contentZIndex,
        minHeight: minHeight
    }, style);
    var _obj;
    var popClassName = classNames(classPrefix, (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-round"), round || position === 'bottom'), _define_property(_obj, "".concat(classPrefix, "-").concat(position), true), _obj), className);
    var _useState4 = _sliced_to_array(useState(''), 2), popupHeight = _useState4[0], setPopupHeight = _useState4[1];
    var _useState5 = _sliced_to_array(useState(''), 2), topBottom = _useState5[0], setTopBottom = _useState5[1];
    var resizeStyles = function() {
        if (popupHeight !== '') {
            return {
                height: popupHeight
            };
        }
    };
    var open = function() {
        if (!innerVisible) {
            // 当高度改变后，再次打开时，将高度置为初始高度
            if (position === 'bottom' && resizable && nodeRef.current && heightRef.current) {
                setPopupHeight(pxTransform(defaultHeightRef.current));
            }
            setInnerVisible(true);
            setIndex(++innerIndex);
        }
        if (destroyOnClose) {
            setShowChildren(true);
        }
        onOpen && onOpen();
    };
    var getPopupHeight = function() {
        return _async_to_generator(function() {
            var _nodeRef_current, rect, height;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            getRectInMultiPlatformWithoutCache(nodeRef.current)
                        ];
                    case 1:
                        rect = _state.sent();
                        height = ((_nodeRef_current = nodeRef.current) === null || _nodeRef_current === void 0 ? void 0 : _nodeRef_current.offsetHeight) || (rect === null || rect === void 0 ? void 0 : rect.height);
                        setTopBottom(pxTransform(height));
                        return [
                            2
                        ];
                }
            });
        })();
    };
    useEffect(function() {
        if (innerVisible && topNodeRef.current && nodeRef.current) {
            nextTick(function() {
                nextTick(function() {
                    getPopupHeight();
                });
            });
        }
    }, [
        innerVisible
    ]);
    var close = function() {
        if (innerVisible) {
            setInnerVisible(false);
            if (destroyOnClose) {
                setTimeout(function() {
                    setShowChildren(false);
                }, Number(duration));
            }
            onClose && onClose();
        }
    };
    var handleOverlayClick = function(e) {
        e.stopPropagation();
        if (closeOnOverlayClick && onOverlayClick(e)) {
            close();
        }
    };
    var handleCloseIconClick = function(e) {
        onCloseIconClick(e) && close();
    };
    var renderCloseIcon = function() {
        var closeClasses = classNames("".concat(classPrefix, "-title-right"), "".concat(classPrefix, "-title-right-").concat(closeIconPosition));
        return /*#__PURE__*/ React.createElement(React.Fragment, null, closeable && /*#__PURE__*/ React.createElement(View, {
            className: closeClasses,
            onClick: handleCloseIconClick,
            ariaRole: "button",
            ariaLabel: closeAriaLabel || locale.close
        }, /*#__PURE__*/ React.isValidElement(closeIcon) ? closeIcon : /*#__PURE__*/ React.createElement(Close, null)));
    };
    var renderTop = function() {
        if (!top) return null;
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-bottom-top"),
            ref: topNodeRef,
            style: {
                bottom: topBottom
            }
        }, top);
    };
    var renderTitle = function() {
        if (left || title || description) {
            return /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-title")
            }, position === 'bottom' && /*#__PURE__*/ React.createElement(React.Fragment, null, left && /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-title-left")
            }, left), (title || description) && /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-title-wrapper")
            }, title && /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-title-title")
            }, title), description && /*#__PURE__*/ React.createElement(View, {
                className: "".concat(classPrefix, "-title-description ").concat(title ? "".concat(classPrefix, "-title-description-gap") : '')
            }, description))), renderCloseIcon());
        }
        if (closeable) {
            return renderCloseIcon();
        }
    };
    var handleTouchStart = function(event) {
        return _async_to_generator(function() {
            var _nodeRef_current, _rootRect_current, e, rect;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (position !== 'bottom' || !resizable || !nodeRef.current) return [
                            2
                        ];
                        e = event;
                        // 开始touch，记录下touch的pageY，用以判断是向上滑动还是向下滑动
                        touchStartRef.current = e.touches[0].pageY;
                        // 标记开始滑动
                        isTouching.current = true;
                        return [
                            4,
                            getRectInMultiPlatformWithoutCache(nodeRef.current)
                        ];
                    case 1:
                        rect = _state.sent();
                        rootRect.current = rect;
                        heightRef.current = ((_nodeRef_current = nodeRef.current) === null || _nodeRef_current === void 0 ? void 0 : _nodeRef_current.offsetHeight) || ((_rootRect_current = rootRect.current) === null || _rootRect_current === void 0 ? void 0 : _rootRect_current.height) || 0;
                        if (!defaultHeightRef.current) defaultHeightRef.current = heightRef.current;
                        onTouchStart === null || onTouchStart === void 0 ? void 0 : onTouchStart(heightRef.current, e);
                        return [
                            2
                        ];
                }
            });
        })();
    };
    var handleTouchMove = function(event) {
        if (position !== 'bottom' || !resizable || !nodeRef.current || !rootRect.current) return;
        var e = event;
        e.stopPropagation();
        // 计算位移：move过程中，当前的pageY 与 start值比较
        touchMoveDistanceRef.current = e.touches[0].pageY - touchStartRef.current;
        var handleMove = function() {
            var min = typeof minHeight === 'number' ? minHeight : parseInt(String(minHeight || 0), 10) || 0;
            var currentHeight = Math.max(min, heightRef.current - touchMoveDistanceRef.current);
            setPopupHeight(pxTransform(currentHeight));
            if (touchMoveDistanceRef.current > 0 && isTouching.current) {
                // 向下滑动
                onTouchMove === null || onTouchMove === void 0 ? void 0 : onTouchMove(currentHeight, e, 'down');
            } else {
                // 向上滑动
                onTouchMove === null || onTouchMove === void 0 ? void 0 : onTouchMove(currentHeight, e, 'up');
            }
        };
        requestAnimationFrame(handleMove);
    };
    var handleTouchEnd = function(event) {
        if (position !== 'bottom' || !resizable || !nodeRef.current || !rootRect.current) return;
        var e = event;
        isTouching.current = false;
        var min = typeof minHeight === 'number' ? minHeight : parseInt(String(minHeight || 0), 10) || 0;
        var currentHeight = Math.max(min, heightRef.current - touchMoveDistanceRef.current);
        onTouchEnd === null || onTouchEnd === void 0 ? void 0 : onTouchEnd(currentHeight, e);
    };
    var renderContent = function() {
        return /*#__PURE__*/ React.createElement(View, {
            ref: nodeRef,
            style: _object_spread(_object_spread_props(_object_spread({}, popStyles), {
                display: innerVisible ? (popStyles === null || popStyles === void 0 ? void 0 : popStyles.display) || 'block' : 'none'
            }), resizeStyles()),
            className: popClassName,
            onClick: onClick,
            catchMove: lockScroll,
            onTouchStart: handleTouchStart,
            onTouchMove: handleTouchMove,
            onTouchEnd: handleTouchEnd,
            onTouchCancel: handleTouchEnd
        }, renderTop(), renderTitle(), showChildren ? children : null);
    };
    var renderPop = function() {
        return /*#__PURE__*/ React.createElement(CSSTransition, {
            nodeRef: nodeRef,
            classNames: transitionName,
            mountOnEnter: true,
            unmountOnExit: destroyOnClose,
            timeout: duration,
            in: innerVisible,
            onEntered: afterShow,
            onExited: afterClose
        }, renderContent());
    };
    var renderNode = function() {
        return /*#__PURE__*/ React.createElement(React.Fragment, null, overlay ? /*#__PURE__*/ React.createElement(Overlay, {
            zIndex: index,
            style: overlayStyles,
            className: overlayClassName,
            visible: innerVisible,
            closeOnOverlayClick: closeOnOverlayClick,
            lockScroll: lockScroll,
            duration: duration,
            onClick: handleOverlayClick
        }) : null, renderPop());
    };
    useEffect(function() {
        visible ? open() : close();
    }, [
        visible
    ]);
    useEffect(function() {
        setTransitionName(transition || "".concat(classPrefix, "-slide-").concat(position));
    }, [
        position,
        transition
    ]);
    var resolveContainer = function(getContainer) {
        return (typeof getContainer === 'function' ? getContainer() : getContainer) || document.body;
    };
    var renderToContainer = function(getContainer, node) {
        if (getContainer) {
            var container = resolveContainer(getContainer);
            return /*#__PURE__*/ createPortal(node, container);
        }
        return node;
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, renderToContainer(portal, renderNode()));
};
Popup.displayName = 'NutPopup';
