import { canonicalizePath } from "../utils/paths.js";
function resourcePrecedenceRank(m) {
    if (m.origin === "package")
        return 4;
    if (m.borrowedProjectLocal)
        return 5;
    const scopeBase = m.scope === "project" ? 0 : 2;
    return scopeBase + (m.source === "local" ? 0 : 1);
}
export function getTargetMap(accumulator, resourceType) {
    switch (resourceType) {
        case "extensions":
            return accumulator.extensions;
        case "skills":
            return accumulator.skills;
        case "prompts":
            return accumulator.prompts;
        case "themes":
            return accumulator.themes;
        case "workflows":
            return accumulator.workflows;
        default:
            throw new Error(`Unknown resource type: ${resourceType}`);
    }
}
export function addResource(map, path, metadata, enabled) {
    if (!path)
        return;
    if (!map.has(path)) {
        map.set(path, { metadata, enabled });
    }
}
export function createAccumulator() {
    return {
        extensions: new Map(),
        skills: new Map(),
        prompts: new Map(),
        themes: new Map(),
        workflows: new Map(),
    };
}
export function toResolvedPaths(accumulator) {
    const mapToResolved = (entries) => {
        const resolved = Array.from(entries.entries()).map(([path, { metadata, enabled }]) => ({
            path,
            enabled,
            metadata,
        }));
        resolved.sort((a, b) => resourcePrecedenceRank(a.metadata) - resourcePrecedenceRank(b.metadata));
        const seen = new Set();
        return resolved.filter((entry) => {
            const canonicalPath = canonicalizePath(entry.path);
            if (seen.has(canonicalPath))
                return false;
            seen.add(canonicalPath);
            return true;
        });
    };
    return {
        extensions: mapToResolved(accumulator.extensions),
        skills: mapToResolved(accumulator.skills),
        prompts: mapToResolved(accumulator.prompts),
        themes: mapToResolved(accumulator.themes),
        workflows: mapToResolved(accumulator.workflows),
    };
}
//# sourceMappingURL=package-manager-resource-accumulator.js.map