import { decodeBase64 } from '../crypto/base64.js';
import { PromptBundleBodyV1Schema } from './promptBundleSchemas.js';
import { PromptDocBodyV1Schema } from './promptDocV2.js';
function readSkillMarkdownFromPromptBundleBody(body) {
    const entry = body.entries.find((candidate) => candidate.path === 'SKILL.md');
    if (!entry)
        return null;
    if (entry.contentKind !== 'utf8')
        return null;
    try {
        return new TextDecoder().decode(decodeBase64(entry.contentBase64, 'base64'));
    }
    catch {
        return null;
    }
}
function listSystemAppendEntries(params) {
    const surfaceEntries = params.surface === 'voice'
        ? (params.stacks.surfaces.voice ?? [])
        : (params.stacks.surfaces.coding ?? []);
    const profileId = typeof params.profileId === 'string' ? params.profileId.trim() : '';
    const profile = profileId ? (params.stacks.surfaces.profilesById?.[profileId] ?? []) : [];
    return [...surfaceEntries, ...profile].filter((entry) => {
        if (!entry.enabled)
            return false;
        return entry.placement === 'system_append' || entry.placement === 'skill_instructions';
    });
}
function parseJsonBodySafe(bodyRaw) {
    try {
        return JSON.parse(bodyRaw);
    }
    catch {
        return null;
    }
}
export async function resolvePromptStackSystemAppendBlocksV1(args) {
    const stacks = args.promptStacksV1;
    if (!stacks)
        return [];
    const entries = listSystemAppendEntries({
        stacks,
        surface: args.surface,
        profileId: args.profileId,
    });
    if (entries.length === 0)
        return [];
    const out = [];
    for (const entry of entries) {
        const artifactId = entry.ref.artifactId;
        if (!artifactId)
            continue;
        const bodyRaw = await args.readArtifactBody(artifactId);
        if (typeof bodyRaw !== 'string')
            continue;
        let text = null;
        if (entry.ref.kind === 'doc') {
            const bodyJson = parseJsonBodySafe(bodyRaw);
            if (bodyJson == null)
                continue;
            const parsed = PromptDocBodyV1Schema.safeParse(bodyJson);
            if (!parsed.success)
                continue;
            text = parsed.data.markdown;
        }
        else if (entry.ref.kind === 'bundle') {
            const bodyJson = parseJsonBodySafe(bodyRaw);
            if (bodyJson == null)
                continue;
            const parsed = PromptBundleBodyV1Schema.safeParse(bodyJson);
            if (!parsed.success)
                continue;
            text = readSkillMarkdownFromPromptBundleBody(parsed.data);
        }
        else {
            continue;
        }
        const trimmed = typeof text === 'string' ? text.trim() : '';
        if (!trimmed)
            continue;
        if (typeof entry.maxChars === 'number' && Number.isFinite(entry.maxChars) && entry.maxChars > 0 && trimmed.length > entry.maxChars) {
            out.push(trimmed.slice(0, Math.floor(entry.maxChars)));
            continue;
        }
        out.push(trimmed);
    }
    return out;
}
//# sourceMappingURL=resolvePromptStackSystemAppendBlocksV1.js.map