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 _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
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 _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
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_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
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);
}
import React, { useEffect, useState, createContext, useContext } from 'react';
export var POPOVER_PORTAL_ID = 'plasma-popup-root';
var items = [];
var PopupBaseContext = /*#__PURE__*/ createContext({
    items: items,
    register: function register(_info) {
        throw new Error('Function not implemented. Add PopupBaseProvider');
    },
    unregister: function unregister(_id) {
        throw new Error('Function not implemented. Add PopupBaseProvider');
    }
});
export var usePopupBaseContext = function usePopupBaseContext() {
    return useContext(PopupBaseContext);
};
export var PopupBaseProvider = function PopupBaseProvider(param) {
    var children = param.children;
    var _useState = _sliced_to_array(useState([]), 2), items = _useState[0], setItems = _useState[1];
    var register = function register(info) {
        setItems(_to_consumable_array(items).concat([
            info
        ]));
    };
    var unregister = function unregister(id) {
        var index = items.findIndex(function(item) {
            return id === item.id;
        });
        if (index === -1) {
            return;
        }
        items.splice(index, 1);
        setItems(_to_consumable_array(items));
    };
    var context = {
        items: items,
        register: register,
        unregister: unregister
    };
    useEffect(function() {
        return function() {
            var portal = document.createElement('div');
            if (portal && document.body.contains(portal)) {
                document.body.removeChild(portal);
            }
        };
    }, []);
    return /*#__PURE__*/ React.createElement(PopupBaseContext.Provider, {
        value: context
    }, children);
};
