import { dirname, join, resolve } from "node:path";
import { getProjectConfigDirs } from "../config.js";
import { getBaseDirsForScope, getHomeDir } from "./package-manager-paths.js";
import { addResource, getTargetMap } from "./package-manager-resource-accumulator.js";
import { collectAncestorAgentsSkillDirs, collectAutoExtensionEntries, collectAutoPromptEntries, collectAutoSkillEntries, collectAutoThemeEntries, collectResourceFiles, } from "./package-manager-resource-files.js";
import { applyPatterns, isEnabledByOverrides } from "./package-manager-resource-patterns.js";
export async function collectProjectLocalResources(sourceRoot, accumulator, filter, metadata) {
    let found = false;
    const projectMetadata = { ...metadata, origin: "top-level", borrowedProjectLocal: true };
    const addResources = (resourceType, paths, resourceMetadata, patterns) => {
        if (paths.length === 0)
            return;
        found = true;
        const target = getTargetMap(accumulator, resourceType);
        const enabledPaths = patterns === undefined
            ? new Set(paths)
            : patterns.length === 0
                ? new Set()
                : applyPatterns(paths, patterns, sourceRoot);
        for (const path of paths)
            addResource(target, path, resourceMetadata, enabledPaths.has(path));
    };
    for (const configDir of getProjectConfigDirs(sourceRoot)) {
        const configMetadata = { ...projectMetadata, baseDir: configDir };
        addResources("extensions", await collectAutoExtensionEntries(join(configDir, "extensions")), configMetadata, filter?.extensions);
        addResources("skills", await collectAutoSkillEntries(join(configDir, "skills"), "pi"), configMetadata, filter?.skills);
        addResources("prompts", await collectAutoPromptEntries(join(configDir, "prompts")), configMetadata, filter?.prompts);
        addResources("themes", await collectAutoThemeEntries(join(configDir, "themes")), configMetadata, filter?.themes);
        addResources("workflows", await collectResourceFiles(join(configDir, "workflows"), "workflows"), configMetadata, filter?.workflows);
    }
    const agentsSkillsDir = join(sourceRoot, ".agents", "skills");
    addResources("skills", await collectAutoSkillEntries(agentsSkillsDir, "agents"), { ...projectMetadata, baseDir: dirname(agentsSkillsDir) }, filter?.skills);
    return found;
}
export async function addAutoDiscoveredResources(context, accumulator, globalSettings, projectSettings, globalBaseDir, projectBaseDir) {
    const userMetadata = { source: "auto", scope: "user", origin: "top-level", baseDir: globalBaseDir };
    const projectMetadata = { source: "auto", scope: "project", origin: "top-level", baseDir: projectBaseDir };
    const userOverrides = {
        extensions: (globalSettings.extensions ?? []),
        skills: (globalSettings.skills ?? []),
        prompts: (globalSettings.prompts ?? []),
        themes: (globalSettings.themes ?? []),
        workflows: (globalSettings.workflows ?? []),
    };
    const projectOverrides = {
        extensions: (projectSettings.extensions ?? []),
        skills: (projectSettings.skills ?? []),
        prompts: (projectSettings.prompts ?? []),
        themes: (projectSettings.themes ?? []),
        workflows: (projectSettings.workflows ?? []),
    };
    const userConfigDirs = getBaseDirsForScope(context, "user");
    const projectConfigDirs = getBaseDirsForScope(context, "project");
    const userAgentsSkillsDir = join(getHomeDir(), ".agents", "skills");
    const projectTrusted = context.settingsManager.isProjectTrusted();
    const projectAgentsSkillDirs = projectTrusted
        ? (await collectAncestorAgentsSkillDirs(context.cwd)).filter((dir) => resolve(dir) !== resolve(userAgentsSkillsDir))
        : [];
    const addResources = (resourceType, paths, metadata, overrides, baseDir) => {
        const target = getTargetMap(accumulator, resourceType);
        for (const path of paths)
            addResource(target, path, metadata, isEnabledByOverrides(path, overrides, baseDir));
    };
    if (projectTrusted) {
        for (const configDir of projectConfigDirs) {
            const metadata = { ...projectMetadata, baseDir: configDir };
            addResources("extensions", await collectAutoExtensionEntries(join(configDir, "extensions")), metadata, projectOverrides.extensions, configDir);
            addResources("skills", await collectAutoSkillEntries(join(configDir, "skills"), "pi"), metadata, projectOverrides.skills, configDir);
            addResources("prompts", await collectAutoPromptEntries(join(configDir, "prompts")), metadata, projectOverrides.prompts, configDir);
            addResources("themes", await collectAutoThemeEntries(join(configDir, "themes")), metadata, projectOverrides.themes, configDir);
            addResources("workflows", await collectResourceFiles(join(configDir, "workflows"), "workflows"), metadata, projectOverrides.workflows, configDir);
        }
    }
    for (const agentsSkillsDir of projectAgentsSkillDirs) {
        const agentsBaseDir = dirname(agentsSkillsDir);
        addResources("skills", await collectAutoSkillEntries(agentsSkillsDir, "agents"), { ...projectMetadata, baseDir: agentsBaseDir }, projectOverrides.skills, agentsBaseDir);
    }
    for (const configDir of userConfigDirs) {
        const metadata = { ...userMetadata, baseDir: configDir };
        addResources("extensions", await collectAutoExtensionEntries(join(configDir, "extensions")), metadata, userOverrides.extensions, configDir);
        addResources("skills", await collectAutoSkillEntries(join(configDir, "skills"), "pi"), metadata, userOverrides.skills, configDir);
        addResources("prompts", await collectAutoPromptEntries(join(configDir, "prompts")), metadata, userOverrides.prompts, configDir);
        addResources("themes", await collectAutoThemeEntries(join(configDir, "themes")), metadata, userOverrides.themes, configDir);
        addResources("workflows", await collectResourceFiles(join(configDir, "workflows"), "workflows"), metadata, userOverrides.workflows, configDir);
    }
    const userAgentsBaseDir = dirname(userAgentsSkillsDir);
    addResources("skills", await collectAutoSkillEntries(userAgentsSkillsDir, "agents"), { ...userMetadata, baseDir: userAgentsBaseDir }, userOverrides.skills, userAgentsBaseDir);
}
//# sourceMappingURL=package-manager-auto-resources.js.map