/**
 * Port: RunArtifactReader — reads a run's manifest and artifact payloads from
 * external object storage (D0091).
 *
 * The GCS adapter (W-bigquery-fact-store-adapter slice) implements this over
 * `gs://ailf-artifacts`; the `InMemoryRunArtifactReader` below is the test
 * double. The fact-loader engine talks only to this port, so it stays pure of
 * I/O specifics.
 *
 * Semantics:
 *   - `readManifest` throws when the manifest is missing or unparseable. The
 *     engine isolates per run, so a throw skips-and-reports that one run and
 *     the batch continues.
 *   - `readArtifact` returns the parsed (JSON-deserialized) entries for one
 *     artifact type. A type the run never wrote yields an empty list, not a
 *     throw — absence is normal (a partial run). A throw means the object was
 *     present but could not be read/parsed, which skips the run.
 *
 * @see docs/decisions/D0091-bigquery-analytics-fact-loader.md
 * @see packages/core/src/ports/artifact-writer.ts — the write-side counterpart
 */
/**
 * In-memory {@link RunArtifactReader} for tests and local dry-runs. Seed runs
 * with {@link InMemoryRunArtifactReader.seed}; an unseeded run id throws from
 * `readManifest`, mirroring a missing manifest in GCS.
 */
export class InMemoryRunArtifactReader {
    runs = new Map();
    constructor(runs) {
        if (runs) {
            for (const [runId, run] of runs)
                this.runs.set(runId, run);
        }
    }
    /** Add or replace a run's seed data. Chainable. */
    seed(runId, run) {
        this.runs.set(runId, run);
        return this;
    }
    async readManifest(runId) {
        const run = this.runs.get(runId);
        if (!run) {
            throw new Error(`no manifest for run ${runId}`);
        }
        if (run.failManifest !== undefined) {
            throw new Error(run.failManifest);
        }
        return run.manifest;
    }
    async readArtifact(runId, type) {
        const run = this.runs.get(runId);
        if (!run) {
            throw new Error(`no manifest for run ${runId}`);
        }
        if (run.failArtifacts?.includes(type)) {
            throw new Error(`artifact ${type} for run ${runId} could not be read`);
        }
        return run.artifacts?.[type] ?? [];
    }
}
