"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PortalBucket = PortalBucket;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
 * A component for rendering portals managed by a `PortalManager`.
 * It subscribes to a `PortalManager` instance to listen for changes in the portal content
 * and renders the content of its assigned portal bucket.
 *
 * @param {PortalBucketProps} props The component props.
 * @param {number} props.id The ID for the portal bucket. This ID is used by the `PortalManager` to manage the content of this bucket.
 * @param {PortalManager} props.portalManager An instance of `PortalManager` which manages the registration and unregistration of portal buckets and their content.
 * @returns {React.ReactElement} The React element(s) that are currently registered to this portal bucket.
 */
function PortalBucket(_ref) {
  var id = _ref.id,
    portalManager = _ref.portalManager;
  // State to hold the current portals for this bucket
  var _useState = (0, _react.useState)({}),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    portals = _useState2[0],
    setPortals = _useState2[1];
  // Effect to register/unregister this bucket with the portal manager on mount/unmount
  (0, _react.useLayoutEffect)(function () {
    portalManager.registerBucket(id, setPortals);
    return function () {
      portalManager.unregisterBucket(id);
    };
  }, [id, portalManager]);
  // Memoize the portal elements to avoid unnecessary re-renders
  var portalElements = (0, _react.useMemo)(function () {
    return Object.values(portals);
  }, [portals]);
  // Render the current portal elements
  // @ts-ignore - TS2322 TypeScript 5.9.2 upgrade
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, portalElements);
}