/**
 * Module-level helpers used by RuntimeHost.open() and the constructor.
 *
 * Extracted from the monolithic runtime host as part of the
 * TypeScript migration.
 *
 * @module domain/runtimeHelpers
 */
import InMemoryBlobStorageAdapter from "./utils/defaultBlobStorage.js";
import WarpError from "./errors/WarpError.js";
import { LEGACY_EXTERNAL_PATCH_STORAGE, LEGACY_GIT_BLOB_PATCH_STORAGE, } from "../ports/CommitMessageCodecPort.js";
export const DEFAULT_ADJACENCY_CACHE_SIZE = 3;
/**
 * Resolves blob storage from an explicit injection, an adapter capability,
 * or the in-memory fallback used by browser/test paths.
 */
export async function resolveBlobStorage(blobStorage, persistence) {
    if (blobStorage !== undefined && blobStorage !== null) {
        return blobStorage;
    }
    if (typeof persistence.createRuntimeBlobStorage === 'function') {
        return await persistence.createRuntimeBlobStorage();
    }
    return new InMemoryBlobStorageAdapter();
}
/**
 * Resolves the default storage route for newly written patches.
 */
export function resolvePatchWriteStorage(persistence, patchBlobStorage) {
    if (typeof persistence.defaultPatchWriteStorage === 'function') {
        return persistence.defaultPatchWriteStorage();
    }
    return patchBlobStorage !== undefined && patchBlobStorage !== null
        ? LEGACY_EXTERNAL_PATCH_STORAGE
        : LEGACY_GIT_BLOB_PATCH_STORAGE;
}
/**
 * Resolves an IndexStorePort: uses the provided instance if present,
 * otherwise auto-constructs a CborIndexStoreAdapter.
 */
export async function resolveIndexStore(indexStore, deps) {
    if (indexStore !== undefined && indexStore !== null) {
        return indexStore;
    }
    const { CborIndexStoreAdapter } = await import(
    /* webpackIgnore: true */ "../infrastructure/adapters/CborIndexStoreAdapter.js");
    return new CborIndexStoreAdapter(deps);
}
/**
 * Constructs an EffectPipeline from an array of sinks and an optional externalization lens.
 */
export async function buildEffectPipeline(sinks, lens) {
    const multMod = await import("./services/MultiplexSink.js");
    const effMod = await import("./services/EffectPipeline.js");
    const mux = new multMod.MultiplexSink();
    for (const sink of sinks) {
        mux.addSink(sink);
    }
    let resolvedLens;
    if (lens !== null && lens !== undefined) {
        resolvedLens = lens;
    }
    else {
        const mod = await import("./types/ExternalizationPolicy.js");
        resolvedLens = mod.LIVE_LENS;
    }
    return new effMod.EffectPipeline({ sink: mux, lens: resolvedLens });
}
const VALID_TRUST_MODES = ['off', 'log-only', 'enforce'];
/**
 * Validates and returns the trust mode from a raw config.
 */
export function validateTrustMode(mode) {
    if (!VALID_TRUST_MODES.includes(mode)) {
        throw new WarpError('trust.mode must be one of: off, log-only, enforce', 'E_TRUST_CONFIG');
    }
    return mode;
}
/**
 * Validates and returns the trust pin from a raw config.
 */
export function validateTrustPin(pin) {
    if (pin !== undefined && pin !== null && typeof pin !== 'string') {
        throw new WarpError('trust.pin must be a string', 'E_TRUST_CONFIG');
    }
    return pin ?? null;
}
/**
 * Normalizes a trust configuration into a canonical shape with defaults.
 */
export function normalizeTrustConfig(trust) {
    if (trust === null || trust === undefined) {
        return { mode: 'off', pin: null };
    }
    if (typeof trust !== 'object') {
        throw new WarpError('trust must be an object', 'E_TRUST_CONFIG');
    }
    return {
        mode: validateTrustMode(trust.mode ?? 'off'),
        pin: validateTrustPin(trust.pin),
    };
}
