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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import {
  type Dict,
  compact,
  createProps,
  isFunction,
  isObject,
  isString,
  mapObject,
  memo,
  walkObject,
} from "../utils"
import { cssVar } from "./css-var"
import { esc } from "./esc"
import { expandTokenReferences as _expandReferences } from "./expand-reference"
import { mapToJson } from "./map-to-json"
import {
  TOKEN_PATH_REGEX,
  expandReferences,
  getReferences,
  hasReference,
} from "./references"
import { tokenMiddlewares } from "./token-middleware"
import { tokenTransforms } from "./token-transforms"
import type {
  SemanticTokenDefinition,
  Token,
  TokenCategory,
  TokenDefinition,
  TokenDictionary,
  TokenEnforcePhase,
  TokenMiddleware,
  TokenTransformer,
} from "./types"

interface Options {
  prefix?: string | undefined
  breakpoints?: Record<string, string> | undefined
  tokens?: TokenDefinition | undefined
  semanticTokens?: SemanticTokenDefinition | undefined
}

const isToken = (value: any) => {
  return isObject(value) && Object.prototype.hasOwnProperty.call(value, "value")
}

function expandBreakpoints(breakpoints?: Record<string, string>) {
  if (!breakpoints) return { breakpoints: {}, sizes: {} }
  return {
    breakpoints: mapObject(breakpoints, (value) => ({ value })),
    sizes: Object.fromEntries(
      Object.entries(breakpoints).map(([key, value]) => [
        `breakpoint-${key}`,
        { value },
      ]),
    ),
  }
}

