"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useInteractionsOverlay = exports.default = exports.InteractionsOverlayProvider = 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); }
/**
 * In-tree overlay host for the message context menu (MessageInteractions).
 *
 * Why this exists: MessageInteractions used to render inside a React Native
 * `<Modal>`. On Android a Modal opens its own window, which steals focus
 * from the chat input — so opening the menu dismissed the keyboard. Hosting
 * the menu in the SAME view tree (no extra window) keeps the keyboard up.
 *
 * Coordinate note: the menu is positioned from `UIManager.measure` WINDOW
 * coordinates (pageX/pageY), but this host sits below the status bar / any
 * safe-area + header insets, so its own origin is offset from the window.
 * The host measures that offset (`measureInWindow`) and exposes it so the
 * menu can convert window coords → host-local coords. The host is mounted
 * ABOVE ChatRoom's KeyboardAvoidingView, so its origin doesn't move when the
 * keyboard opens (only the measured bubble's pageY does).
 */

const Ctx = /*#__PURE__*/(0, _react.createContext)({
  present: () => {},
  dismiss: () => {},
  originX: 0,
  originY: 0
});
const useInteractionsOverlay = () => (0, _react.useContext)(Ctx);
exports.useInteractionsOverlay = useInteractionsOverlay;
const InteractionsOverlayProvider = ({
  children
}) => {
  const [entries, setEntries] = (0, _react.useState)([]);
  const [origin, setOrigin] = (0, _react.useState)({
    x: 0,
    y: 0
  });
  const hostRef = (0, _react.useRef)(null);
  const measure = (0, _react.useCallback)(() => {
    hostRef.current?.measureInWindow?.((x, y) => {
      if (typeof x !== 'number' || typeof y !== 'number') {
        return;
      }
      setOrigin(prev => prev.x === x && prev.y === y ? prev : {
        x,
        y
      });
    });
  }, []);
  const present = (0, _react.useCallback)((id, node) => {
    // Re-measure on present: the keyboard / layout may have shifted us
    // since the last onLayout.
    measure();
    setEntries(prev => {
      const next = prev.filter(e => e.id !== id);
      next.push({
        id,
        node
      });
      return next;
    });
  }, [measure]);
  const dismiss = (0, _react.useCallback)(id => {
    setEntries(prev => prev.some(e => e.id === id) ? prev.filter(e => e.id !== id) : prev);
  }, []);

  // MUST be memoized. Otherwise every host re-render (e.g. from present()'s
  // setEntries) creates a new context value → all consumers re-render →
  // MessageInteractions' present-effect fires again → setEntries → … →
  // "Maximum update depth exceeded". present/dismiss are stable (useCallback)
  // and origin rarely changes, so this value is stable across entry changes.
  const ctxValue = (0, _react.useMemo)(() => ({
    present,
    dismiss,
    originX: origin.x,
    originY: origin.y
  }), [present, dismiss, origin.x, origin.y]);
  return (
    /*#__PURE__*/
    // `children` is a stable element from the caller, so re-rendering this
    // host (present/dismiss/origin state) does NOT re-render the chat
    // subtree — React reconciliation skips the unchanged children element.
    (0, _jsxRuntime.jsx)(_reactNative.View, {
      ref: hostRef,
      style: styles.host,
      onLayout: measure,
      collapsable: false,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Ctx.Provider, {
        value: ctxValue,
        children: [children, entries.length > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: styles.overlay,
          pointerEvents: "box-none",
          children: entries.map(e => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
            children: e.node
          }, e.id))
        })]
      })
    })
  );
};
exports.InteractionsOverlayProvider = InteractionsOverlayProvider;
const styles = _reactNative.StyleSheet.create({
  host: {
    flex: 1
  },
  overlay: {
    ..._reactNative.StyleSheet.absoluteFillObject,
    zIndex: 9999,
    elevation: 9999
  }
});
var _default = exports.default = InteractionsOverlayProvider;
//# sourceMappingURL=InteractionsOverlay.js.map