File size: 1,752 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { type SystemContext } from "../src"

function toLiteralStringType(strings: string[]) {
  return (
    strings
      .map((s) => `"${s}"`)
      .join(" | ")
      .trim() || "string"
  )
}

type PropertyInfo = Record<
  string,
  {
    type: string
    defaultValue?: string
    required: boolean
    description: string
  }
>

const toRemove = ["transparent", "current", "border", "bg", "fg"]

function extractColorScheme(system: SystemContext) {
  const palettes = system.tokens.colorPaletteMap
  toRemove.forEach((key) => palettes.delete(key))
  return toLiteralStringType([...system.tokens.colorPaletteMap.keys()])
}

export function extractRecipeProps(system: SystemContext) {
  const result: Record<string, PropertyInfo> = {}

  const colorSchemeType = extractColorScheme(system)

  const recipeKeys = Object.keys({
    ...(system._config.theme?.recipes ?? {}),
    ...(system._config.theme?.slotRecipes ?? {}),
  })

  for (const key of recipeKeys) {
    const _recipe = system.getRecipe(key)
    let config = _recipe || system.getSlotRecipe(key)

    const recipe = _recipe ? system.cva(config) : system.sva(config)
    const _key = _recipe ? key : `${key}Root`

    result[_key] ||= {
      colorPalette: {
        defaultValue: "gray",
        type: colorSchemeType,
        required: false,
        description: "The color palette of the component",
      },
    }

    Object.keys(recipe.variantMap).forEach((variant) => {
      const values = recipe.variantMap[variant] as string[]
      result[_key][variant] = {
        type: toLiteralStringType(values),
        defaultValue: config.defaultVariants[variant],
        required: false,
        description: `The ${variant} of the component`,
      }
    })
  }

  return result
}