import { get, put } from "./api"; export interface Template { id: string; name: string; text: string; } export interface TemplateLists { personal: Template[]; global: Template[]; can_edit_global: boolean; } export const loadTemplates = () => get("/templates"); export const saveTemplates = (scope: "personal" | "global", list: Template[]) => put(`/templates/${scope}`, list); /** Placeholders like {{file}} or {{goal: what should change}} (name, optional hint). */ export function placeholders(text: string): { name: string; hint: string }[] { const seen = new Map(); for (const m of text.matchAll(/\{\{\s*([^}:]+?)\s*(?::\s*([^}]*?))?\s*\}\}/g)) { if (!seen.has(m[1])) seen.set(m[1], m[2] ?? ""); } return [...seen].map(([name, hint]) => ({ name, hint })); } export function fill(text: string, values: Record): string { return text.replace(/\{\{\s*([^}:]+?)\s*(?::\s*([^}]*?))?\s*\}\}/g, (_, name: string) => values[name] ?? ""); }