import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { getAgentDir } from "@sheason/pi-coding-agent";
import { getLocalPiDir } from "../config.js";
import { ensureMcpResourceIds, ensureModelsResourceIds } from "../resource-ids.js";
import { isRecord } from "../utils.js";
function readJson(path) {
    if (!existsSync(path)) {
        return undefined;
    }
    try {
        return JSON.parse(readFileSync(path, "utf8"));
    }
    catch {
        return undefined;
    }
}
function readLayerFromDir(dir, includeAuth, source) {
    const layer = { source };
    if (includeAuth) {
        const auth = readJson(join(dir, "auth.json"));
        if (auth && typeof auth === "object" && !Array.isArray(auth)) {
            layer.auth = auth;
        }
    }
    const modelsPath = join(dir, "models.json");
    ensureModelsResourceIds(modelsPath);
    const models = readJson(modelsPath);
    if (models !== undefined) {
        layer.models = models;
    }
    const settings = readJson(join(dir, "settings.json"));
    if (settings !== undefined) {
        layer.settings = settings;
    }
    const mcpPath = join(dir, "mcp.json");
    ensureMcpResourceIds(mcpPath);
    const mcp = readJson(mcpPath);
    if (mcp !== undefined) {
        layer.mcp = mcp;
    }
    return Object.keys(layer).length > 1 ? layer : undefined;
}
function readHostResourceSelection(value) {
    if (value === true) {
        return true;
    }
    if (Array.isArray(value) && value.every((item) => typeof item === "string" && item.length > 0)) {
        return new Set(value);
    }
    return undefined;
}
function getChildHostMcpSelection(childLayer) {
    if (!isRecord(childLayer?.mcp)) {
        return undefined;
    }
    const ext = childLayer.mcp.extends;
    if (!isRecord(ext) || !isRecord(ext.host)) {
        return undefined;
    }
    return readHostResourceSelection(ext.host.mcp);
}
function filterHostMcpBySelection(layer, selection) {
    if (!layer?.mcp) {
        return layer;
    }
    if (selection === true) {
        return layer;
    }
    if (!isRecord(layer.mcp) || !Array.isArray(layer.mcp.servers) || selection === undefined) {
        const { mcp: _mcp, ...rest } = layer;
        return Object.keys(rest).length > 1 ? rest : undefined;
    }
    const servers = layer.mcp.servers.filter((server) => isRecord(server) && typeof server.name === "string" && selection.has(server.name));
    if (servers.length === 0) {
        const { mcp: _mcp, ...rest } = layer;
        return Object.keys(rest).length > 1 ? rest : undefined;
    }
    return { ...layer, mcp: { ...layer.mcp, servers } };
}
function removeChildMcpExtends(layer) {
    if (!isRecord(layer?.mcp) || layer.mcp.extends === undefined) {
        return layer;
    }
    const { extends: _extends, ...mcp } = layer.mcp;
    return { ...layer, mcp };
}
export function buildAgentConfigLayers(options) {
    const peerSnapshots = (options.peerSnapshots ?? (options.peerSnapshot ? [{ peerId: "primary", snapshot: options.peerSnapshot }] : []))
        .slice()
        .sort((a, b) => a.peerId.localeCompare(b.peerId));
    const globalLayer = readLayerFromDir(getAgentDir(), true, { kind: "hub", scope: "global" });
    const workspaceLayer = readLayerFromDir(getLocalPiDir(options.cwd), false, { kind: "hub", scope: "workspace" });
    const childLayer = options.agentDir
        ? readLayerFromDir(options.agentDir, true, { kind: "hub", scope: "child" })
        : undefined;
    const hostMcpSelection = options.agentDir ? getChildHostMcpSelection(childLayer) : true;
    const layers = [
        filterHostMcpBySelection(globalLayer, hostMcpSelection),
        filterHostMcpBySelection(workspaceLayer, hostMcpSelection),
        removeChildMcpExtends(childLayer),
        ...peerSnapshots.flatMap(({ peerId, snapshot }) => [
            snapshot.global
                ? { ...snapshot.global, source: { kind: "peer", peerId, scope: "global" } }
                : undefined,
            snapshot.cwdLayer
                ? { ...snapshot.cwdLayer, source: { kind: "peer", peerId, scope: "cwd" } }
                : undefined,
        ]),
    ];
    return layers.filter((layer) => layer !== undefined);
}
//# sourceMappingURL=local-config-layers.js.map