export function createTokenDictionary(options: Options): TokenDictionary {
  const {
    prefix = "",
    tokens = {},
    semanticTokens = {},
    breakpoints = {},
  } = options

  const formatTokenName = (path: string[]) => path.join(".")

  const formatCssVar = (path: string[], prefix: string) =>
    cssVar(path.join("-"), { prefix })

  const allTokens: Token[] = []
  const tokenNameMap: Map<string, Token> = new Map()
  const conditionMap = new Map<string, Set<Token>>()
  const cssVarMap = new Map<string, Map<string, string>>()
  const colorPaletteMap = new Map<string, Map<string, string>>()
  const flatMap = new Map<string, string>()
  const byCategory = new Map<string, Map<string, string>>()
  const categoryMap = new Map<string, Map<string, Token>>()

  const transforms: Map<string, TokenTransformer> = new Map()
  const middlewares: TokenMiddleware[] = []

  function registerToken(token: Token, phase?: TokenEnforcePhase) {
    allTokens.push(token)
    tokenNameMap.set(token.name, token)

    if (phase) {
      transforms.forEach((fn) => {
        if (fn.enforce === phase) transformToken(fn, token)
      })
    }
  }

  const breakpointTokens = expandBreakpoints(breakpoints)

  const computedTokens = compact({
    ...tokens,
    breakpoints: breakpointTokens.breakpoints,
    sizes: {
      ...tokens.sizes,
      ...breakpointTokens.sizes,
    },
  })

  function registerTokens() {
    walkObject(
      computedTokens,
      (entry, path) => {
        const isDefault = path.includes("DEFAULT")
        path = filterDefault(path)

        const category = path[0]
        const name = formatTokenName(path)

        const t = isString(entry) ? { value: entry } : entry

        const token: Token = {
          value: t.value,
          originalValue: t.value,
          name,
          path,
          extensions: {
            condition: "base",
            originalPath: path,
            category,
            prop: formatTokenName(path.slice(1)),
          },
        }

        if (isDefault) {
          token.extensions.default = true
        }

        registerToken(token)
      },
      { stop: isToken },
    )

    walkObject(
      semanticTokens,
      (entry, path) => {
        const isDefault = path.includes("DEFAULT")
        path = filterBaseCondition(filterDefault(path))

        const category = path[0]

        const name = formatTokenName(path)
        const t = isString(entry.value)
          ? { value: { base: entry.value } }
          : entry

        const token: Token = {
          value: t.value.base || "",
          originalValue: t.value.base || "",
          name,
          path,
          extensions: {
            originalPath: path,
            category,
            conditions: t.value,
            condition: "base",
            prop: formatTokenName(path.slice(1)),
          },
        }

        if (isDefault) {
          token.extensions.default = true
        }

        registerToken(token)
      },
      { stop: isToken },
    )
  }

  function getByName(name: string) {
    return tokenNameMap.get(name)
  }

  function buildConditionMap(token: Token) {
    const { condition } = token.extensions
    if (!condition) return

    if (!conditionMap.has(condition)) {
      conditionMap.set(condition, new Set<Token>())
    }

    conditionMap.get(condition)!.add(token)
  }

  function buildCategoryMap(token: Token) {
    const { category, prop } = token.extensions
    if (!category) return

    if (!categoryMap.has(category)) {
      categoryMap.set(category, new Map())
    }

    categoryMap.get(category)!.set(prop, token)
  }

  function buildCssVars(token: Token) {
    const { condition, negative, virtual, cssVar } = token.extensions
    if (negative || virtual || !condition || !cssVar) return

    if (!cssVarMap.has(condition)) {
      cssVarMap.set(condition, new Map())
    }

    cssVarMap.get(condition)!.set(cssVar.var, token.value)
  }

  function buildFlatMap(token: Token) {
    const { category, prop, cssVar, negative } = token.extensions
    if (!category) return

    if (!byCategory.has(category)) {
      byCategory.set(category, new Map())
    }

    const value = negative
      ? token.extensions.conditions
        ? token.originalValue
        : token.value
      : cssVar!.ref

    byCategory.get(category)!.set(prop, value)
    flatMap.set([category, prop].join("."), value)
  }

  function buildColorPalette(token: Token) {
    const { colorPalette, virtual, default: isDefault } = token.extensions
    if (!colorPalette || virtual) return

    colorPalette.roots.forEach((root) => {
      const name = formatTokenName(root)

      if (!colorPaletteMap.has(name)) {
        colorPaletteMap.set(name, new Map())
      }

      const virtualPath = replaceRootWithColorPalette(
        [...token.path],
        [...root],
      )

      const virtualName = formatTokenName(virtualPath)

      const virtualToken = getByName(virtualName)
      if (!virtualToken || !virtualToken.extensions.cssVar) return

      const { var: virtualVar } = virtualToken.extensions.cssVar

      colorPaletteMap.get(name)!.set(virtualVar, token.extensions.cssVar!.ref)

      if (isDefault && root.length === 1) {
        const colorPaletteName = formatTokenName(["colors", "colorPalette"])

        const colorPaletteToken = getByName(colorPaletteName)
        if (!colorPaletteToken) return

        const name = formatTokenName(token.path)

        const virtualToken = getByName(name)
        if (!virtualToken) return

        const keyPath = colorPalette.keys[0]?.filter(Boolean)
        if (!keyPath.length) return

        const computedName = formatTokenName(root.concat(keyPath))

        if (!colorPaletteMap.has(computedName)) {
          colorPaletteMap.set(computedName, new Map())
        }

        colorPaletteMap
          .get(computedName)!
          .set(
            colorPaletteToken.extensions.cssVar!.var,
            virtualToken.extensions.cssVar!.ref,
          )
      }
    })
  }

  let byCategoryJson: Record<string, Record<string, string>> = {}

  function setupViews() {
    allTokens.forEach((token) => {
      buildConditionMap(token)
      buildCategoryMap(token)
      buildCssVars(token)
      buildFlatMap(token)
      buildColorPalette(token)
    })

    byCategoryJson = mapToJson(byCategory)
  }

  const colorMix = (value: string, tokenFn: (path: string) => string) => {
    if (!value || typeof value !== "string") return { invalid: true, value }

    const [colorPath, rawOpacity] = value.split("/")

    if (!colorPath || !rawOpacity) {
      return { invalid: true, value: colorPath }
    }

    const colorToken = tokenFn(colorPath)
    const opacityToken = getByName(`opacity.${rawOpacity}`)?.value

    if (!opacityToken && isNaN(Number(rawOpacity))) {
      return { invalid: true, value: colorPath }
    }

    const percent = opacityToken
      ? Number(opacityToken) * 100 + "%"
      : `${rawOpacity}%`
    const color = colorToken ?? colorPath

    return {
      invalid: false,
      color,
      value: `color-mix(in srgb, ${color} ${percent}, transparent)`,
    }
  }

  const getVar = memo((value: string, fallback?: string) => {
    return flatMap.get(value) ?? fallback
  })

  const getCategoryValues = memo((category: string) => {
    return byCategoryJson[category] || null
  })

  const expandReferenceInValue = memo((value: string) => {
    return _expandReferences(value, (path) => {
      if (!path) return

      if (path.includes("/")) {
        const mix = colorMix(path, (v) => getVar(v)!)
        if (mix.invalid) {
          throw new Error("Invalid color mix at " + path + ": " + mix.value)
        }

        return mix.value
      }

      const resolved = getVar(path)
      if (resolved) return resolved

      // If the path includes an unresolved token reference, we need to escape it
      return TOKEN_PATH_REGEX.test(path) ? esc(path) : path
    })
  })

  const dictionary: TokenDictionary = {
    prefix,
    allTokens,
    tokenMap: tokenNameMap,
    registerToken,
    getByName,
    formatTokenName,
    formatCssVar,
    flatMap,
    cssVarMap,
    categoryMap,
    colorPaletteMap,
    getVar,
    getCategoryValues,
    expandReferenceInValue,
  }

  function registerTransform(...fns: TokenTransformer[]) {
    fns.forEach((fn) => {
      transforms.set(fn.name, fn)
    })
  }

  function registerMiddleware(...fns: TokenMiddleware[]) {
    middlewares.push(...fns)
  }

  function transformToken(transform: TokenTransformer, token: Token) {
    if (token.extensions.references) return
    if (isFunction(transform.match) && !transform.match(token)) return

    const fn = (v: Token) => transform.transform(v, dictionary)

    const transformed = fn(token)

    switch (true) {
      case transform.type === "extensions":
        Object.assign(token.extensions, transformed)
        break
      case transform.type === "value":
        token.value = transformed
        break
      default:
        token[transform.type] = transformed
        break
    }
  }

  function applyMiddlewares(enforce: TokenEnforcePhase) {
    middlewares.forEach((middleware) => {
      if (middleware.enforce === enforce) {
        middleware.transform(dictionary)
      }
    })
  }

  function applyTransforms(enforce: TokenEnforcePhase) {
    transforms.forEach((transform) => {
      if (transform.enforce === enforce) {
        allTokens.forEach((token) => {
          transformToken(transform, token)
        })
      }
    })
  }

  function addConditionalTokens() {
    allTokens.forEach((token) => {
      const tokens = getConditionalTokens(token)
      if (!tokens || tokens.length === 0) return
      tokens.forEach((token) => {
        registerToken(token)
      })
    })
  }

  function getTokenReferences(value: string) {
    const refs = getReferences(value)
    return refs.map((ref) => getByName(ref)).filter(Boolean) as Token[]
  }

  function addReferences() {
    allTokens.forEach((token) => {
      if (!hasReference(token.value)) return
      const references = getTokenReferences(token.value)
      token.extensions.references = references.reduce<Dict>((acc, ref) => {
        acc[ref.name] = ref
        return acc
      }, {})
    })
  }

  function expandTokenReferences() {
    allTokens.forEach((token) => {
      expandReferences(token)
    })
  }

  function build() {
    applyMiddlewares("pre")
    applyTransforms("pre")

    addConditionalTokens()

    addReferences()
    expandTokenReferences()

    applyMiddlewares("post")
    applyTransforms("post")

    setupViews()
  }

  registerTokens()
  registerTransform(...tokenTransforms)
  registerMiddleware(...tokenMiddlewares)
  build()

  return dictionary
}

