import { createSelectorReadError, normalizeProviderError } from './world-evolution-selector-read-errors.js';
import { validateCheckpointSelector, validateCommitSelector, validateExecutionSelector, validateReplaySelector, validateSupervisionSelector } from './world-evolution-selector-read-selectors.js';
import { ensureArray } from './world-evolution-selector-read-shared.js';
import { normalizeCheckpointView, normalizeCommitRequestView, normalizeExecutionEventView, normalizeReplayView, normalizeSupervisionView } from './world-evolution-selector-read-views.js';
async function readExecutionEvents(resolveProvider, selector) {
    const methodId = 'worldEvolution.executionEvents.read';
    const validated = validateExecutionSelector(selector);
    const provider = resolveProvider();
    if (!provider) {
        throw createSelectorReadError({
            category: 'BOUNDARY_DENIED',
            methodId,
            message: `${methodId} is not available on this boundary`,
        });
    }
    let matches;
    try {
        matches = ensureArray(await provider.executionEvents.read(validated.selector), methodId);
    }
    catch (error) {
        throw normalizeProviderError(error, methodId);
    }
    return {
        selector: validated.selector,
        matchMode: validated.matchMode,
        matches: matches.map((match) => normalizeExecutionEventView(match, methodId)),
    };
}
async function readReplays(resolveProvider, selector) {
    const methodId = 'worldEvolution.replays.read';
    const validated = validateReplaySelector(selector);
    const provider = resolveProvider();
    if (!provider) {
        throw createSelectorReadError({
            category: 'BOUNDARY_DENIED',
            methodId,
            message: `${methodId} is not available on this boundary`,
        });
    }
    let matches;
    try {
        matches = ensureArray(await provider.replays.read(validated.selector), methodId);
    }
    catch (error) {
        throw normalizeProviderError(error, methodId);
    }
    return {
        selector: validated.selector,
        matchMode: validated.matchMode,
        matches: matches.map((match) => normalizeReplayView(match, methodId)),
    };
}
async function readCheckpoints(resolveProvider, selector) {
    const methodId = 'worldEvolution.checkpoints.read';
    const validated = validateCheckpointSelector(selector);
    const provider = resolveProvider();
    if (!provider) {
        throw createSelectorReadError({
            category: 'BOUNDARY_DENIED',
            methodId,
            message: `${methodId} is not available on this boundary`,
        });
    }
    let matches;
    try {
        matches = ensureArray(await provider.checkpoints.read(validated.selector), methodId);
    }
    catch (error) {
        throw normalizeProviderError(error, methodId);
    }
    return {
        selector: validated.selector,
        matchMode: validated.matchMode,
        matches: matches.map((match) => normalizeCheckpointView(match, methodId)),
    };
}
async function readSupervision(resolveProvider, selector) {
    const methodId = 'worldEvolution.supervision.read';
    const validated = validateSupervisionSelector(selector);
    const provider = resolveProvider();
    if (!provider) {
        throw createSelectorReadError({
            category: 'BOUNDARY_DENIED',
            methodId,
            message: `${methodId} is not available on this boundary`,
        });
    }
    let matches;
    try {
        matches = ensureArray(await provider.supervision.read(validated.selector), methodId);
    }
    catch (error) {
        throw normalizeProviderError(error, methodId);
    }
    return {
        selector: validated.selector,
        matchMode: validated.matchMode,
        matches: matches.map((match) => normalizeSupervisionView(match, methodId)),
    };
}
async function readCommitRequests(resolveProvider, selector) {
    const methodId = 'worldEvolution.commitRequests.read';
    const validated = validateCommitSelector(selector);
    const provider = resolveProvider();
    if (!provider) {
        throw createSelectorReadError({
            category: 'BOUNDARY_DENIED',
            methodId,
            message: `${methodId} is not available on this boundary`,
        });
    }
    let matches;
    try {
        matches = ensureArray(await provider.commitRequests.read(validated.selector), methodId);
    }
    catch (error) {
        throw normalizeProviderError(error, methodId);
    }
    return {
        selector: validated.selector,
        matchMode: validated.matchMode,
        matches: matches.map((match) => normalizeCommitRequestView(match, methodId)),
    };
}
export function createWorldEvolutionSelectorReadFacade(resolveProvider) {
    return {
        executionEvents: {
            read: (selector) => readExecutionEvents(resolveProvider, selector),
        },
        replays: {
            read: (selector) => readReplays(resolveProvider, selector),
        },
        checkpoints: {
            read: (selector) => readCheckpoints(resolveProvider, selector),
        },
        supervision: {
            read: (selector) => readSupervision(resolveProvider, selector),
        },
        commitRequests: {
            read: (selector) => readCommitRequests(resolveProvider, selector),
        },
    };
}
//# sourceMappingURL=world-evolution-selector-read-readers.js.map