"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getAttach = exports.default = void 0;
var _react = require("react");
var _reactDom = require("react-dom");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var getAttach = exports.getAttach = function getAttach(attach) {
  if (typeof attach === 'string') {
    return document.querySelector(attach);
  }
  if (_typeof(attach) === 'object' && attach instanceof window.HTMLElement) {
    return attach;
  }
  return document.body;
};
var Portal = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _props$attach = props.attach,
    attach = _props$attach === void 0 ? document.body : _props$attach,
    children = props.children;
  var container = (0, _react.useMemo)(function () {
    var el = document.createElement('div');
    el.className = "portal-wrapper";
    return el;
  }, []);
  (0, _react.useEffect)(function () {
    var _parentElement$append;
    var parentElement = getAttach(attach);
    parentElement === null || parentElement === void 0 || (_parentElement$append = parentElement.appendChild) === null || _parentElement$append === void 0 || _parentElement$append.call(parentElement, container);
    return function () {
      var _parentElement$remove;
      parentElement === null || parentElement === void 0 || (_parentElement$remove = parentElement.removeChild) === null || _parentElement$remove === void 0 || _parentElement$remove.call(parentElement, container);
    };
  }, [container, attach]);
  (0, _react.useImperativeHandle)(ref, function () {
    return container;
  });
  return /*#__PURE__*/(0, _reactDom.createPortal)(children, container);
});
var _default = exports.default = Portal;