import { resolvePath } from "../utils/paths.js";
import { createEventBus } from "./event-bus.js";
import { createExtensionRuntime } from "./extensions/loader.js";
import { DefaultPackageManager } from "./package-manager.js";
import { SettingsManager } from "./settings-manager.js";
import { updatePromptsFromPathsAsync, updateSkillsFromPathsAsync, updateThemesFromPathsAsync } from "./resource-loader-assets.js";
import { clonePackageSources, mergeInheritedStrings } from "./resource-loader-helpers.js";
import { collectWorkflowResources, normalizeExtensionPaths, recordExtensionSourceInfo, resolvePackageResourcePaths, } from "./resource-loader-package-resources.js";
import { mergeResourcePaths } from "./resource-loader-paths.js";
import { loadProjectTrustExtensions, reloadDefaultResourceLoader } from "./resource-loader-reload.js";
export class DefaultResourceLoader {
    constructor(options) {
        const inheritanceSnapshot = options.resourceLoaderInheritanceSnapshot;
        const inheritedSettingsOptions = inheritanceSnapshot?.projectTrusted === undefined
            ? undefined
            : { projectTrusted: inheritanceSnapshot.projectTrusted };
        this.cwd = resolvePath(options.cwd);
        this.agentDir = resolvePath(options.agentDir);
        this.settingsManager = options.settingsManager ?? SettingsManager.create(this.cwd, this.agentDir, inheritedSettingsOptions);
        this.eventBus = options.eventBus ?? createEventBus();
        this.packageManager = new DefaultPackageManager({
            cwd: this.cwd,
            agentDir: this.agentDir,
            settingsManager: this.settingsManager,
        });
        this.additionalExtensionPaths = mergeInheritedStrings(inheritanceSnapshot?.additionalExtensionPaths, options.additionalExtensionPaths);
        this.additionalSkillPaths = mergeInheritedStrings(inheritanceSnapshot?.additionalSkillPaths, options.additionalSkillPaths);
        this.additionalPromptTemplatePaths = mergeInheritedStrings(inheritanceSnapshot?.additionalPromptTemplatePaths, options.additionalPromptTemplatePaths);
        this.additionalThemePaths = mergeInheritedStrings(inheritanceSnapshot?.additionalThemePaths, options.additionalThemePaths);
        this.builtinPackagePaths = options.builtinPackagePaths !== undefined
            ? clonePackageSources(options.builtinPackagePaths)
            : clonePackageSources(inheritanceSnapshot?.builtinPackagePaths);
        this.extensionFactories = [...(inheritanceSnapshot?.extensionFactories ?? []), ...(options.extensionFactories ?? [])];
        this.noExtensions = options.noExtensions ?? inheritanceSnapshot?.noExtensions ?? false;
        this.noSkills = options.noSkills ?? inheritanceSnapshot?.noSkills ?? false;
        this.noPromptTemplates = options.noPromptTemplates ?? inheritanceSnapshot?.noPromptTemplates ?? false;
        this.noThemes = options.noThemes ?? inheritanceSnapshot?.noThemes ?? false;
        this.noContextFiles = options.noContextFiles ?? inheritanceSnapshot?.noContextFiles ?? false;
        this.systemPromptSource = options.systemPrompt ?? inheritanceSnapshot?.systemPrompt;
        this.appendSystemPromptSource = options.appendSystemPrompt !== undefined
            ? [...options.appendSystemPrompt]
            : inheritanceSnapshot?.appendSystemPrompt === undefined
                ? undefined
                : [...inheritanceSnapshot.appendSystemPrompt];
        this.extensionsOverride = options.extensionsOverride;
        this.skillsOverride = options.skillsOverride;
        this.promptsOverride = options.promptsOverride;
        this.themesOverride = options.themesOverride;
        this.agentsFilesOverride = options.agentsFilesOverride;
        this.systemPromptOverride = options.systemPromptOverride;
        this.appendSystemPromptOverride = options.appendSystemPromptOverride;
        this.extensionsResult = { extensions: [], errors: [], runtime: createExtensionRuntime() };
        this.skills = [];
        this.skillDiagnostics = [];
        this.prompts = [];
        this.promptDiagnostics = [];
        this.themes = [];
        this.themeDiagnostics = [];
        this.agentsFiles = [];
        this.appendSystemPrompt = [];
        this.workflowResources = [];
        this.trustedBorrowedProjectLocalSources = inheritanceSnapshot?.trustedBorrowedProjectLocalSources === undefined
            ? undefined
            : new Set(inheritanceSnapshot.trustedBorrowedProjectLocalSources);
        this.lastSkillPaths = [];
        this.extensionSkillSourceInfos = new Map();
        this.extensionPromptSourceInfos = new Map();
        this.extensionThemeSourceInfos = new Map();
        this.lastPromptPaths = [];
        this.lastThemePaths = [];
        this.loaded = false;
        this.touchInternalFieldsForSplit();
    }
    getExtensions() {
        return this.extensionsResult;
    }
    getSkills() {
        return { skills: this.skills, diagnostics: this.skillDiagnostics };
    }
    getPrompts() {
        return { prompts: this.prompts, diagnostics: this.promptDiagnostics };
    }
    getThemes() {
        return { themes: this.themes, diagnostics: this.themeDiagnostics };
    }
    getAgentsFiles() {
        return { agentsFiles: this.agentsFiles };
    }
    getSystemPrompt() {
        return this.systemPrompt;
    }
    getAppendSystemPrompt() {
        return this.appendSystemPrompt;
    }
    getWorkflowResources() {
        return [...this.workflowResources];
    }
    getInheritanceSnapshot() {
        return {
            projectTrusted: this.settingsManager.isProjectTrusted(),
            additionalExtensionPaths: [...this.additionalExtensionPaths],
            additionalSkillPaths: [...this.additionalSkillPaths],
            additionalPromptTemplatePaths: [...this.additionalPromptTemplatePaths],
            additionalThemePaths: [...this.additionalThemePaths],
            builtinPackagePaths: clonePackageSources(this.builtinPackagePaths),
            extensionFactories: [...this.extensionFactories],
            noExtensions: this.noExtensions,
            noSkills: this.noSkills,
            noPromptTemplates: this.noPromptTemplates,
            noThemes: this.noThemes,
            noContextFiles: this.noContextFiles,
            ...(this.systemPromptSource === undefined ? {} : { systemPrompt: this.systemPromptSource }),
            ...(this.appendSystemPromptSource === undefined ? {} : { appendSystemPrompt: [...this.appendSystemPromptSource] }),
            ...(this.trustedBorrowedProjectLocalSources === undefined
                ? {}
                : { trustedBorrowedProjectLocalSources: [...this.trustedBorrowedProjectLocalSources] }),
        };
    }
    async refreshWorkflowResources() {
        const { resolvedPaths, cliExtensionPaths, builtinPackagePaths } = await resolvePackageResourcePaths(this, {
            trustedBorrowedProjectLocalSources: this.trustedBorrowedProjectLocalSources,
        });
        const workflowResources = collectWorkflowResources(resolvedPaths, cliExtensionPaths, builtinPackagePaths);
        this.workflowResources = workflowResources;
        return [...this.workflowResources];
    }
    async extendResources(paths) {
        const skillPaths = normalizeExtensionPaths(this, paths.skillPaths ?? []);
        const promptPaths = normalizeExtensionPaths(this, paths.promptPaths ?? []);
        const themePaths = normalizeExtensionPaths(this, paths.themePaths ?? []);
        recordExtensionSourceInfo(this, skillPaths, "skill");
        recordExtensionSourceInfo(this, promptPaths, "prompt");
        recordExtensionSourceInfo(this, themePaths, "theme");
        if (skillPaths.length > 0) {
            this.lastSkillPaths = mergeResourcePaths(this.cwd, this.lastSkillPaths, skillPaths.map((entry) => entry.path));
            await updateSkillsFromPathsAsync(this, this.lastSkillPaths);
        }
        if (promptPaths.length > 0) {
            this.lastPromptPaths = mergeResourcePaths(this.cwd, this.lastPromptPaths, promptPaths.map((entry) => entry.path));
            await updatePromptsFromPathsAsync(this, this.lastPromptPaths);
        }
        if (themePaths.length > 0) {
            this.lastThemePaths = mergeResourcePaths(this.cwd, this.lastThemePaths, themePaths.map((entry) => entry.path));
            await updateThemesFromPathsAsync(this, this.lastThemePaths);
        }
    }
    async loadProjectTrustExtensions() {
        return loadProjectTrustExtensions(this);
    }
    async reload(options) {
        return reloadDefaultResourceLoader(this, options);
    }
    touchInternalFieldsForSplit() {
        void this.eventBus;
        void this.packageManager;
        void this.noContextFiles;
        void this.extensionsOverride;
        void this.skillsOverride;
        void this.promptsOverride;
        void this.themesOverride;
        void this.agentsFilesOverride;
        void this.systemPromptOverride;
        void this.appendSystemPromptOverride;
        void this.extensionSkillSourceInfos;
        void this.extensionPromptSourceInfos;
        void this.extensionThemeSourceInfos;
        void this.loaded;
    }
}
//# sourceMappingURL=resource-loader-core.js.map