"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Provider = exports.Portal = exports.Outlet = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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); }
function createPortalGroup() {
  const Context = /*#__PURE__*/(0, _react.createContext)({
    outlet: null,
    append: () => {},
    remove: () => {}
  });
  Context.displayName = 'BloomPortalContext';
  function Provider(props) {
    const map = (0, _react.useRef)({});
    const [outlet, setOutlet] = (0, _react.useState)(null);
    const append = (0, _react.useCallback)((id, component) => {
      map.current[id] = /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
        children: component
      }, id);
      setOutlet(/*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: Object.values(map.current)
      }));
    }, []);
    const remove = (0, _react.useCallback)(id => {
      delete map.current[id];
      setOutlet(/*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: Object.values(map.current)
      }));
    }, []);
    const contextValue = (0, _react.useMemo)(() => ({
      outlet,
      append,
      remove
    }), [outlet, append, remove]);
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(Context.Provider, {
      value: contextValue,
      children: props.children
    });
  }
  function Outlet() {
    const ctx = (0, _react.useContext)(Context);
    if (_reactNative.Platform.OS === 'web') {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.portalOutlet,
        children: ctx.outlet
      });
    }
    return ctx.outlet;
  }
  function Portal({
    children
  }) {
    const {
      append,
      remove
    } = (0, _react.useContext)(Context);
    const id = (0, _react.useId)();
    (0, _react.useEffect)(() => {
      append(id, children);
      return () => remove(id);
    }, [id, children, append, remove]);
    return null;
  }
  return {
    Provider,
    Outlet,
    Portal
  };
}
/**
 * `DefaultPortal` is a `globalThis`-guarded singleton so that every physical copy of
 * this module shares ONE portal group (one `Context`, one `Provider`/`Outlet`/`Portal`
 * triple).
 *
 * Why this is required: like the theme module, `exports["./portal"]` ships a
 * `react-native` → `./src/...` condition alongside the `lib/module` / `lib/commonjs`
 * forks, and the `Portal` component is consumed cross-subpath from `./tooltip`,
 * `./prompt-input`, `./dialog`, `./select`, `./menu`, `./context-menu`, and `./popover`
 * while the `Provider`/`Outlet` are mounted once at the app root via `@oxyhq/bloom/portal`.
 * A bundler can resolve those subpaths through different export conditions, so without a
 * guard each physical copy runs its own `createPortalGroup()` and a `<Portal>` from copy
 * A would register against the `Provider`'s map in copy B — its content would never reach
 * the `Outlet`. Anchoring the group on `globalThis` (keyed by `__oxyhq_bloom_portal_group__`)
 * collapses all copies onto one group. Same src-vs-lib dual-instance class as
 * `BloomThemeContext`.
 */
const DefaultPortal = globalThis.__oxyhq_bloom_portal_group__ ??= createPortalGroup();
const Provider = exports.Provider = DefaultPortal.Provider;
const Outlet = exports.Outlet = /*#__PURE__*/(0, _react.memo)(DefaultPortal.Outlet);
const Portal = exports.Portal = DefaultPortal.Portal;
const styles = _reactNative.StyleSheet.create({
  portalOutlet: {
    position: 'fixed',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    pointerEvents: 'box-none',
    zIndex: 999999
  }
});
//# sourceMappingURL=index.js.map