"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;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _react1 = require("@floating-ui/react");
var _styledcomponents = require("styled-components");
var _Icon = require("../../_Icon");
var _Resizable = require("../../_Resizable");
var _Slot = require("../../_Slot/Slot");
var _utils = require("./utils");
var _Popovertokens = require("./Popover.tokens");
var _Popoverstyles = require("./Popover.styles");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var ARROW_WIDTH = 20;
var ARROW_HEIGHT = 8;
var ARROW_POLYGON = 'M20 20L0 20C8.88889 20.0001 10 12.5714 10 12C10 12.5714 11.3273 20.006 20 20Z';
var ARROW_PADDING = 16;
var popoverRoot = function popoverRoot(Root) {
    return /*#__PURE__*/ (0, _react.forwardRef)(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), _ref_appearance = _ref2.appearance, appearance = _ref_appearance === void 0 ? 'closeNone' : _ref_appearance, children = _ref2.children, target = _ref2.target, outerOpened = _ref2.opened, _ref_defaultOpened = _ref2.defaultOpened, defaultOpened = _ref_defaultOpened === void 0 ? false : _ref_defaultOpened, _ref_trigger = _ref2.trigger, trigger = _ref_trigger === void 0 ? 'click' : _ref_trigger, _ref_placement = _ref2.placement, placement = _ref_placement === void 0 ? 'bottom' : _ref_placement, _ref_hasTail = _ref2.hasTail, hasTail = _ref_hasTail === void 0 ? true : _ref_hasTail, tmp = _ref2.flip, outsideFlip = tmp === void 0 ? false : tmp, tmp1 = _ref2.shift, outsideShift = tmp1 === void 0 ? false : tmp1, tmp2 = _ref2.offset, outerOffset = tmp2 === void 0 ? 0 : tmp2, _ref_outsideClick = _ref2.outsideClick, outsideClick = _ref_outsideClick === void 0 ? true : _ref_outsideClick, _ref_resizable = _ref2.resizable, resizable = _ref_resizable === void 0 ? false : _ref_resizable, onResizeStart = _ref2.onResizeStart, onResizeEnd = _ref2.onResizeEnd, onToggle = _ref2.onToggle, _ref_delayOpen = _ref2.delayOpen, delayOpen = _ref_delayOpen === void 0 ? 0 : _ref_delayOpen, _ref_delayClose = _ref2.delayClose, delayClose = _ref_delayClose === void 0 ? 0 : _ref_delayClose, _ref_zIndex = _ref2.zIndex, zIndex = _ref_zIndex === void 0 ? 1000 : _ref_zIndex, className = _ref2.className, style = _ref2.style, size = _ref2.size, view = _ref2.view, portal = _ref2.portal, rest = _object_without_properties(_ref2, [
            "appearance",
            "children",
            "target",
            "opened",
            "defaultOpened",
            "trigger",
            "placement",
            "hasTail",
            "flip",
            "shift",
            "offset",
            "outsideClick",
            "resizable",
            "onResizeStart",
            "onResizeEnd",
            "onToggle",
            "delayOpen",
            "delayClose",
            "zIndex",
            "className",
            "style",
            "size",
            "view",
            "portal"
        ]), _rest1 = _sliced_to_array(_rest, 1), outerRootRef = _rest1[0];
        var _useState = _sliced_to_array((0, _react.useState)(defaultOpened), 2), innerOpened = _useState[0], setInnerOpened = _useState[1];
        var opened = outerOpened !== null && outerOpened !== void 0 ? outerOpened : innerOpened;
        var arrowRef = (0, _react.useRef)(null);
        var handleToggle = function handleToggle(opened) {
            setInnerOpened(opened);
            if (onToggle) {
                onToggle(opened);
            }
        };
        var _useFloating = (0, _react1.useFloating)({
            whileElementsMounted: _react1.autoUpdate,
            placement: 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)
            ]
        }), refs = _useFloating.refs, floatingStyles = _useFloating.floatingStyles, context = _useFloating.context;
        var click = (0, _react1.useClick)(context, {
            enabled: trigger === 'click' || matchMedia('(hover: none)').matches
        });
        var focus = (0, _react1.useFocus)(context, {
            enabled: trigger === 'focus'
        });
        var dismiss = (0, _react1.useDismiss)(context, {
            enabled: true,
            outsidePress: outsideClick
        });
        var role = (0, _react1.useRole)(context);
        var hover = (0, _react1.useHover)(context, {
            mouseOnly: true,
            enabled: trigger === 'hover',
            handleClose: (0, _react1.safePolygon)({
                requireIntent: false
            }),
            delay: {
                open: delayOpen,
                close: delayClose
            }
        });
        var _useInteractions = (0, _react1.useInteractions)([
            dismiss,
            role,
            click,
            hover,
            focus
        ]), getReferenceProps = _useInteractions.getReferenceProps, getFloatingProps = _useInteractions.getFloatingProps;
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_Slot.Slot, _object_spread({
            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, _object_spread({
            ref: refs.setFloating,
            size: size,
            view: view,
            style: _object_spread_props(_object_spread({}, floatingStyles), {
                zIndex: 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, _object_spread({
            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(".concat(_Popovertokens.tokens.backgroundColor, ")"),
            d: ARROW_POLYGON
        }), children, appearance === 'closeInner' && /*#__PURE__*/ _react.default.createElement(_Popoverstyles.CloseButton, {
            className: _Popovertokens.classes.popoverCloseIconButton,
            onClick: function onClick() {
                return handleToggle(false);
            }
        }, /*#__PURE__*/ _react.default.createElement(_Icon.IconClose, {
            size: (0, _utils.sizeToIconSize)(size),
            color: "inherit"
        }))))))));
    });
};
var 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'
    }
};
