"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SheetShell = SheetShell;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _index = require("../bottom-sheet/index.js");
var _useTheme = require("../theme/use-theme.js");
var _context = require("./context.js");
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); }
/**
 * Internal bottom-sheet shell shared by `Menu`, `Select` and `ContextMenu`.
 *
 * Not exported from the public `@oxyhq/bloom` surface — these three
 * components historically used the low-level `Dialog.Outer / Inner /
 * Handle` primitives, which are gone in 0.5.0. This shell captures the
 * shared shape (BottomSheet + drag handle + close-on-tap context) in a
 * single place so the three internal call sites stay symmetrical.
 *
 * Consumers needing the same behaviour for app code should use the public
 * `BottomSheet` primitive directly.
 */

/**
 * Bottom-sheet shell with an embedded drag handle. Exposes the bloom
 * dialog `close()` context to descendants — `Menu.Item`, `Select.Item`
 * etc. rely on it to dismiss the sheet after selection.
 */
function SheetShell({
  control,
  label,
  header,
  onClose,
  contentStyle,
  children
}) {
  const theme = (0, _useTheme.useTheme)();
  const ref = (0, _react.useRef)(null);
  const closeCallbacks = (0, _react.useRef)([]);
  const callQueuedCallbacks = (0, _react.useCallback)(() => {
    const queued = closeCallbacks.current;
    closeCallbacks.current = [];
    for (const cb of queued) {
      try {
        cb();
      } catch (e) {
        if (typeof console !== 'undefined' && console.error) {
          console.error('SheetShell close callback error:', e);
        }
      }
    }
  }, []);
  const open = (0, _react.useCallback)(() => {
    ref.current?.present();
  }, []);
  const close = (0, _react.useCallback)(cb => {
    if (typeof cb === 'function') {
      closeCallbacks.current.push(cb);
    }
    ref.current?.dismiss();
  }, []);
  const handleDismiss = (0, _react.useCallback)(() => {
    callQueuedCallbacks();
    onClose?.();
  }, [callQueuedCallbacks, onClose]);
  (0, _react.useImperativeHandle)(control.ref, () => ({
    open,
    close
  }), [open, close]);
  const context = (0, _react.useMemo)(() => ({
    close,
    isWithinDialog: true
  }), [close]);
  const sheetStyle = (0, _react.useMemo)(() => ({
    maxWidth: 500,
    backgroundColor: theme.colors.background,
    borderRadius: 20
  }), [theme.colors.background]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_index.BottomSheet, {
    ref: ref,
    onDismiss: handleDismiss,
    enablePanDownToClose: true,
    detached: true,
    backdropOpacity: 0.7,
    style: sheetStyle,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_context.Context.Provider, {
      value: context,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(SheetHandle, {
        onPress: () => close()
      }), header, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        accessibilityLabel: label,
        style: [styles.body, {
          backgroundColor: theme.colors.background
        }, contentStyle],
        children: children
      })]
    })
  });
}
function SheetHandle({
  onPress
}) {
  const theme = (0, _useTheme.useTheme)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: styles.handleContainer,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
      onPress: onPress,
      accessibilityLabel: "Dismiss",
      accessibilityHint: "Tap to close",
      hitSlop: {
        top: 10,
        bottom: 10,
        left: 40,
        right: 40
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: [styles.handleBar, {
          backgroundColor: theme.colors.text
        }]
      })
    })
  });
}
const styles = _reactNative.StyleSheet.create({
  handleContainer: {
    position: 'absolute',
    width: '100%',
    alignItems: 'center',
    zIndex: 10,
    height: 20
  },
  handleBar: {
    top: 8,
    width: 35,
    height: 5,
    borderRadius: 3,
    alignSelf: 'center',
    opacity: 0.5
  },
  body: {
    paddingTop: 20,
    paddingHorizontal: 20,
    paddingBottom: 20
  }
});
//# sourceMappingURL=SheetShell.js.map