const CANONICAL_PROMPT_SECTIONS = [
  "role",
  "scope",
  "data",
  "tools",
  "attachments",
  "mutationPolicy",
  "responseStyle",
  "overrides"
];
const RESERVED_OVERRIDE_KEYS = [
  "mutationPolicy",
  "readOnly",
  "allowedTools",
  "acceptedMediaTypes"
];
const HEADER_MAP = {
  role: "role",
  scope: "scope",
  data: "data",
  tools: "tools",
  attachments: "attachments",
  "mutation policy": "mutationPolicy",
  mutation_policy: "mutationPolicy",
  mutationpolicy: "mutationPolicy",
  "response style": "responseStyle",
  response_style: "responseStyle",
  responsestyle: "responseStyle",
  overrides: "overrides"
};
function canonicalize(key) {
  const normalized = key.trim().toLowerCase();
  if (!normalized) return null;
  return HEADER_MAP[normalized] ?? null;
}
function prettyHeader(name) {
  switch (name) {
    case "mutationPolicy":
      return "MUTATION POLICY";
    case "responseStyle":
      return "RESPONSE STYLE";
    default:
      return name.toUpperCase();
  }
}
function applyPromptOverride(template, override) {
  const baseSections = [...template.sections];
  const sections = override?.sections ?? null;
  if (!sections || typeof sections !== "object") {
    return {
      sections: baseSections,
      systemPrompt: renderPrompt(baseSections)
    };
  }
  assertNoReservedKeys(sections);
  const byCanonical = /* @__PURE__ */ new Map();
  const unknownKeys = [];
  for (const [rawKey, value] of Object.entries(sections)) {
    if (typeof value !== "string") continue;
    const trimmed = value.trim();
    if (!trimmed) continue;
    const canonical = canonicalize(rawKey);
    if (canonical) {
      const existing = byCanonical.get(canonical);
      byCanonical.set(canonical, existing ? `${existing}

${trimmed}` : trimmed);
    } else {
      unknownKeys.push({ rawKey, value: trimmed });
    }
  }
  const appended = baseSections.map((section) => {
    const addendum = byCanonical.get(section.name);
    if (!addendum) return section;
    const nextContent = section.content.trim().length === 0 ? addendum : `${section.content}

${addendum}`;
    return { ...section, content: nextContent };
  });
  const declaredNames = new Set(appended.map((s) => s.name));
  for (const [canonical, addendum] of byCanonical.entries()) {
    if (!declaredNames.has(canonical)) {
      appended.push({ name: canonical, content: addendum });
      declaredNames.add(canonical);
    }
  }
  if (unknownKeys.length > 0) {
    const newSections = unknownKeys.map(({ rawKey, value }) => ({
      name: "overrides",
      content: `[${rawKey.trim().toUpperCase()}]
${value}`
    }));
    const responseStyleIndex = appended.findIndex((s) => s.name === "responseStyle");
    const overridesIndex = appended.findIndex((s) => s.name === "overrides");
    let insertAt;
    if (overridesIndex >= 0) {
      insertAt = overridesIndex;
    } else if (responseStyleIndex >= 0) {
      insertAt = responseStyleIndex + 1;
    } else {
      insertAt = appended.length;
    }
    appended.splice(insertAt, 0, ...newSections);
  }
  return {
    sections: appended,
    systemPrompt: renderPrompt(appended)
  };
}
function renderPrompt(sections) {
  const lines = [];
  for (const section of sections) {
    const content = section.content.trim();
    if (!content) continue;
    lines.push(`[${prettyHeader(section.name)}]
${content}`);
  }
  return lines.join("\n\n");
}
function findReservedKeys(sections) {
  if (!sections || typeof sections !== "object") return [];
  const reservedSet = new Set(RESERVED_OVERRIDE_KEYS.map((key) => key.toLowerCase()));
  const hits = [];
  for (const key of Object.keys(sections)) {
    if (reservedSet.has(key.trim().toLowerCase())) {
      hits.push(key);
    }
  }
  return hits;
}
function assertNoReservedKeys(sections) {
  const hits = findReservedKeys(sections);
  if (hits.length > 0) {
    throw new PromptOverrideReservedKeyError(hits);
  }
}
class PromptOverrideReservedKeyError extends Error {
  constructor(keys) {
    super(
      `Prompt override includes reserved policy keys: ${keys.join(", ")}. Policy fields (${RESERVED_OVERRIDE_KEYS.join(", ")}) are never editable via prompt overrides.`
    );
    this.keys = keys;
    this.code = "reserved_key";
    this.name = "PromptOverrideReservedKeyError";
  }
}
function composeSystemPromptWithOverride(baseSystemPrompt, override) {
  if (!override?.sections || Object.keys(override.sections).length === 0) {
    return baseSystemPrompt;
  }
  const template = {
    id: "legacy-system-prompt",
    sections: [
      { name: "role", content: baseSystemPrompt },
      { name: "scope", content: "" },
      { name: "data", content: "" },
      { name: "tools", content: "" },
      { name: "attachments", content: "" },
      { name: "mutationPolicy", content: "" },
      { name: "responseStyle", content: "" }
    ]
  };
  const applied = applyPromptOverride(template, override);
  return applied.systemPrompt;
}
export {
  CANONICAL_PROMPT_SECTIONS,
  PromptOverrideReservedKeyError,
  RESERVED_OVERRIDE_KEYS,
  applyPromptOverride,
  composeSystemPromptWithOverride,
  findReservedKeys,
  renderPrompt
};
//# sourceMappingURL=prompt-override-merge.js.map
