{"version":3,"file":"resolveTheme.js","names":["resolver"],"sources":["../../../src/highlighter/themes/resolveTheme.ts"],"sourcesContent":["import { bundledThemes, normalizeTheme } from 'shiki';\n\nimport type { DiffsThemeNames, ThemeRegistrationResolved } from '../../types';\nimport { isWorkerContext } from '../../utils/isWorkerContext';\nimport {\n  RegisteredCustomThemes,\n  ResolvedThemes,\n  ResolvingThemes,\n} from './constants';\n\nexport async function resolveTheme(\n  themeName: DiffsThemeNames\n): Promise<ThemeRegistrationResolved> {\n  if (isWorkerContext()) {\n    throw new Error(\n      `resolveTheme(\"${themeName}\") cannot be called from a worker context. ` +\n        'Themes must be pre-resolved on the main thread and passed to the worker via the resolvedLanguages parameter.'\n    );\n  }\n\n  const resolver = ResolvingThemes.get(themeName);\n  if (resolver != null) {\n    return resolver;\n  }\n\n  try {\n    const loader =\n      RegisteredCustomThemes.get(themeName) ??\n      bundledThemes[themeName as keyof typeof bundledThemes];\n\n    if (loader == null) {\n      throw new Error(`resolveTheme: No valid loader for ${themeName}`);\n    }\n\n    const resolver = loader().then((result) => {\n      return normalizeAndCacheResolvedTheme(\n        themeName,\n        ('default' in result\n          ? result.default\n          : result) as ThemeRegistrationResolved\n      );\n    });\n\n    ResolvingThemes.set(themeName, resolver);\n    const theme = await resolver;\n    if (theme.name !== themeName) {\n      throw new Error(\n        `resolvedTheme: themeName: ${themeName} does not match theme.name: ${theme.name}`\n      );\n    }\n    ResolvedThemes.set(theme.name, theme);\n    return theme;\n  } finally {\n    ResolvingThemes.delete(themeName);\n  }\n}\n\nfunction normalizeAndCacheResolvedTheme(\n  themeName: string,\n  themeData: ThemeRegistrationResolved\n): ThemeRegistrationResolved {\n  const resolvedTheme = ResolvedThemes.get(themeName);\n  if (resolvedTheme != null) {\n    return resolvedTheme;\n  }\n  themeData = normalizeTheme(themeData);\n  ResolvedThemes.set(themeName, themeData);\n  return themeData;\n}\n"],"mappings":";;;;;AAUA,eAAsB,aACpB,WACoC;AACpC,KAAI,iBAAiB,CACnB,OAAM,IAAI,MACR,iBAAiB,UAAU,yJAE5B;CAGH,MAAM,WAAW,gBAAgB,IAAI,UAAU;AAC/C,KAAI,YAAY,KACd,QAAO;AAGT,KAAI;EACF,MAAM,SACJ,uBAAuB,IAAI,UAAU,IACrC,cAAc;AAEhB,MAAI,UAAU,KACZ,OAAM,IAAI,MAAM,qCAAqC,YAAY;EAGnE,MAAMA,aAAW,QAAQ,CAAC,MAAM,WAAW;AACzC,UAAO,+BACL,WACC,aAAa,SACV,OAAO,UACP,OACL;IACD;AAEF,kBAAgB,IAAI,WAAWA,WAAS;EACxC,MAAM,QAAQ,MAAMA;AACpB,MAAI,MAAM,SAAS,UACjB,OAAM,IAAI,MACR,6BAA6B,UAAU,8BAA8B,MAAM,OAC5E;AAEH,iBAAe,IAAI,MAAM,MAAM,MAAM;AACrC,SAAO;WACC;AACR,kBAAgB,OAAO,UAAU;;;AAIrC,SAAS,+BACP,WACA,WAC2B;CAC3B,MAAM,gBAAgB,eAAe,IAAI,UAAU;AACnD,KAAI,iBAAiB,KACnB,QAAO;AAET,aAAY,eAAe,UAAU;AACrC,gBAAe,IAAI,WAAW,UAAU;AACxC,QAAO"}