"use strict";

import React, { Children, cloneElement, isValidElement, useContext, useMemo } from 'react';
import { View } from 'react-native';
import { BloomThemeContext } from "../BloomThemeProvider.js";
import { buildTheme } from "../build-theme.js";
import { buildNativePresetStyle, buildScopeVars, getVariableContextProvider } from "./style-builder.js";
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
export function BloomColorScope({
  colorPreset,
  asChild = false,
  style,
  children
}) {
  const parent = useContext(BloomThemeContext);
  if (!parent) {
    throw new Error('BloomColorScope must be used within a <BloomThemeProvider>');
  }
  if (!colorPreset) return /*#__PURE__*/_jsx(_Fragment, {
    children: children
  });
  const resolvedMode = parent.theme.mode;
  const contextValue = useMemo(() => {
    const theme = 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 = useMemo(() => buildScopeVars(colorPreset, resolvedMode), [colorPreset, resolvedMode]);
  const VariableProvider = getVariableContextProvider();
  let content;
  if (asChild) {
    const child = Children.only(children);
    if (! /*#__PURE__*/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__*/cloneElement(child, {
      style: mergedStyle
    });
  } else {
    content = /*#__PURE__*/_jsx(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__*/_jsx(VariableProvider, {
    value: nativeVars,
    children: content
  }) : content;
  return /*#__PURE__*/_jsx(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`.
 */
export function useColorScopeStyle(colorPreset) {
  const parent = useContext(BloomThemeContext);
  if (!parent) {
    throw new Error('useColorScopeStyle must be used within a <BloomThemeProvider>');
  }
  const resolvedMode = parent.theme.mode;
  return useMemo(() => buildNativePresetStyle(colorPreset, resolvedMode), [colorPreset, resolvedMode]);
}
//# sourceMappingURL=index.js.map