"use strict";

exports.__esModule = true;
exports.useOpenCollectionOverlay = useOpenCollectionOverlay;
var _react = require("react");
var _react2 = require("@wix/bex-core/react");
/**
 * Builds an opener that navigates to a collection (by id) as an overlay on top
 * of the current page, and invokes `onReturnToForeground` once the overlay
 * closes and the page returns to the foreground.
 *
 * Must be called from a component that outlives the inline cell editor (e.g. a
 * cell type's `EditWrapper`): opening the overlay typically commits/unmounts
 * the editor, so the layer-state subscription is owned here and disposed only
 * on this owner's unmount (and before re-opening). The editor passes the
 * refresh it wants to run on return as `onReturnToForeground` when it calls the
 * opener, since the editor — not this owner — holds the data to refresh.
 *
 * Returns `undefined` when the host can't navigate (no `collectionId`,
 * `navigateTo`, or page location).
 */
function useOpenCollectionOverlay({
  collectionId
}) {
  const container = (0, _react2.useWixPatternsContainer)();
  const pageLocation = container.usePageLocation == null ? void 0 : container.usePageLocation();
  const navigateTo = container.navigateTo;
  const onLayerStateChange = container.onLayerStateChange;

  // The subscription is owned by this (stable) component, not the editor that
  // triggers it, so it survives the editor unmounting when the overlay opens
  // and is disposed on this owner's unmount (or before re-opening).
  const subscriptionRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => () => {
    var _subscriptionRef$curr;
    (_subscriptionRef$curr = subscriptionRef.current) == null || _subscriptionRef$curr.remove();
    subscriptionRef.current = null;
  }, []);
  const open = (0, _react.useCallback)(onReturnToForeground => {
    var _subscriptionRef$curr2;
    if (!collectionId || !navigateTo || !pageLocation) {
      return;
    }
    // Preserve the current route (pathname, existing query params, hash) and
    // only swap in the referenced collectionId, so nested routes and required
    // query state survive the navigation.
    const params = new URLSearchParams(pageLocation.search);
    params.set('collectionId', collectionId);
    navigateTo({
      pageId: pageLocation.pageId,
      relativeUrl: `${pageLocation.pathname}?${params.toString()}${pageLocation.hash ?? ''}`,
      displayMode: 'overlay',
      history: 'replace'
    });
    (_subscriptionRef$curr2 = subscriptionRef.current) == null || _subscriptionRef$curr2.remove();
    subscriptionRef.current = (onLayerStateChange == null ? void 0 : onLayerStateChange(newState => {
      if (newState === 'foreground') {
        var _subscriptionRef$curr3;
        (_subscriptionRef$curr3 = subscriptionRef.current) == null || _subscriptionRef$curr3.remove();
        subscriptionRef.current = null;
        onReturnToForeground == null || onReturnToForeground();
      }
    })) ?? null;
  }, [collectionId, navigateTo, pageLocation, onLayerStateChange]);
  const canOpen = Boolean(collectionId && navigateTo && pageLocation);
  return canOpen ? open : undefined;
}
//# sourceMappingURL=useOpenCollectionOverlay.js.map