import { loadModelResolver } from "../models/catalog.js";
import { parsePersonaReference } from "../persona_reference.js";
import { loadAllContent } from "./content_loader.js";
import { resolveConfigLevels } from "./paths.js";
import { loadConfigWithDiagnostics } from "./schema.js";
import { buildVirtualBundle } from "./virtual_bundle.js";
export function loadRuntimeBootstrap(cwd, deps) {
    const levels = resolveConfigLevels(deps, { cwd });
    const modelResolver = loadModelResolver({ deps, levels });
    const configResult = loadConfigWithDiagnostics(deps, {
        levels,
        modelResolver,
    });
    const config = configResult.config;
    const virtualBundle = buildVirtualBundle(config, modelResolver.resolveConfiguredModel);
    return {
        config,
        levels,
        modelResolver,
        virtualBundle,
        warnings: configResult.errors,
    };
}
export async function loadRuntimeConfig(cwd, deps) {
    const bootstrap = loadRuntimeBootstrap(cwd, deps);
    const content = await loadAllContent(bootstrap.config, {
        deps,
        levels: bootstrap.levels,
        modelResolver: bootstrap.modelResolver,
    });
    const warnings = [...bootstrap.warnings, ...content.errors];
    if (bootstrap.config.defaultPersona) {
        const parsedDefaultPersona = parsePersonaReference(bootstrap.config.defaultPersona);
        const personaId = parsedDefaultPersona.personaId;
        const matched = personaId
            ? content.personas.some((persona) => persona.id === personaId)
            : false;
        if (!matched) {
            warnings.push(`defaultPersona '${bootstrap.config.defaultPersona}' not found in loaded personas.`);
        }
    }
    if (bootstrap.config.defaultTheme) {
        const matched = content.themes.some((theme) => theme.id === bootstrap.config.defaultTheme);
        if (!matched) {
            warnings.push(`defaultTheme '${bootstrap.config.defaultTheme}' not found in built-in themes, .tau/themes, or ~/.config/tau/themes.`);
        }
    }
    return {
        bootstrap,
        config: bootstrap.config,
        personas: content.personas,
        prompts: content.prompts,
        skills: content.skills,
        themes: content.themes,
        bashCommands: bootstrap.config.bashCommands ?? [],
        diffTool: bootstrap.config.diffTool,
        warnings,
    };
}
//# sourceMappingURL=runtime.js.map