{"version":3,"file":"templateCatalog.cjs","names":["parseWebTemplate","pluginsAtScope","parseNativeTemplate","DEFAULT_TEMPLATE_ID","splitHostId","NO_BACKDROP","DEFAULT_BACKDROP_ID"],"sources":["../../../src/web/lib/templateCatalog.ts"],"sourcesContent":["import {\n  DEFAULT_BACKDROP_ID,\n  DEFAULT_TEMPLATE_ID,\n  NO_BACKDROP,\n  parseNativeTemplate,\n  parseWebTemplate,\n  type NativeTemplateContribution,\n  type WebPluginDefinition,\n  type WebPluginScope,\n  type WebTemplateContribution,\n} from '@agimon-ai/po-core/web';\n\nimport { splitHostId } from './hostIdentity';\nimport { pluginsAtScope } from './pluginScopes';\nexport interface InstalledWebTemplate extends WebTemplateContribution {\n  pluginId: string;\n}\n\nexport interface WebTemplateDiagnostic {\n  kind: 'invalid-template' | 'duplicate-template';\n  pluginId: string;\n  message: string;\n}\n\nexport interface WebTemplateCatalog {\n  templates: InstalledWebTemplate[];\n  diagnostics: WebTemplateDiagnostic[];\n}\n\nlet bundledTemplates: readonly WebPluginDefinition[] = [];\n\n/** The host supplies its bundled recovery application, avoiding a library-to-template dependency. */\nexport function bindBundledWebTemplates(plugins: readonly WebPluginDefinition[]): void {\n  bundledTemplates = plugins;\n}\n\n/**\n * One plugin field of template contributions, parsed entry by entry; the first owner of an id wins.\n * Bundled plugins only fill gaps: an id the host already provides is skipped without a diagnostic.\n */\nfunction collectTemplates<T extends { id: string }>(\n  plugins: readonly WebPluginDefinition[],\n  field: 'templates' | 'nativeTemplates',\n  parse: (value: unknown) => T | undefined,\n  bundled: readonly WebPluginDefinition[] = [],\n): { templates: (T & { pluginId: string })[]; diagnostics: WebTemplateDiagnostic[] } {\n  const templates = new Map<string, T & { pluginId: string }>();\n  const diagnostics: WebTemplateDiagnostic[] = [];\n  const collect = (plugin: WebPluginDefinition, fillsGaps: boolean): void => {\n    let entries: unknown;\n    try {\n      entries = plugin[field];\n    } catch {\n      diagnostics.push({\n        kind: 'invalid-template',\n        pluginId: plugin.id,\n        message: `Template metadata from '${plugin.id}' could not be read.`,\n      });\n      return;\n    }\n    if (entries === undefined) return;\n    if (!Array.isArray(entries)) {\n      diagnostics.push({\n        kind: 'invalid-template',\n        pluginId: plugin.id,\n        message: `Plugin '${plugin.id}' must declare ${field} as an array.`,\n      });\n      return;\n    }\n    for (const entry of entries) {\n      let template: T | undefined;\n      try {\n        template = parse(entry);\n      } catch {\n        template = undefined;\n      }\n      if (template === undefined) {\n        diagnostics.push({\n          kind: 'invalid-template',\n          pluginId: plugin.id,\n          message: `Plugin '${plugin.id}' declares an invalid template or an unsupported template contract.`,\n        });\n        continue;\n      }\n      const owner = templates.get(template.id);\n      if (owner !== undefined && fillsGaps) continue;\n      if (owner !== undefined) {\n        diagnostics.push({\n          kind: 'duplicate-template',\n          pluginId: plugin.id,\n          message: `Template '${template.id}' is already provided by '${owner.pluginId}'; keeping the first.`,\n        });\n        continue;\n      }\n      templates.set(template.id, { ...template, pluginId: plugin.id });\n    }\n  };\n\n  for (const plugin of plugins) collect(plugin, false);\n  for (const plugin of bundled) collect(plugin, true);\n  return { templates: [...templates.values()], diagnostics };\n}\n\n/** Read scoped templates, then add any bundled template the scope does not provide. */\nexport function collectWebTemplates(\n  plugins: readonly WebPluginDefinition[],\n  scope?: WebPluginScope,\n): WebTemplateCatalog {\n  return collectTemplates(\n    plugins,\n    'templates',\n    parseWebTemplate,\n    scope === undefined ? [] : pluginsAtScope(bundledTemplates, scope),\n  );\n}\n\nexport interface InstalledNativeTemplate extends NativeTemplateContribution {\n  pluginId: string;\n}\n\nexport interface NativeTemplateCatalog {\n  templates: InstalledNativeTemplate[];\n  diagnostics: WebTemplateDiagnostic[];\n}\n\nlet bundledNativeTemplates: readonly WebPluginDefinition[] = [];\n\n/** The phone app's bundled default layout, bound by the host exactly as the cockpit binds its own. */\nexport function bindBundledNativeTemplates(plugins: readonly WebPluginDefinition[]): void {\n  bundledNativeTemplates = plugins;\n}\n\n/** Phone layouts from `nativeTemplates`, then any bundled layout the scope does not provide. */\nexport function collectNativeTemplates(\n  plugins: readonly WebPluginDefinition[],\n  scope?: WebPluginScope,\n): NativeTemplateCatalog {\n  return collectTemplates(\n    plugins,\n    'nativeTemplates',\n    parseNativeTemplate,\n    scope === undefined ? [] : pluginsAtScope(bundledNativeTemplates, scope),\n  );\n}\n\n/** The phone app's own copy of a bundled layout, valid at the global scope, for screens no host owns. */\nexport function bundledNativeTemplate(id: string): NativeTemplateContribution | undefined {\n  return collectTemplates(\n    [],\n    'nativeTemplates',\n    parseNativeTemplate,\n    pluginsAtScope(bundledNativeTemplates, 'host'),\n  ).templates.find((template) => template.id === id);\n}\n\n/** The cockpit's choice rule for the phone: configured, then the default, then the first by id. */\nexport function resolveNativeTemplate(\n  templates: readonly InstalledNativeTemplate[],\n  configured: string | undefined,\n  failed: readonly string[] = [],\n): InstalledNativeTemplate | undefined {\n  const available = templates.filter((template) => !failed.includes(template.id));\n  return (\n    available.find((template) => template.id === configured) ??\n    available.find((template) => template.id === DEFAULT_TEMPLATE_ID) ??\n    [...available].sort((left, right) => left.id.localeCompare(right.id))[0]\n  );\n}\n\n/** A missing default stays in config, since another workspace may still provide it. */\nexport function resolveWebTemplate(\n  templates: readonly InstalledWebTemplate[],\n  configured: string | undefined,\n  failed: readonly string[] = [],\n): { template: InstalledWebTemplate | undefined; warning: string | undefined } {\n  const available = templates.filter((template) => !failed.includes(template.id));\n  const preferred = available.find((template) => template.id === configured);\n  const template =\n    preferred ??\n    available.find((candidate) => candidate.id === DEFAULT_TEMPLATE_ID) ??\n    [...available].sort((left, right) => left.id.localeCompare(right.id))[0];\n  if (!template)\n    return {\n      template: undefined,\n      warning: 'No compatible web template is available. Add a template package to presets.yaml and run po sync.',\n    };\n  if (configured && !preferred)\n    return { template, warning: `Configured template '${configured}' is unavailable; showing '${template.label}'.` };\n  if (failed.length > 0)\n    return { template, warning: 'A template could not render. A different available template is being shown.' };\n  return { template, warning: undefined };\n}\n\n/**\n * A background's `web.backdrop` value: the `<pluginId>/<fillId>` of its `backdrop` fill. A cloud host's registry\n * prefixes plugin ids with its host id; config never carries that runtime prefix.\n */\nexport function backdropId(fill: { pluginId: string; id: string }): string {\n  return `${splitHostId(fill.pluginId)?.localId ?? fill.pluginId}/${fill.id}`;\n}\n\n/**\n * The one background to show, never a stack: the configured fill, else Po's bamboo, else none; `none` shows\n * none. A configured fill that is not installed stays in config, since another workspace may still provide it.\n */\nexport function resolveBackdrop<Fill extends { pluginId: string; id: string }>(\n  fills: readonly Fill[],\n  configured: string | undefined,\n): Fill | undefined {\n  if (configured === NO_BACKDROP) return undefined;\n  return (\n    fills.find((fill) => backdropId(fill) === configured) ??\n    fills.find((fill) => backdropId(fill) === DEFAULT_BACKDROP_ID)\n  );\n}\n"],"mappings":";;;;AA6BA,IAAI,mBAAmD,CAAC;;AAGxD,SAAgB,wBAAwB,SAA+C;CACrF,mBAAmB;AACrB;;;;;AAMA,SAAS,iBACP,SACA,OACA,OACA,UAA0C,CAAC,GACwC;CACnF,MAAM,4BAAY,IAAI,IAAsC;CAC5D,MAAM,cAAuC,CAAC;CAC9C,MAAM,WAAW,QAA6B,cAA6B;EACzE,IAAI;EACJ,IAAI;GACF,UAAU,OAAO;EACnB,QAAQ;GACN,YAAY,KAAK;IACf,MAAM;IACN,UAAU,OAAO;IACjB,SAAS,2BAA2B,OAAO,GAAG;GAChD,CAAC;GACD;EACF;EACA,IAAI,YAAY,KAAA,GAAW;EAC3B,IAAI,CAAC,MAAM,QAAQ,OAAO,GAAG;GAC3B,YAAY,KAAK;IACf,MAAM;IACN,UAAU,OAAO;IACjB,SAAS,WAAW,OAAO,GAAG,iBAAiB,MAAM;GACvD,CAAC;GACD;EACF;EACA,KAAK,MAAM,SAAS,SAAS;GAC3B,IAAI;GACJ,IAAI;IACF,WAAW,MAAM,KAAK;GACxB,QAAQ;IACN,WAAW,KAAA;GACb;GACA,IAAI,aAAa,KAAA,GAAW;IAC1B,YAAY,KAAK;KACf,MAAM;KACN,UAAU,OAAO;KACjB,SAAS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