import { BackendTargetRefSchema } from '../backendTargets/backendTargetRef.js';
import { buildExecutionRunsGuidanceBlockV1 } from './executionRunsGuidanceV1.js';
import { buildMemoryRecallGuidanceBlockV1 } from './memoryRecallGuidanceV1.js';
import { buildPromptPlanV1, renderPromptPlanV1 } from './promptPlanV1.js';
import { HAPPIER_BASE_SYSTEM_PROMPT_V1 } from './systemPromptBaseV1.js';
function coerceExecutionRunsGuidanceEntriesV1(raw) {
    if (!Array.isArray(raw))
        return [];
    const out = [];
    for (const item of raw) {
        if (!item || typeof item !== 'object' || Array.isArray(item))
            continue;
        const id = typeof item.id === 'string'
            ? String(item.id).trim()
            : '';
        const description = typeof item.description === 'string'
            ? String(item.description)
            : '';
        if (!id || !description.trim())
            continue;
        const title = typeof item.title === 'string'
            ? String(item.title).trim()
            : '';
        const enabled = typeof item.enabled === 'boolean'
            ? Boolean(item.enabled)
            : undefined;
        const suggestedIntentRaw = typeof item.suggestedIntent === 'string'
            ? String(item.suggestedIntent).trim()
            : '';
        const suggestedIntent = suggestedIntentRaw === 'review' || suggestedIntentRaw === 'plan' || suggestedIntentRaw === 'delegate'
            ? suggestedIntentRaw
            : undefined;
        const suggestedBackendTargetParsed = BackendTargetRefSchema.safeParse(item.suggestedBackendTarget);
        const suggestedModelId = typeof item.suggestedModelId === 'string'
            ? String(item.suggestedModelId).trim()
            : '';
        const exampleToolCallsRaw = item.exampleToolCalls;
        const exampleToolCalls = Array.isArray(exampleToolCallsRaw)
            ? exampleToolCallsRaw
                .filter((value) => typeof value === 'string')
                .map((value) => value.trim())
                .filter(Boolean)
            : [];
        out.push({
            id,
            description,
            ...(title ? { title } : {}),
            ...(typeof enabled === 'boolean' ? { enabled } : {}),
            ...(suggestedIntent ? { suggestedIntent } : {}),
            ...(suggestedBackendTargetParsed.success ? { suggestedBackendTarget: suggestedBackendTargetParsed.data } : {}),
            ...(suggestedModelId ? { suggestedModelId } : {}),
            ...(exampleToolCalls.length > 0 ? { exampleToolCalls } : {}),
        });
    }
    return out;
}
export function buildCodingSessionPromptPlanBaseV1(args) {
    const base = typeof args.base === 'string' ? args.base : HAPPIER_BASE_SYSTEM_PROMPT_V1;
    const settings = args.settings && typeof args.settings === 'object' && !Array.isArray(args.settings)
        ? args.settings
        : null;
    const blocks = [{
            id: 'coding.base',
            scope: 'session',
            text: base.trim(),
        }];
    if (args.memoryRecallGuidanceEnabled === true) {
        blocks.push({
            id: 'coding.memory_recall',
            scope: 'session',
            text: buildMemoryRecallGuidanceBlockV1('generic'),
        });
    }
    if (!args.executionRunsFeatureEnabled) {
        return buildPromptPlanV1({ modality: 'coding', blocks });
    }
    if (settings?.executionRunsGuidanceEnabled !== true) {
        return buildPromptPlanV1({ modality: 'coding', blocks });
    }
    const maxCharsRaw = settings?.executionRunsGuidanceMaxChars;
    const maxChars = typeof maxCharsRaw === 'number' && Number.isFinite(maxCharsRaw) && maxCharsRaw >= 200
        ? Math.floor(maxCharsRaw)
        : 4_000;
    const entries = coerceExecutionRunsGuidanceEntriesV1(settings?.executionRunsGuidanceEntries);
    const guidance = buildExecutionRunsGuidanceBlockV1({ entries, maxChars }).text;
    if (guidance) {
        blocks.push({
            id: 'coding.execution_runs',
            scope: 'session',
            text: guidance,
        });
    }
    return buildPromptPlanV1({ modality: 'coding', blocks });
}
export function buildAppendSystemPromptBaseV1(args) {
    return renderPromptPlanV1(buildCodingSessionPromptPlanBaseV1(args));
}
//# sourceMappingURL=buildAppendSystemPromptBaseV1.js.map