function filterDefault(path: string[]) {
  if (path[0] === "DEFAULT") return path
  return path.filter((item) => item !== "DEFAULT")
}

function filterBaseCondition(path: string[]) {
  return path.filter((item) => item !== "base")
}

function getConditionalTokens(token: Token) {
  if (!token.extensions.conditions) return

  const { conditions } = token.extensions
  const tokens: Token[] = []

  walkObject(conditions, (value, path) => {
    const nextPath = filterBaseCondition(path)
    if (!nextPath.length) return

    const nextToken = structuredClone(token)

    nextToken.value = value
    nextToken.extensions.condition = nextPath.join(":")

    tokens.push(nextToken)
  })

  return tokens
}

function replaceRootWithColorPalette(path: string[], roots: string[]) {
  const startIndex = path.findIndex((_, index) =>
    roots.every(
      (rootElement, rootIndex) => path[index + rootIndex] === rootElement,
    ),
  )

  if (startIndex === -1) {
    return path
  }

  path.splice(startIndex, roots.length)
  path.splice(startIndex, 0, "colorPalette")

  return path
}

export const tokenCategories = createProps<Record<TokenCategory, any>>()([
  "aspectRatios",
  "zIndex",
  "opacity",
  "colors",
  "fonts",
  "fontSizes",
  "fontWeights",
  "lineHeights",
  "letterSpacings",
  "sizes",
  "shadows",
  "spacing",
  "radii",
  "cursor",
  "borders",
  "borderWidths",
  "borderStyles",
  "durations",
  "easings",
  "animations",
  "blurs",
  "gradients",
  "breakpoints",
  "assets",
])