"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BloomColorScope = BloomColorScope;
exports.useColorScopeStyle = useColorScopeStyle;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _BloomThemeProvider = require("../BloomThemeProvider.js");
var _buildTheme = require("../build-theme.js");
var _styleBuilder = require("./style-builder.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); }
function BloomColorScope({
  colorPreset,
  asChild = false,
  style,
  children
}) {
  const parent = (0, _react.useContext)(_BloomThemeProvider.BloomThemeContext);
  if (!parent) {
    throw new Error('BloomColorScope must be used within a <BloomThemeProvider>');
  }
  if (!colorPreset) return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: children
  });
  const resolvedMode = parent.theme.mode;
  const contextValue = (0, _react.useMemo)(() => {
    const theme = (0, _buildTheme.buildTheme)(colorPreset, resolvedMode);
    return {
      ...parent,
      theme,
      colorPreset
    };
  }, [colorPreset, resolvedMode, parent]);

  // Preset vars flow to the subtree through react-native-css's real
  // VariableContext (`VariableContextProvider`), NOT via an inline `vars()`
  // style — under react-native-css@3 inline vars applied to a plain `<View>`
  // (no matched className rules) are dropped silently. See `style-builder.ts`.
  const nativeVars = (0, _react.useMemo)(() => (0, _styleBuilder.buildScopeVars)(colorPreset, resolvedMode), [colorPreset, resolvedMode]);
  const VariableProvider = (0, _styleBuilder.getVariableContextProvider)();
  let content;
  if (asChild) {
    const child = _react.Children.only(children);
    if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
      throw new Error('BloomColorScope with `asChild` requires a single React element child that accepts a `style` prop.');
    }
    // The vars no longer ride on the child's `style`; merge only the caller's
    // `style` and the child's own `style` (child last so its styles win).
    const childStyle = child.props.style;
    const mergedStyle = [style, childStyle];
    content = /*#__PURE__*/(0, _react.cloneElement)(child, {
      style: mergedStyle
    });
  } else {
    content = /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: [{
        flex: 1
      }, style],
      children: children
    });
  }

  // Wrap the subtree in the variable provider when NativeWind is available;
  // otherwise render as-is so we never crash on web/non-NativeWind hosts.
  const scoped = VariableProvider ? /*#__PURE__*/(0, _jsxRuntime.jsx)(VariableProvider, {
    value: nativeVars,
    children: content
  }) : content;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_BloomThemeProvider.BloomThemeContext.Provider, {
    value: contextValue,
    children: scoped
  });
}

/**
 * Escape hatch for advanced cases where the wrapping element is owned by the
 * caller. Returns a stable native style object carrying the preset's CSS vars
 * via NativeWind's `vars()`. Returns `undefined` on web or when `nativewind`
 * is not installed.
 *
 * NOTE: subject to the react-native-css@3 limitation — the returned style only
 * propagates vars to descendants when applied to an interop className component
 * (the runtime gates propagation on matched className style rules). It is a
 * no-op on a plain `<View>` with no `className`. For the common case prefer
 * `<BloomColorScope>`, which wraps children in `VariableContextProvider`.
 */
function useColorScopeStyle(colorPreset) {
  const parent = (0, _react.useContext)(_BloomThemeProvider.BloomThemeContext);
  if (!parent) {
    throw new Error('useColorScopeStyle must be used within a <BloomThemeProvider>');
  }
  const resolvedMode = parent.theme.mode;
  return (0, _react.useMemo)(() => (0, _styleBuilder.buildNativePresetStyle)(colorPreset, resolvedMode), [colorPreset, resolvedMode]);
}
//# sourceMappingURL=index.js.map