import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { ensureDir, getDataDir } from '../storage/json-store.js';
import { listTemplates } from './agent.js';
function getPromptsDir() {
    return join(getDataDir(), 'prompt-templates');
}
function getMetaPath() {
    return join(getPromptsDir(), 'meta.json');
}
function readMeta() {
    const path = getMetaPath();
    if (!existsSync(path))
        return { templates: [] };
    try {
        return JSON.parse(readFileSync(path, 'utf-8'));
    }
    catch {
        return { templates: [] };
    }
}
function writeMeta(meta) {
    ensureDir(getPromptsDir());
    writeFileSync(getMetaPath(), JSON.stringify(meta, null, 2), 'utf-8');
}
export function listPromptTemplates() {
    const templates = readMeta().templates;
    const agents = listTemplates();
    return templates.map((tmpl) => ({
        ...tmpl,
        boundAgents: agents
            .filter((a) => a.systemPrompt === tmpl.content)
            .map((a) => ({ id: a.id, name: a.name || a.id, avatarUrl: a.avatarUrl })),
    }));
}
export function createPromptTemplate(name, content, storeId) {
    const meta = readMeta();
    const now = new Date().toISOString();
    const id = `prompt-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
    const tmpl = { id, name, content, storeId, createdAt: now, updatedAt: now };
    meta.templates.push(tmpl);
    writeMeta(meta);
    return tmpl;
}
export function updatePromptTemplate(id, data) {
    const meta = readMeta();
    const tmpl = meta.templates.find((t) => t.id === id);
    if (!tmpl)
        return null;
    if (data.name !== undefined)
        tmpl.name = data.name;
    if (data.content !== undefined)
        tmpl.content = data.content;
    tmpl.updatedAt = new Date().toISOString();
    writeMeta(meta);
    return tmpl;
}
export function deletePromptTemplate(id) {
    const meta = readMeta();
    const idx = meta.templates.findIndex((t) => t.id === id);
    if (idx < 0)
        return false;
    meta.templates.splice(idx, 1);
    writeMeta(meta);
    return true;
}
export function applyPromptToAgents(templateId, agentIds) {
    const meta = readMeta();
    const tmpl = meta.templates.find((t) => t.id === templateId);
    if (!tmpl)
        return 0;
    const agents = listTemplates();
    let applied = 0;
    for (const agentId of agentIds) {
        const agent = agents.find((a) => a.id === agentId);
        if (!agent)
            continue;
        const agentDir = join(getDataDir(), 'agent-templates', agent.id);
        const agentPath = join(agentDir, 'agent.json');
        if (!existsSync(agentPath))
            continue;
        const config = JSON.parse(readFileSync(agentPath, 'utf-8'));
        config.systemPrompt = tmpl.content;
        writeFileSync(agentPath, JSON.stringify(config, null, 2), 'utf-8');
        applied++;
    }
    return applied;
}
export function listAgentCandidates() {
    return listTemplates()
        .filter((a) => a.id !== 'agent-generator')
        .map((a) => ({
        id: a.id,
        name: a.name || a.id,
        avatarUrl: a.avatarUrl,
        description: a.description,
        hasSystemPrompt: !!a.systemPrompt,
    }));
}
//# sourceMappingURL=prompt-template.js.map