"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ARROW_HEIGHT () {
        return ARROW_HEIGHT;
    },
    get ARROW_PADDING () {
        return ARROW_PADDING;
    },
    get ARROW_POLYGON () {
        return ARROW_POLYGON;
    },
    get ARROW_WIDTH () {
        return ARROW_WIDTH;
    },
    get popoverConfig () {
        return popoverConfig;
    },
    get popoverRoot () {
        return popoverRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _react1 = require("@floating-ui/react");
const _styledcomponents = require("styled-components");
const _Icon = require("../../_Icon");
const _Resizable = require("../../_Resizable");
const _Slot = require("../../_Slot/Slot");
const _utils = require("./utils");
const _Popovertokens = require("./Popover.tokens");
const _Popoverstyles = require("./Popover.styles");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const ARROW_WIDTH = 20;
const ARROW_HEIGHT = 8;
const ARROW_POLYGON = 'M20 20L0 20C8.88889 20.0001 10 12.5714 10 12C10 12.5714 11.3273 20.006 20 20Z';
const ARROW_PADDING = 16;
const popoverRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ appearance = 'closeNone', children, target, opened: outerOpened, defaultOpened = false, trigger = 'click', placement = 'bottom', hasTail = true, flip: outsideFlip = false, shift: outsideShift = false, offset: outerOffset = 0, outsideClick = true, resizable = false, onResizeStart, onResizeEnd, onToggle, delayOpen = 0, delayClose = 0, zIndex = 1000, className, style, size, view, portal, ...rest }, outerRootRef)=>{
        const [innerOpened, setInnerOpened] = (0, _react.useState)(defaultOpened);
        const opened = outerOpened ?? innerOpened;
        const arrowRef = (0, _react.useRef)(null);
        const handleToggle = (opened)=>{
            setInnerOpened(opened);
            if (onToggle) {
                onToggle(opened);
            }
        };
        const { refs, floatingStyles, context } = (0, _react1.useFloating)({
            whileElementsMounted: _react1.autoUpdate,
            placement,
            open: opened,
            onOpenChange: handleToggle,
            middleware: [
                outsideShift && (0, _react1.shift)({
                    limiter: (0, _react1.limitShift)()
                }),
                outsideFlip && (0, _react1.flip)(),
                hasTail && (0, _react1.arrow)({
                    element: arrowRef,
                    padding: ARROW_PADDING
                }),
                (0, _react1.offset)((hasTail ? ARROW_HEIGHT : 0) + outerOffset)
            ]
        });
        const click = (0, _react1.useClick)(context, {
            enabled: trigger === 'click' || matchMedia('(hover: none)').matches
        });
        const focus = (0, _react1.useFocus)(context, {
            enabled: trigger === 'focus'
        });
        const dismiss = (0, _react1.useDismiss)(context, {
            enabled: true,
            outsidePress: outsideClick
        });
        const role = (0, _react1.useRole)(context);
        const hover = (0, _react1.useHover)(context, {
            mouseOnly: true,
            enabled: trigger === 'hover',
            handleClose: (0, _react1.safePolygon)({
                requireIntent: false
            }),
            delay: {
                open: delayOpen,
                close: delayClose
            }
        });
        const { getReferenceProps, getFloatingProps } = (0, _react1.useInteractions)([
            dismiss,
            role,
            click,
            hover,
            focus
        ]);
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_Slot.Slot, {
            ref: refs.setReference,
            ...getReferenceProps()
        }, target), opened && /*#__PURE__*/ _react.default.createElement(_react1.FloatingPortal, (0, _utils.getFloatingPortalProps)(portal), /*#__PURE__*/ _react.default.createElement(_react1.FloatingFocusManager, {
            context: context
        }, /*#__PURE__*/ _react.default.createElement(Root, {
            ref: refs.setFloating,
            size: size,
            view: view,
            style: {
                ...floatingStyles,
                zIndex
            },
            ...getFloatingProps()
        }, /*#__PURE__*/ _react.default.createElement(_Resizable.Resizable, {
            placement: (0, _utils.matchPlacements)(placement),
            resizable: resizable,
            onResizeStart: onResizeStart,
            onResizeEnd: onResizeEnd
        }, /*#__PURE__*/ _react.default.createElement(_Popoverstyles.Wrapper, {
            ref: outerRootRef,
            className: (0, _classnames.default)(className, _Popovertokens.classes.popoverRoot),
            style: style,
            "data-popover-open": opened,
            ...rest
        }, hasTail && /*#__PURE__*/ _react.default.createElement(_react1.FloatingArrow, {
            ref: arrowRef,
            context: context,
            width: ARROW_WIDTH,
            height: ARROW_HEIGHT,
            fill: `var(${_Popovertokens.tokens.backgroundColor})`,
            d: ARROW_POLYGON
        }), children, appearance === 'closeInner' && /*#__PURE__*/ _react.default.createElement(_Popoverstyles.CloseButton, {
            className: _Popovertokens.classes.popoverCloseIconButton,
            onClick: ()=>handleToggle(false)
        }, /*#__PURE__*/ _react.default.createElement(_Icon.IconClose, {
            size: (0, _utils.sizeToIconSize)(size),
            color: "inherit"
        }))))))));
    });
const popoverConfig = {
    name: 'Popover',
    tag: 'div',
    layout: popoverRoot,
    base: _Popoverstyles.base,
    variations: {
        view: {
            css: (0, _styledcomponents.css)([
                ``
            ])
        },
        size: {
            css: (0, _styledcomponents.css)([
                ``
            ])
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
