{"version":3,"file":"css.cjs","names":[],"sources":["../../src/utils/css.ts"],"sourcesContent":["/**\n * CSS tagged template utility and CSSStyleSheet caching.\n */\n\nimport { error } from '../_dev';\nimport { makeBrand } from './brand';\n\nexport type CSSResult = {\n  content: string;\n  toString(): string;\n};\n\nconst cssResultBrand = makeBrand<CSSResult>('ore:css-result');\n\nexport const isCssResult = cssResultBrand.is;\n\nconst cssResultToString = function (this: CSSResult): string {\n  return this.content;\n};\n\nexport const css = (strings: TemplateStringsArray, ...values: Array<CSSResult | string | number>): CSSResult => {\n  let content = '';\n\n  for (let i = 0; i < strings.length; i++) {\n    content += strings[i];\n\n    if (i < values.length) {\n      const v = values[i];\n\n      content += isCssResult(v) ? v.content : String(v);\n    }\n  }\n\n  return cssResultBrand.stamp({ content: content.trim(), toString: cssResultToString });\n};\n\nconst stylesheetStringCache = new Map<string, CSSStyleSheet>();\nconst STYLESHEET_CACHE_LIMIT = 256;\n\n/** @internal Clear the stylesheet cache. Used for test isolation and HMR. */\nexport const _clearStylesheetCache = (): void => {\n  stylesheetStringCache.clear();\n};\n\n/** @internal Visible to cache-behavior tests. */\nexport const _getStylesheetCacheSize = (): number => stylesheetStringCache.size;\n\nexport const loadStylesheet = (style: string | CSSStyleSheet | CSSResult): CSSStyleSheet => {\n  if (style instanceof CSSStyleSheet) return style;\n\n  const cssText = typeof style === 'string' ? style : style.content;\n  const cached = stylesheetStringCache.get(cssText);\n\n  if (cached) {\n    // Refresh recency so styles repeatedly adopted by components survive eviction.\n    stylesheetStringCache.delete(cssText);\n    stylesheetStringCache.set(cssText, cached);\n\n    return cached;\n  }\n\n  const sheet = new CSSStyleSheet();\n\n  try {\n    sheet.replaceSync(cssText);\n  } catch (err) {\n    // Deliberately not cached: a broken sheet served from cache would fail silently\n    // for every subsequent caller with no further error.\n    error('Style sheet replace failed', err);\n\n    return sheet;\n  }\n\n  stylesheetStringCache.set(cssText, sheet);\n\n  if (stylesheetStringCache.size > STYLESHEET_CACHE_LIMIT) {\n    const oldestKey = stylesheetStringCache.keys().next().value;\n\n    if (oldestKey !== undefined) stylesheetStringCache.delete(oldestKey);\n  }\n\n  return sheet;\n};\n"],"mappings":"uBAYA,IAAM,uBAAiB,CAAA,CAAA,UAAqB,gBAAgB,EAE/C,EAAc,EAAe,GAEpC,EAAoB,UAAmC,CAC3D,OAAO,KAAK,OACd,EAEa,GAAO,EAA+B,GAAG,IAA0D,CAC9G,IAAI,EAAU,GAEd,IAAK,IAAI,EAAI,EAAG,EAAI,EAAQ,OAAQ,IAGlC,GAFA,GAAW,EAAQ,GAEf,EAAI,EAAO,OAAQ,CACrB,IAAM,EAAI,EAAO,GAEjB,GAAW,EAAY,CAAC,EAAI,EAAE,QAAU,OAAO,CAAC,CAClD,CAGF,OAAO,EAAe,MAAM,CAAE,QAAS,EAAQ,KAAK,EAAG,SAAU,CAAkB,CAAC,CACtF,EAEM,EAAwB,IAAI,IAC5B,EAAyB,IAGlB,MAAoC,CAC/C,EAAsB,MAAM,CAC9B,EAKa,EAAkB,GAA6D,CAC1F,GAAI,aAAiB,cAAe,OAAO,EAE3C,IAAM,EAAU,OAAO,GAAU,SAAW,EAAQ,EAAM,QACpD,EAAS,EAAsB,IAAI,CAAO,EAEhD,GAAI,EAKF,OAHA,EAAsB,OAAO,CAAO,EACpC,EAAsB,IAAI,EAAS,CAAM,EAElC,EAGT,IAAM,EAAQ,IAAI,cAElB,GAAI,CACF,EAAM,YAAY,CAAO,CAC3B,MAAc,CAKZ,OAAO,CACT,CAIA,GAFA,EAAsB,IAAI,EAAS,CAAK,EAEpC,EAAsB,KAAO,EAAwB,CACvD,IAAM,EAAY,EAAsB,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,MAElD,IAAc,IAAA,IAAW,EAAsB,OAAO,CAAS,CACrE,CAEA,OAAO,CACT"}