/**
 * Assertion mapping for knowledge probe evaluations.
 */
import { resolveTemplatedAssertion } from "../../rubric-resolution.js";
/**
 * Map a raw knowledge probe assertion to a Promptfoo assertion.
 *
 * Tool-use assertions are rejected (knowledge probes don't use tools).
 * LLM-graded assertions receive the configured grader provider.
 * All other assertions are passed through.
 *
 * Templated `llm-rubric` assertions (those with `template` + `criteria`) go
 * through the shared rubric resolver so the compiled assertion carries
 * `metadata.dimension` — without this, the scoring engine can't classify
 * KP grader output and dimension scores collapse to zero (W0128 / DOC-2077).
 */
export function mapKnowledgeProbeAssertion(assertion, options, warnings) {
    switch (assertion.type) {
        // Standard assertions — pass through
        case "contains":
        case "contains-all":
        case "contains-any":
        case "equals":
        case "is-json":
        case "javascript":
        case "python":
        case "regex":
        case "similar":
            return {
                type: assertion.type,
                ...("value" in assertion ? { value: assertion.value } : {}),
                ...(typeof assertion.weight === "number"
                    ? { weight: assertion.weight }
                    : {}),
            };
        case "llm-rubric":
            // Templated form (template + criteria) → resolve to full rubric text
            // with dimension metadata attached.
            if ("template" in assertion && "criteria" in assertion) {
                return resolveTemplatedAssertion(assertion, options?.rubricConfig, options?.graderProvider, warnings);
            }
            // Inline value form — pass through with grader provider, no metadata.
            // Back-compat for tasks not yet migrated to the templated form.
            return {
                type: "llm-rubric",
                ...("value" in assertion ? { value: assertion.value } : {}),
                ...(typeof assertion.weight === "number"
                    ? { weight: assertion.weight }
                    : {}),
                ...(options?.graderProvider
                    ? { provider: options.graderProvider }
                    : {}),
            };
        // Other LLM-graded assertions — add grader provider
        case "g-eval":
        case "model-graded-closedqa":
        case "model-graded-factuality":
            return {
                type: assertion.type,
                ...("value" in assertion ? { value: assertion.value } : {}),
                ...(typeof assertion.weight === "number"
                    ? { weight: assertion.weight }
                    : {}),
                ...(options?.graderProvider
                    ? { provider: options.graderProvider }
                    : {}),
            };
        // Tool-use assertions are NOT valid for knowledge probes
        case "skill-used":
        case "tool-call-f1":
        case "tool-called":
        case "tool-input-matches":
        case "tool-output-matches":
            warnings.push(`Knowledge probe "${assertion.type}" assertion is not applicable — ` +
                "knowledge probes don't use tools. Assertion skipped.");
            return null;
        default:
            warnings.push(`Knowledge probe: unknown assertion type "${assertion.type}" — passed through`);
            return {
                type: assertion.type,
                ...("value" in assertion ? { value: assertion.value } : {}),
            };
    }
}
