{"version":3,"file":"settingsDraft.cjs","names":[],"sources":["../../../src/web/lib/settingsDraft.ts"],"sourcesContent":["import type { SettingsFieldContribution } from '@agimon-ai/po-core/web';\n\nimport type { SettingsFileScope, SettingsValueView, SettingsWriteRequest } from '../../types/settings';\n\n/**\n * What a settings page does with pending edits, kept out of the component so it\n * can be checked without a browser.\n *\n * The rules that matter are all here: a field is only editable at a scope the\n * key is actually read from, a cleared field removes the key rather than\n * writing an empty value the parser rejects, and numeric drafts retain their\n * type when the page combines these edits into one atomic save.\n */\n\nconst KEY_SEPARATOR = '.';\n\nexport function settingsKeyOf(field: SettingsFieldContribution): string {\n  return field.keyPath.join(KEY_SEPARATOR);\n}\n\n/** Why a field cannot be edited at the selected scope, or nothing. */\nexport function settingsLockedReason(\n  view: SettingsValueView | undefined,\n  scope: SettingsFileScope,\n): string | undefined {\n  if (view === undefined || view.scope === 'both' || view.scope === scope) return undefined;\n  // User-facing copy keeps 'host' until the copy pass; the value is the host tier.\n  return view.scope === 'host' ? 'host only' : 'repository only';\n}\n\nexport interface PlannedWritesInput {\n  fields: readonly SettingsFieldContribution[];\n  /** Pending edits by dotted key; null clears the key. */\n  drafts: Readonly<Record<string, string | null>>;\n  scope: SettingsFileScope;\n  repoRoot: string;\n  /** The target file's hash when the page last read it. */\n  startingHash: string;\n}\n\n/**\n * The typed edits a save combines into one request with the file's read hash.\n * Keeping contribution order makes the result predictable for callers.\n */\nexport function plannedSettingsWrites(input: PlannedWritesInput): SettingsWriteRequest[] {\n  return input.fields\n    .filter((field) => settingsKeyOf(field) in input.drafts)\n    .map((field) => {\n      const draft = input.drafts[settingsKeyOf(field)] ?? null;\n      const numeric = field.kind === 'number' && draft !== null && draft.trim() !== '' ? Number(draft) : undefined;\n      return {\n        repoRoot: input.repoRoot,\n        scope: input.scope,\n        keyPath: field.keyPath,\n        value: numeric !== undefined && Number.isFinite(numeric) ? numeric : draft,\n        expectedHash: input.startingHash,\n      };\n    });\n}\n\n/** Whether a save can be attempted at all: something changed, and it can land somewhere. */\nexport function canSaveSettings(input: { dirty: number; scope: SettingsFileScope; repoRoot: string }): boolean {\n  if (input.dirty === 0) return false;\n  // A repository write needs a repository; a host one never does.\n  return input.scope === 'host' || input.repoRoot !== '';\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAM,gBAAgB;AAEtB,SAAgB,cAAc,OAA0C;CACtE,OAAO,MAAM,QAAQ,KAAK,aAAa;AACzC;;AAGA,SAAgB,qBACd,MACA,OACoB;CACpB,IAAI,SAAS,KAAA,KAAa,KAAK,UAAU,UAAU,KAAK,UAAU,OAAO,OAAO,KAAA;CAEhF,OAAO,KAAK,UAAU,SAAS,cAAc;AAC/C;;;;;AAgBA,SAAgB,sBAAsB,OAAmD;CACvF,OAAO,MAAM,OACV,QAAQ,UAAU,cAAc,KAAK,KAAK,MAAM,MAAM,CAAC,CACvD,KAAK,UAAU;EACd,MAAM,QAAQ,MAAM,OAAO,cAAc,KAAK,MAAM;EACpD,MAAM,UAAU,MAAM,SAAS,YAAY,UAAU,QAAQ,MAAM,KAAK,MAAM,KAAK,OAAO,KAAK,IAAI,KAAA;EACnG,OAAO;GACL,UAAU,MAAM;GAChB,OAAO,MAAM;GACb,SAAS,MAAM;GACf,OAAO,YAAY,KAAA,KAAa,OAAO,SAAS,OAAO,IAAI,UAAU;GACrE,cAAc,MAAM;EACtB;CACF,CAAC;AACL;;AAGA,SAAgB,gBAAgB,OAA+E;CAC7G,IAAI,MAAM,UAAU,GAAG,OAAO;CAE9B,OAAO,MAAM,UAAU,UAAU,MAAM,aAAa;AACtD"}