"use strict";

import React, { createContext, Fragment, memo, useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from 'react';
import { View, StyleSheet, Platform } from 'react-native';
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
function createPortalGroup() {
  const Context = /*#__PURE__*/createContext({
    outlet: null,
    append: () => {},
    remove: () => {}
  });
  Context.displayName = 'BloomPortalContext';
  function Provider(props) {
    const map = useRef({});
    const [outlet, setOutlet] = useState(null);
    const append = useCallback((id, component) => {
      map.current[id] = /*#__PURE__*/_jsx(Fragment, {
        children: component
      }, id);
      setOutlet(/*#__PURE__*/_jsx(_Fragment, {
        children: Object.values(map.current)
      }));
    }, []);
    const remove = useCallback(id => {
      delete map.current[id];
      setOutlet(/*#__PURE__*/_jsx(_Fragment, {
        children: Object.values(map.current)
      }));
    }, []);
    const contextValue = useMemo(() => ({
      outlet,
      append,
      remove
    }), [outlet, append, remove]);
    return /*#__PURE__*/_jsx(Context.Provider, {
      value: contextValue,
      children: props.children
    });
  }
  function Outlet() {
    const ctx = useContext(Context);
    if (Platform.OS === 'web') {
      return /*#__PURE__*/_jsx(View, {
        style: styles.portalOutlet,
        children: ctx.outlet
      });
    }
    return ctx.outlet;
  }
  function Portal({
    children
  }) {
    const {
      append,
      remove
    } = useContext(Context);
    const id = useId();
    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();
export const Provider = DefaultPortal.Provider;
export const Outlet = /*#__PURE__*/memo(DefaultPortal.Outlet);
export const Portal = DefaultPortal.Portal;
const styles = StyleSheet.create({
  portalOutlet: {
    position: 'fixed',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    pointerEvents: 'box-none',
    zIndex: 999999
  }
});
//# sourceMappingURL=index.js.map