export const SHARED_SESSION_INPUT_INTENTS = ['submit', 'steer', 'follow-up'];
export const SHARED_SESSION_INPUT_STATES = ['queued', 'delivered', 'spawned', 'completed', 'cancelled', 'failed', 'rejected'];
function normalizeSharedSessionModelId(modelId, providerId) {
    const trimmedModelId = modelId?.trim();
    if (!trimmedModelId)
        return undefined;
    const trimmedProviderId = providerId?.trim();
    const separatorIndex = trimmedModelId.indexOf(':');
    if (separatorIndex > 0) {
        const modelProviderId = trimmedModelId.slice(0, separatorIndex);
        if (trimmedProviderId && trimmedProviderId !== modelProviderId) {
            throw new Error(`Shared-session routing model '${trimmedModelId}' conflicts with provider '${trimmedProviderId}'.`);
        }
        return trimmedModelId;
    }
    if (trimmedProviderId)
        return `${trimmedProviderId}:${trimmedModelId}`;
    throw new Error(`Shared-session routing model '${trimmedModelId}' must be provider-qualified.`);
}
function normalizeSharedSessionFallbackModels(models) {
    return (models ?? [])
        .filter((model) => typeof model === 'string' && model.trim().length > 0)
        .map((model) => {
        const trimmed = model.trim();
        const separatorIndex = trimmed.indexOf(':');
        if (separatorIndex <= 0 || separatorIndex === trimmed.length - 1) {
            throw new Error(`Shared-session fallback model '${model}' must be provider-qualified.`);
        }
        return trimmed;
    });
}
export function buildSharedSessionAgentSpawnRoutingInput(routing, options = {}) {
    if (!routing)
        return options.restrictTools ? { restrictTools: true } : {};
    const provider = routing.providerId?.trim();
    const model = normalizeSharedSessionModelId(routing.modelId, provider);
    if (provider && !model) {
        throw new Error('Shared-session provider routing requires a provider-qualified model when provider is supplied.');
    }
    const fallbackModels = normalizeSharedSessionFallbackModels(routing.fallbackModels);
    const providerFailurePolicy = routing.providerFailurePolicy ?? (fallbackModels.length ? 'ordered-fallbacks' : 'fail');
    if (providerFailurePolicy === 'ordered-fallbacks' && fallbackModels.length === 0) {
        throw new Error('Shared-session ordered fallback routing requires at least one provider-qualified fallback model.');
    }
    if (providerFailurePolicy === 'fail' && fallbackModels.length > 0) {
        throw new Error('Shared-session fail routing cannot include fallback models; use ordered-fallbacks to enable model failover.');
    }
    const agentRouting = {
        providerSelection: routing.providerSelection ?? (provider ? 'concrete' : 'inherit-current'),
        providerFailurePolicy,
        ...(fallbackModels.length ? { fallbackModels } : {}),
    };
    return {
        ...(model ? { model } : {}),
        ...(provider ? { provider } : {}),
        ...(routing.tools?.length ? { tools: [...routing.tools] } : {}),
        ...(options.restrictTools ? { restrictTools: true } : {}),
        routing: agentRouting,
        ...(routing.executionIntent ? { executionIntent: routing.executionIntent } : {}),
        ...(routing.reasoningEffort ? { reasoningEffort: routing.reasoningEffort } : {}),
    };
}
