import { describeActionForVoiceTool, describeActionInputFieldForVoice, getActionVoiceWorkflowNotes, isVoicePromptHotPathSpec, } from '@happier-dev/protocol';
function normalizeText(value) {
    return typeof value === 'string' ? value.trim() : '';
}
function listStaticOptionValues(field) {
    const options = Array.isArray(field.options) ? field.options : [];
    const seen = new Set();
    const out = [];
    for (const option of options) {
        const value = normalizeText(option?.value);
        if (!value || seen.has(value))
            continue;
        seen.add(value);
        out.push(value);
    }
    return out;
}
function describeRequirement(field) {
    if (field.required === true)
        return 'required';
    if (field.requiredWhen || field.visibleWhen)
        return 'conditional';
    return 'optional';
}
function formatFieldSummary(spec, field, availability) {
    const path = normalizeText(field.path);
    if (!path)
        return null;
    const summary = describeActionInputFieldForVoice(spec, field, availability);
    const staticOptions = listStaticOptionValues(field);
    const optionSuffix = staticOptions.length > 0 ? ` one of ${staticOptions.join(' | ')}` : '';
    const widget = normalizeText(field.widget);
    const widgetSuffix = widget ? `; ${widget}` : '';
    return `    - ${path} (${describeRequirement(field)}${widgetSuffix}${optionSuffix}): ${summary}`;
}
function formatToolDocumentation(spec, toolName, invocationLabel, availability) {
    const desc = normalizeText(describeActionForVoiceTool(spec)) || toolName;
    const argsExample = normalizeText(spec.examples?.voice?.argsExample) || '{}';
    const lines = [`- ${toolName}: ${desc} ${invocationLabel} ${argsExample}.`];
    const workflowNotes = getActionVoiceWorkflowNotes(spec.id, availability);
    if (workflowNotes.length > 0) {
        lines.push(`  Use: ${workflowNotes.join(' ')}`);
    }
    const fields = Array.isArray(spec.inputHints?.fields) ? spec.inputHints?.fields : [];
    const fieldLines = fields.map((field) => formatFieldSummary(spec, field, availability)).filter(Boolean);
    if (fieldLines.length > 0) {
        lines.push('  Fields:');
        lines.push(...fieldLines);
    }
    return lines;
}
export function buildVoiceToolDocumentationLines(specs, params) {
    const disabled = new Set((params.disabledActionIds ?? []).map((value) => normalizeText(value)).filter(Boolean));
    const enabledSpecs = specs.filter((spec) => isVoicePromptHotPathSpec(spec) && !disabled.has(spec.id));
    const availability = {
        disabledActionIds: params.disabledActionIds,
        availableActionIds: specs.filter((spec) => !disabled.has(spec.id)).map((spec) => spec.id),
    };
    const out = [];
    for (const spec of enabledSpecs) {
        const toolName = normalizeText(spec.bindings?.voiceClientToolName);
        if (!toolName)
            continue;
        out.push(...formatToolDocumentation(spec, toolName, params.invocationLabel, availability));
    }
    return out;
}
export function buildVoiceDiscoveryChecklistLines(specs, params) {
    const disabled = new Set((params?.disabledActionIds ?? []).map((value) => normalizeText(value)).filter(Boolean));
    const enabledSpecs = specs.filter((spec) => !disabled.has(spec.id));
    const availability = {
        disabledActionIds: params?.disabledActionIds,
        availableActionIds: enabledSpecs.map((spec) => spec.id),
    };
    const seen = new Set();
    const out = [];
    for (const spec of enabledSpecs) {
        for (const note of getActionVoiceWorkflowNotes(spec.id, availability)) {
            const normalized = normalizeText(note);
            if (!normalized || seen.has(normalized))
                continue;
            seen.add(normalized);
            out.push(`- ${normalized}`);
        }
    }
    return out;
}
//# sourceMappingURL=voiceToolDocumentation.js.map