{"version":3,"file":"create-style-context.mjs","names":[],"sources":["../../src/jsx/create-style-context.js"],"sourcesContent":["'use client'\n\nimport { cx, css, sva } from '../css/index.js';\nimport { styled } from './factory.js';\nimport { getDisplayName } from './factory-helper.js';\nimport { createContext, useContext, createElement, forwardRef } from 'react'\n\nfunction createSafeContext(contextName) {\n  const Context = createContext(undefined)\n  const useStyleContext = (componentName, slot) => {\n    const context = useContext(Context)\n    if (context === undefined) {\n      const componentInfo = componentName ? `Component \"${componentName}\"` : 'A component'\n      const slotInfo = slot ? ` (slot: \"${slot}\")` : ''\n      \n      throw new Error(\n        `${componentInfo}${slotInfo} cannot access ${contextName} because it's missing its Provider.`\n      )\n    }\n    return context\n  }\n  return [Context, useStyleContext]\n}\n\nexport function createStyleContext(recipe) {\n  const isConfigRecipe = '__recipe__' in recipe\n  const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined\n  const contextName = recipeName ? `createStyleContext(\"${recipeName}\")` : 'createStyleContext'\n  \n  const [StyleContext, useStyleContext] = createSafeContext(contextName)\n  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)\n\n  const getResolvedProps = (props, slotStyles) => {\n    const { unstyled, ...restProps } = props\n    if (unstyled) return restProps\n    if (isConfigRecipe) {\n       return { ...restProps, className: cx(slotStyles, restProps.className) }\n    }\n    return { ...restProps, css: css.raw(slotStyles, restProps.css) }\n  }\n\n  const withRootProvider = (Component, options) => {\n    const WithRootProvider = (props) => {\n      const [variantProps, otherProps] = svaFn.splitVariantProps(props)\n      \n      const slotStyles = isConfigRecipe ? svaFn(variantProps) : svaFn.raw(variantProps)\n      slotStyles._classNameMap = svaFn.classNameMap\n\n      const mergedProps = options?.defaultProps \n        ? { ...options.defaultProps, ...otherProps } \n        : otherProps\n\n      return createElement(StyleContext.Provider, {\n        value: slotStyles,\n        children: createElement(Component, mergedProps)\n      })\n    }\n    \n    const componentName = getDisplayName(Component)\n    WithRootProvider.displayName = `withRootProvider(${componentName})`\n    \n    return WithRootProvider\n  }\n\n  const withProvider = (Component, slot, options) => {\n    const StyledComponent = styled(Component, {}, options)\n    \n    const WithProvider = forwardRef((props, ref) => {\n      const [variantProps, restProps] = svaFn.splitVariantProps(props)\n      \n      const slotStyles = isConfigRecipe ? svaFn(variantProps) : svaFn.raw(variantProps)\n      slotStyles._classNameMap = svaFn.classNameMap\n\n      const propsWithClass = { ...restProps, className: restProps.className ?? options?.defaultProps?.className }\n      const resolvedProps = getResolvedProps(propsWithClass, slotStyles[slot])\n      return createElement(StyleContext.Provider, {\n        value: slotStyles,\n        children: createElement(StyledComponent, {\n          ...resolvedProps,\n          className: cx(resolvedProps.className, slotStyles._classNameMap[slot]),\n          ref,\n        })\n      })\n    })\n    \n    const componentName = getDisplayName(Component)\n    WithProvider.displayName = `withProvider(${componentName})`\n    \n    return WithProvider\n  }\n\n  const withContext = (Component, slot, options) => {\n    const StyledComponent = styled(Component, {}, options)\n    const componentName = getDisplayName(Component)\n    \n    const WithContext = forwardRef((props, ref) => {\n      const slotStyles = useStyleContext(componentName, slot)\n\n      const propsWithClass = { ...props, className: props.className ?? options?.defaultProps?.className }\n      const resolvedProps = getResolvedProps(propsWithClass, slotStyles[slot])\n      return createElement(StyledComponent, {\n        ...resolvedProps,\n        className: cx(resolvedProps.className, slotStyles._classNameMap[slot]),\n        ref,\n      })\n    })\n    \n    WithContext.displayName = `withContext(${componentName})`\n    \n    return WithContext\n  }\n\n  return {\n    withRootProvider,\n    withProvider,\n    withContext,\n  }\n}"],"mappings":";;;;;;;;AAOA,SAAS,kBAAkB,aAAa;CACtC,MAAM,UAAU,cAAc,KAAA,CAAS;CACvC,MAAM,mBAAmB,eAAe,SAAS;EAC/C,MAAM,UAAU,WAAW,OAAO;EAClC,IAAI,YAAY,KAAA,GAAW;GACzB,MAAM,gBAAgB,gBAAgB,cAAc,cAAc,KAAK;GACvE,MAAM,WAAW,OAAO,YAAY,KAAK,MAAM;GAE/C,MAAM,IAAI,MACR,GAAG,gBAAgB,SAAS,iBAA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