import { validateProviderModelCompatibility } from './providerModelCompatibility.js';
import { normalizeConfigProviderReferenceDetailed, } from './providerReference.js';
function normalizeQualityGate(gate) {
    if (typeof gate === 'string') {
        return gate;
    }
    return {
        type: gate.type,
        ...(gate.name !== undefined ? { name: gate.name } : {}),
        command: gate.command,
        ...(gate.cwd !== undefined ? { cwd: gate.cwd } : {}),
        ...(gate.timeoutMs !== undefined || gate.timeout_ms !== undefined
            ? { timeoutMs: gate.timeoutMs ?? gate.timeout_ms }
            : {}),
    };
}
function normalizeQualityGates(gates) {
    return gates?.map(normalizeQualityGate);
}
function denormalizeQualityGate(gate) {
    if (typeof gate === 'string') {
        return gate;
    }
    return {
        type: gate.type,
        ...(gate.name !== undefined ? { name: gate.name } : {}),
        command: gate.command,
        ...(gate.cwd !== undefined ? { cwd: gate.cwd } : {}),
        ...(gate.timeoutMs !== undefined ? { timeout_ms: gate.timeoutMs } : {}),
    };
}
function denormalizeQualityGates(gates) {
    return gates?.map(denormalizeQualityGate);
}
function assertNormalizedPersonaProviderOptions(persona, providerOptions) {
    if (providerOptions !== undefined) {
        return;
    }
    throw new Error(`Configuration error: persona_providers.${persona}.provider_options must include at least one provider-specific option`);
}
export function normalizeRuntime(runtime) {
    if (!runtime?.prepare || runtime.prepare.length === 0) {
        return undefined;
    }
    return { prepare: [...new Set(runtime.prepare)] };
}
export function normalizeRateLimitFallback(raw) {
    if (!raw) {
        return undefined;
    }
    const switchChain = raw.switch_chain ?? [];
    return {
        switchChain: switchChain.map((entry, index) => {
            validateProviderModelCompatibility(entry.provider, entry.model, {
                modelFieldName: `Configuration error: rate_limit_fallback.switch_chain[${index}].model`,
            });
            return {
                provider: entry.provider,
                ...(entry.model !== undefined ? { model: entry.model } : {}),
            };
        }),
    };
}
export function denormalizeRateLimitFallback(config) {
    if (!config) {
        return undefined;
    }
    return {
        switch_chain: config.switchChain.map((entry) => ({
            provider: entry.provider,
            ...(entry.model !== undefined ? { model: entry.model } : {}),
        })),
    };
}
export function normalizeProviderProfiles(raw) {
    if (!raw)
        return undefined;
    const entries = Object.entries(raw).map(([provider, profile]) => [
        provider,
        {
            defaultPermissionMode: profile.default_permission_mode,
            stepPermissionOverrides: profile.step_permission_overrides,
        },
    ]);
    return Object.fromEntries(entries);
}
export function denormalizeProviderProfiles(profiles) {
    if (!profiles)
        return undefined;
    const entries = Object.entries(profiles);
    if (entries.length === 0)
        return undefined;
    return Object.fromEntries(entries.map(([provider, profile]) => [provider, {
            default_permission_mode: profile.defaultPermissionMode,
            ...(profile.stepPermissionOverrides
                ? { step_permission_overrides: profile.stepPermissionOverrides }
                : {}),
        }]));
}
export function normalizeWorkflowOverrides(raw) {
    if (!raw)
        return undefined;
    return {
        qualityGates: normalizeQualityGates(raw.quality_gates),
        qualityGatesEditOnly: raw.quality_gates_edit_only,
        steps: raw.steps
            ? Object.fromEntries(Object.entries(raw.steps).map(([name, override]) => [
                name,
                { qualityGates: normalizeQualityGates(override.quality_gates) },
            ]))
            : undefined,
        personas: raw.personas
            ? Object.fromEntries(Object.entries(raw.personas).map(([name, override]) => [
                name,
                { qualityGates: normalizeQualityGates(override.quality_gates) },
            ]))
            : undefined,
    };
}
export function denormalizeWorkflowOverrides(overrides) {
    if (!overrides)
        return undefined;
    const result = {};
    if (overrides.qualityGates !== undefined) {
        result.quality_gates = denormalizeQualityGates(overrides.qualityGates);
    }
    if (overrides.qualityGatesEditOnly !== undefined) {
        result.quality_gates_edit_only = overrides.qualityGatesEditOnly;
    }
    if (overrides.steps) {
        result.steps = Object.fromEntries(Object.entries(overrides.steps).map(([name, override]) => {
            const stepOverride = {};
            if (override.qualityGates !== undefined) {
                stepOverride.quality_gates = denormalizeQualityGates(override.qualityGates);
            }
            return [name, stepOverride];
        }));
    }
    if (overrides.personas) {
        result.personas = Object.fromEntries(Object.entries(overrides.personas).map(([name, override]) => {
            const personaOverride = {};
            if (override.qualityGates !== undefined) {
                personaOverride.quality_gates = denormalizeQualityGates(override.qualityGates);
            }
            return [name, personaOverride];
        }));
    }
    return Object.keys(result).length > 0 ? result : undefined;
}
export function normalizePersonaProviders(raw) {
    if (!raw)
        return undefined;
    const entries = Object.entries(raw);
    if (entries.length === 0)
        return undefined;
    return Object.fromEntries(entries.map(([persona, entry]) => {
        const rawProviderOptions = typeof entry === 'string' ? undefined : entry.provider_options;
        const normalizedReference = normalizeConfigProviderReferenceDetailed((typeof entry === 'string' ? entry : (entry.provider ?? entry.type)), typeof entry === 'string' ? undefined : entry.model, rawProviderOptions);
        if (rawProviderOptions !== undefined) {
            assertNormalizedPersonaProviderOptions(persona, normalizedReference.providerOptions);
        }
        const normalizedEntry = {
            ...(normalizedReference.provider !== undefined ? { provider: normalizedReference.provider } : {}),
            ...(normalizedReference.model !== undefined ? { model: normalizedReference.model } : {}),
            ...(normalizedReference.providerOptions !== undefined
                ? { providerOptions: normalizedReference.providerOptions }
                : {}),
        };
        validateProviderModelCompatibility(normalizedEntry.provider, normalizedEntry.model, {
            modelFieldName: `Configuration error: persona_providers.${persona}.model`,
            requireProviderQualifiedModelForOpencode: false,
        });
        return [persona, normalizedEntry];
    }));
}
export function denormalizePersonaProviders(personaProviders) {
    if (!personaProviders) {
        return undefined;
    }
    const entries = Object.entries(personaProviders);
    if (entries.length === 0) {
        return undefined;
    }
    return Object.fromEntries(entries.map(([persona, entry]) => {
        const rawEntry = {};
        if (entry.provider !== undefined) {
            rawEntry.provider = entry.provider;
        }
        if (entry.model !== undefined) {
            rawEntry.model = entry.model;
        }
        const rawProviderOptions = denormalizeProviderOptions(entry.providerOptions);
        if (entry.providerOptions !== undefined) {
            assertNormalizedPersonaProviderOptions(persona, rawProviderOptions);
        }
        if (rawProviderOptions !== undefined) {
            rawEntry.provider_options = rawProviderOptions;
        }
        if (Object.keys(rawEntry).length === 0) {
            throw new Error(`Configuration error: persona_providers.${persona} must include at least one of 'provider', 'model', or 'provider_options'`);
        }
        return [persona, rawEntry];
    }));
}
export function normalizePipelineConfig(raw) {
    if (!raw)
        return undefined;
    const { default_branch_prefix, commit_message_template, pr_body_template } = raw;
    if (default_branch_prefix === undefined && commit_message_template === undefined && pr_body_template === undefined) {
        return undefined;
    }
    return {
        defaultBranchPrefix: default_branch_prefix,
        commitMessageTemplate: commit_message_template,
        prBodyTemplate: pr_body_template,
    };
}
export function normalizeAssistantConfig(raw) {
    if (!raw?.init_files || raw.init_files.length === 0) {
        return undefined;
    }
    return { initFiles: raw.init_files };
}
export function denormalizeAssistantConfig(config) {
    if (!config?.initFiles || config.initFiles.length === 0) {
        return undefined;
    }
    return { init_files: config.initFiles };
}
export function normalizeTaktProviders(raw) {
    if (!raw) {
        return undefined;
    }
    const normalizedAssistant = normalizeTaktAssistantProvider(raw.assistant);
    if (!normalizedAssistant) {
        return undefined;
    }
    return { assistant: normalizedAssistant };
}
export function normalizeTaktAssistantProvider(assistant) {
    if (!assistant) {
        return undefined;
    }
    const { provider, model } = assistant;
    if (provider === undefined && model === undefined) {
        throw new Error("Configuration error: 'takt_providers.assistant' must include provider or model.");
    }
    validateProviderModelCompatibility(provider, model, {
        modelFieldName: 'Configuration error: takt_providers.assistant.model',
    });
    if (provider !== undefined) {
        return {
            provider,
            ...(model !== undefined ? { model } : {}),
        };
    }
    if (model === undefined) {
        throw new Error("Configuration error: 'takt_providers.assistant' must include provider or model.");
    }
    return { model };
}
export function buildRawTaktProvidersOrThrow(taktProviders) {
    if (taktProviders === undefined) {
        return undefined;
    }
    if (taktProviders.assistant === undefined) {
        throw new Error("Configuration error: 'takt_providers.assistant' is required when takt_providers is set.");
    }
    const assistant = normalizeTaktAssistantProvider(taktProviders.assistant);
    if (!assistant) {
        throw new Error("Configuration error: 'takt_providers.assistant' must include provider or model.");
    }
    return { assistant };
}
export function denormalizeProviderOptions(providerOptions) {
    if (!providerOptions) {
        return undefined;
    }
    const raw = {};
    if (providerOptions.codex?.networkAccess !== undefined
        || providerOptions.codex?.reasoningEffort !== undefined) {
        raw.codex = {
            ...(providerOptions.codex.networkAccess !== undefined
                ? { network_access: providerOptions.codex.networkAccess }
                : {}),
            ...(providerOptions.codex.reasoningEffort !== undefined
                ? { reasoning_effort: providerOptions.codex.reasoningEffort }
                : {}),
        };
    }
    if (providerOptions.opencode?.networkAccess !== undefined || providerOptions.opencode?.variant !== undefined) {
        raw.opencode = {
            ...(providerOptions.opencode.networkAccess !== undefined
                ? { network_access: providerOptions.opencode.networkAccess }
                : {}),
            ...(providerOptions.opencode.variant !== undefined
                ? { variant: providerOptions.opencode.variant }
                : {}),
        };
    }
    if (providerOptions.claude) {
        const claude = {};
        if (providerOptions.claude.allowedTools !== undefined) {
            claude.allowed_tools = providerOptions.claude.allowedTools;
        }
        if (providerOptions.claude.effort !== undefined) {
            claude.effort = providerOptions.claude.effort;
        }
        const sandbox = {};
        if (providerOptions.claude.sandbox?.allowUnsandboxedCommands !== undefined) {
            sandbox.allow_unsandboxed_commands = providerOptions.claude.sandbox.allowUnsandboxedCommands;
        }
        if (providerOptions.claude.sandbox?.excludedCommands !== undefined) {
            sandbox.excluded_commands = providerOptions.claude.sandbox.excludedCommands;
        }
        if (Object.keys(sandbox).length > 0) {
            claude.sandbox = sandbox;
        }
        if (Object.keys(claude).length > 0) {
            raw.claude = claude;
        }
    }
    if (providerOptions.copilot?.effort !== undefined) {
        raw.copilot = { effort: providerOptions.copilot.effort };
    }
    if (providerOptions.claudeTerminal) {
        const claudeTerminal = {};
        if (providerOptions.claudeTerminal.backend !== undefined) {
            claudeTerminal.backend = providerOptions.claudeTerminal.backend;
        }
        if (providerOptions.claudeTerminal.timeoutMs !== undefined) {
            claudeTerminal.timeout_ms = providerOptions.claudeTerminal.timeoutMs;
        }
        if (providerOptions.claudeTerminal.keepSession !== undefined) {
            claudeTerminal.keep_session = providerOptions.claudeTerminal.keepSession;
        }
        if (providerOptions.claudeTerminal.transcriptPollIntervalMs !== undefined) {
            claudeTerminal.transcript_poll_interval_ms = providerOptions.claudeTerminal.transcriptPollIntervalMs;
        }
        if (Object.keys(claudeTerminal).length > 0) {
            raw.claude_terminal = claudeTerminal;
        }
    }
    return Object.keys(raw).length > 0 ? raw : undefined;
}
//# sourceMappingURL=configNormalizers.js.map