{"version":3,"file":"layeredConfig-CTr0Vbe8.mjs","names":["parseYaml"],"sources":["../src/services/layeredConfig/core.ts","../src/services/layeredConfig/sync.ts","../src/services/layeredConfig/index.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { parse as parseYaml } from 'yaml';\n\nimport { globalPoConfigDirectory, isGlobalPoConfigRoot } from '../config/core';\n\n/** Repository-local po configuration directory, committed to git. */\nexport const PO_DIR = '.po';\n\n/** Source identity retained for diagnostics and composition fingerprints. */\nexport interface PoConfigProvenance {\n  /** Absolute path of the declaring configuration file. */\n  filePath: string;\n  /** Directory used to resolve paths authored by that file. */\n  baseDirectory: string;\n}\n\n/** One `.po` document plus the root its relative paths resolve against. */\nexport interface PoConfigSource<TDocument> extends PoConfigProvenance {\n  /** Absolute path of the file the document was read from. */\n  filePath: string;\n  /**\n   * Where relative paths inside this document point.\n   *\n   * A repository document resolves against the repository root, so `plugins/x`\n   * keeps meaning the same thing it always has. A global document resolves\n   * against the global `.po` directory, so it can name plugins and extensions\n   * that live beside it in `~/.po` rather than inside whichever\n   * repository happens to be open.\n   */\n  baseDirectory: string;\n  /** Raw file contents, for callers that cache parsed results by source text. */\n  text: string;\n  document: TDocument;\n}\n\n/**\n * The global and repository locations of one named `.po` document, lowest\n * precedence first.\n */\nexport function poConfigCandidates(\n  fileName: string,\n  repoRoot: string,\n  homeDirectory?: string,\n  environment?: Parameters<typeof globalPoConfigDirectory>[1],\n): Array<{ filePath: string; baseDirectory: string }> {\n  const globalDirectory = globalPoConfigDirectory(homeDirectory, environment);\n  const globalCandidate = { filePath: path.join(globalDirectory, fileName), baseDirectory: globalDirectory };\n  if (isGlobalPoConfigRoot(repoRoot, homeDirectory, environment)) return [globalCandidate];\n  return [globalCandidate, { filePath: path.join(repoRoot, PO_DIR, fileName), baseDirectory: repoRoot }];\n}\n\n/** Retired file names, keyed by the file that replaced them in the same directory. */\nconst RETIRED_CONFIG_SIBLING: Readonly<Record<string, string>> = {\n  'presets.yaml': 'modes.yaml', // retired-name\n};\n\n/** The retired file that would sit beside `filePath`, for a name that replaced one. */\nexport function retiredConfigSibling(filePath: string): string | undefined {\n  const retired = RETIRED_CONFIG_SIBLING[path.basename(filePath)];\n  return retired === undefined ? undefined : path.join(path.dirname(filePath), retired);\n}\n\n/** The error for a retired file found beside its replacement's location. */\nexport function retiredConfigError(retiredPath: string): Error {\n  return new Error(\n    `${retiredPath} was renamed to presets.yaml. Rename the file and its keys ` +\n      '(majorMode to preset, defaultMajorMode to defaultPreset). ' + // retired-name\n      'If Po already created a presets.yaml next to it, replace that file.',\n  );\n}\n\n/**\n * Parses one candidate's text into a source, or undefined when the file is\n * absent (`undefined` text) or empty. An empty file parses to null and is\n * treated as an absent one, so a seeded placeholder never has to be deleted to\n * get out of the way.\n */\nexport function sourceFromText<TDocument>(\n  candidate: { filePath: string; baseDirectory: string },\n  text: string | undefined,\n): PoConfigSource<TDocument> | undefined {\n  if (text === undefined) return undefined;\n  const document = parseYaml(text) as TDocument | null | undefined;\n  if (document === null || document === undefined) return undefined;\n  return { ...candidate, text, document };\n}\n\n/**\n * Merges same-named records across sources, tagging each entry with the base\n * directory of the document that supplied it.\n *\n * Later sources win outright rather than being deep-merged: a repository that\n * redefines a name is replacing it, and a half-inherited definition whose paths\n * straddle two roots would be impossible to resolve.\n */\nexport function mergeNamedSources<TDocument, TEntry extends object>(\n  sources: ReadonlyArray<PoConfigSource<TDocument>>,\n  select: (document: TDocument) => Record<string, TEntry> | undefined,\n): Record<string, TEntry & PoConfigProvenance> {\n  const merged: Record<string, TEntry & PoConfigProvenance> = {};\n  for (const source of sources) {\n    for (const [name, entry] of Object.entries(select(source.document) ?? {})) {\n      merged[name] = {\n        ...entry,\n        baseDirectory: source.baseDirectory,\n        filePath: source.filePath,\n      };\n    }\n  }\n  return merged;\n}\n","// sync: Preserve the exported Node-only readPoConfigSources signature until phase 2.\nimport fs from 'node:fs';\n\nimport {\n  poConfigCandidates,\n  retiredConfigError,\n  retiredConfigSibling,\n  sourceFromText,\n  type PoConfigSource,\n} from './core';\n\nconst TEXT_ENCODING = 'utf8';\n\n/** @deprecated Node-only sync shim; use readPoConfigSourcesAsync. Removed in M2. */\nexport function readPoConfigSources<TDocument>(\n  fileName: string,\n  repoRoot: string,\n  homeDirectory?: string,\n): Array<PoConfigSource<TDocument>> {\n  const sources: Array<PoConfigSource<TDocument>> = [];\n  for (const candidate of poConfigCandidates(fileName, repoRoot, homeDirectory)) {\n    const retired = retiredConfigSibling(candidate.filePath);\n    if (retired !== undefined && fs.existsSync(retired)) throw retiredConfigError(retired);\n    if (!fs.existsSync(candidate.filePath)) continue;\n    const source = sourceFromText<TDocument>(candidate, fs.readFileSync(candidate.filePath, TEXT_ENCODING));\n    if (source) sources.push(source);\n  }\n  return sources;\n}\n","import { readTextIfExists, type FileSystem } from '@agimon-ai/po-core/platform';\n\nimport {\n  poConfigCandidates,\n  retiredConfigError,\n  retiredConfigSibling,\n  sourceFromText,\n  type PoConfigSource,\n} from './core';\n\ntype IoContext = Parameters<FileSystem['readTextFile']>[1];\n\n/**\n * Reads one named `.po` document from the global and repository config\n * directories over a FileSystem, lowest precedence first.\n *\n * A missing or empty file contributes nothing, so either location on its own is\n * a complete configuration, and a host with neither (a fresh edge filesystem)\n * reads no sources, as an empty Node home does. Documents are deliberately\n * returned unmerged: merging is by name, and only the caller knows what a name\n * means in its own document, so each one folds the sources itself with the\n * repository last.\n */\nexport async function readPoConfigSourcesAsync<TDocument>(\n  fs: FileSystem,\n  fileName: string,\n  repoRoot: string,\n  context: IoContext,\n  homeDirectory?: string,\n  environment?: Parameters<typeof poConfigCandidates>[3],\n): Promise<Array<PoConfigSource<TDocument>>> {\n  const sources: Array<PoConfigSource<TDocument>> = [];\n  for (const candidate of poConfigCandidates(fileName, repoRoot, homeDirectory, environment)) {\n    const retired = retiredConfigSibling(candidate.filePath);\n    if (retired !== undefined && (await readTextIfExists(fs, retired, context)) !== undefined) {\n      throw retiredConfigError(retired);\n    }\n    const source = sourceFromText<TDocument>(candidate, await readTextIfExists(fs, candidate.filePath, context));\n    if (source) sources.push(source);\n  }\n  return sources;\n}\n\nexport { PO_DIR, poConfigCandidates, mergeNamedSources, retiredConfigError, retiredConfigSibling } from './core';\nexport type { PoConfigProvenance, PoConfigSource } from './core';\nexport { readPoConfigSources } from './sync';\n"],"mappings":";;;;;;;AAOA,MAAa,SAAS;;;;;AAiCtB,SAAgB,mBACd,UACA,UACA,eACA,aACoD;CACpD,MAAM,kBAAkB,wBAAwB,eAAe,WAAW;CAC1E,MAAM,kBAAkB;EAAE,UAAU,KAAK,KAAK,iBAAiB,QAAQ;EAAG,eAAe;CAAgB;CACzG,IAAI,qBAAqB,UAAU,eAAe,WAAW,GAAG,OAAO,CAAC,eAAe;CACvF,OAAO,CAAC,iBAAiB;EAAE,UAAU,KAAK,KAAK,UAAA,OAAkB,QAAQ;EAAG,eAAe;CAAS,CAAC;AACvG;;AAGA,MAAM,yBAA2D,EAC/D,gBAAgB,aAClB;;AAGA,SAAgB,qBAAqB,UAAsC;CACzE,MAAM,UAAU,uBAAuB,KAAK,SAAS,QAAQ;CAC7D,OAAO,YAAY,KAAA,IAAY,KAAA,IAAY,KAAK,KAAK,KAAK,QAAQ,QAAQ,GAAG,OAAO;AACtF;;AAGA,SAAgB,mBAAmB,aAA4B;CAC7D,uBAAO,IAAI,MACT,GAAG,YAAY,yLAGjB;AACF;;;;;;;AAQA,SAAgB,eACd,WACA,MACuC;CACvC,IAAI,SAAS,KAAA,GAAW,OAAO,KAAA;CAC/B,MAAM,WAAWA,MAAU,IAAI;CAC/B,IAAI,aAAa,QAAQ,aAAa,KAAA,GAAW,OAAO,KAAA;CACxD,OAAO;EAAE,GAAG;EAAW;EAAM;CAAS;AACxC;;;;;;;;;AAUA,SAAgB,kBACd,SACA,QAC6C;CAC7C,MAAM,SAAsD,CAAC;CAC7D,KAAK,MAAM,UAAU,SACnB,KAAK,MAAM,CAAC,MAAM,UAAU,OAAO,QAAQ,OAAO,OAAO,QAAQ,KAAK,C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