// Runtime Artifacts module — generic by-id artifact bytes retrieval.
//
// Authority: S-RUNTIME-111 (.nimi/spec/sdk/kernel/runtime-contract.md)
// Underlying contract: K-AGCORE-053 (.nimi/spec/runtime/kernel/runtime-artifact-contract.md)
// Proto: proto/runtime/v1/artifact_service.proto (RuntimeArtifactService.ReadArtifactBytes)
//
// Use cases:
//   - avatar app reads voice_playback_requested.audio_artifact_id bytes for
//     wLipSync analysis + AudioContext decode + speaker output.
//   - future image/video/music consumers read by id without depending on
//     ScenarioJob (artifacts may originate from realtime, cache, streaming
//     TTS, local voice engine, or user upload).
//
// Orthogonal to:
//   - runtime.media.* typed projections (S-RUNTIME-073 via getScenarioArtifacts)
//   - runtime voice asset library (getVoiceAsset)
import { ReasonCode } from '../types/index.js';
import { createNimiError } from './errors.js';
/**
 * Construct the RuntimeArtifactsModule with the Runtime class internal context.
 * Called from Runtime constructor (per S-RUNTIME-111: Runtime class
 * `readonly artifacts` field, NOT a singleton const export).
 */
export function createRuntimeArtifactsModule(input) {
    const { ctx } = input;
    return {
        async readBytes(req, optionsValue) {
            const artifactId = typeof req.artifactId === 'string' ? req.artifactId.trim() : '';
            if (!artifactId) {
                throw createNimiError({
                    message: 'runtime.artifacts.readBytes requires non-empty artifactId',
                    reasonCode: ReasonCode.ARTIFACT_INVALID_INPUT,
                    actionHint: 'pass_runtime_emitted_artifact_id',
                    source: 'sdk',
                });
            }
            const response = await ctx.invokeWithClient(async (client) => client.artifact.readArtifactBytes({ artifactId }, optionsValue));
            if (req.expectedMimePrefix) {
                const prefix = req.expectedMimePrefix.toLowerCase();
                if (!response.mimeType.toLowerCase().startsWith(prefix)) {
                    throw createNimiError({
                        message: `runtime artifact mime "${response.mimeType}" does not match expected prefix "${prefix}"`,
                        reasonCode: ReasonCode.ARTIFACT_MIME_MISMATCH,
                        actionHint: 'verify_runtime_provider_mime_type',
                        source: 'runtime',
                    });
                }
            }
            return {
                bytes: response.bytes.buffer.slice(response.bytes.byteOffset, response.bytes.byteOffset + response.bytes.byteLength),
                mimeType: response.mimeType,
                sizeBytes: Number(response.sizeBytes),
                mimeInferred: response.mimeInferred,
            };
        },
    };
}
//# sourceMappingURL=runtime-artifacts.js.map