// figmaSnapshotAdapter.ts — Figma-snapshot source seam for QI ingestion (Epic #750, Issue #754).
//
// Two server-tier seams the pure-domain ingestion cannot own:
//
//   1. The snapshot LOADER — reads the immutable Figma Snapshot evidence record for a runId through
//      the existing keiko-evidence store (`createNodeFigmaSnapshotStore(evidenceDir).load`). It
//      reads ONLY the stored snapshot and never contacts Figma. Returns `undefined` when no
//      evidence dir is configured, so the ingestion layer rejects the source with a coded error.
//
//   2. The capability-routed VISION hint provider — consults `resolveQiMultimodalSelection` (#810)
//      to decide whether a multimodal model is available; when one is, it MAY call an injected
//      `visionCall` to recover image-derived semantics, returning them as additive hints. There is
//      NO hard-coded model id. On "unavailable", a thrown call, or a non-array/garbage result the
//      provider returns `[]`, so the source degrades silently to the deterministic IR-only baseline.
//      No model port is wired here yet, so the default provider is IR-only by construction — the
//      seam is ready for the multimodal port without any further refactor of the ingestion path.
import { createNodeFigmaSnapshotStore, } from "@oscharko-dev/keiko-evidence";
import { resolveQiMultimodalSelection } from "./modelSelection.js";
/**
 * Build the snapshot loader. Returns `undefined` when `deps.evidenceDir` is not configured so the
 * ingestion layer maps an attempted figma-snapshot source to QI_FIGMA_SNAPSHOT_UNAVAILABLE.
 */
export function makeFigmaSnapshotLoader(deps) {
    const evidenceDir = deps.evidenceDir;
    if (evidenceDir === undefined || evidenceDir.length === 0)
        return undefined;
    const store = createNodeFigmaSnapshotStore(evidenceDir);
    return (snapshotRunId) => {
        try {
            return store.load(snapshotRunId);
        }
        catch {
            // A malformed / unreadable record is treated as "not found"; the caller emits a coded error.
            return undefined;
        }
    };
}
function sanitiseCallResult(value) {
    if (!Array.isArray(value))
        return [];
    return value.filter((entry) => typeof entry === "string");
}
/**
 * Build a capability-routed vision hint provider. When no multimodal capability is configured (or no
 * `visionCall` is injected), every request returns `[]` — the deterministic IR-only baseline path.
 * When a multimodal model IS available, the provider routes the call through it; any thrown error or
 * garbage result is swallowed to `[]` so a misbehaving model can never break the run or override the
 * structural baseline. No model id is hard-coded — selection is via `resolveQiMultimodalSelection`.
 */
export function makeFigmaVisionHintProvider(deps, visionCall) {
    const selection = resolveQiMultimodalSelection(deps);
    if (selection.kind === "unavailable" || visionCall === undefined) {
        return () => [];
    }
    const { modelId } = selection;
    return (request) => {
        try {
            return sanitiseCallResult(visionCall(request, modelId));
        }
        catch {
            return [];
        }
    };
}
