import { createExtensionRuntime } from "./extensions/loader.js";
import { createSourceInfo } from "./source-info.js";
import { resourceInternals } from "./resource-loader-internals.js";
import { resolveResourcePath } from "./resource-loader-paths.js";
export function emptyResolvedPaths() {
    return { extensions: [], skills: [], prompts: [], themes: [], workflows: [] };
}
export async function resolvePackageResourcePaths(loader, options) {
    const state = resourceInternals(loader);
    await state.settingsManager.reload();
    const resolvedPaths = await state.packageManager.resolve();
    const includeCliProjectLocalResources = options?.includeCliProjectLocalResources ?? true;
    let cliExtensionPaths = await state.packageManager.resolveExtensionSources(state.additionalExtensionPaths, {
        temporary: true,
        includeProjectLocalResources: includeCliProjectLocalResources,
    });
    if (includeCliProjectLocalResources && options?.trustedBorrowedProjectLocalSources) {
        cliExtensionPaths = filterBorrowedProjectLocalResources(cliExtensionPaths, options.trustedBorrowedProjectLocalSources);
    }
    const builtinPackagePaths = state.builtinPackagePaths.length > 0
        ? await state.packageManager.resolveExtensionSources(state.builtinPackagePaths, { temporary: true })
        : emptyResolvedPaths();
    return { resolvedPaths, cliExtensionPaths, builtinPackagePaths };
}
export async function resolveTrustedBorrowedProjectLocalSources(loader, resolveBorrowedProjectTrust, preTrustExtensions) {
    const state = resourceInternals(loader);
    const cliExtensionPaths = await state.packageManager.resolveExtensionSources(state.additionalExtensionPaths, {
        temporary: true,
        includeProjectLocalResources: true,
    });
    const resourcesBySource = new Map();
    for (const resources of Object.values(cliExtensionPaths)) {
        for (const resource of resources) {
            if (!resource.metadata.borrowedProjectLocal) {
                continue;
            }
            const sourceResources = resourcesBySource.get(resource.metadata.source) ?? [];
            sourceResources.push(resource);
            resourcesBySource.set(resource.metadata.source, sourceResources);
        }
    }
    const trustedSources = new Set();
    for (const [source, resources] of resourcesBySource) {
        const trusted = await resolveBorrowedProjectTrust({
            source,
            resources,
            extensionsResult: preTrustExtensions ?? { extensions: [], errors: [], runtime: createExtensionRuntime() },
        });
        if (trusted) {
            trustedSources.add(source);
        }
    }
    return trustedSources;
}
export function filterBorrowedProjectLocalResources(paths, trustedSources) {
    const filterResources = (resources) => resources.filter((resource) => !resource.metadata.borrowedProjectLocal || trustedSources.has(resource.metadata.source));
    return {
        extensions: filterResources(paths.extensions),
        skills: filterResources(paths.skills),
        prompts: filterResources(paths.prompts),
        themes: filterResources(paths.themes),
        workflows: filterResources(paths.workflows),
    };
}
function enabledWorkflowResources(resources) {
    return resources.filter((resource) => resource.enabled);
}
function enabledPackageWorkflowResources(resources) {
    return resources.filter((resource) => resource.enabled && resource.metadata.origin === "package");
}
export function collectWorkflowResources(resolvedPaths, cliExtensionPaths, builtinPackagePaths) {
    return [
        ...enabledWorkflowResources(cliExtensionPaths.workflows),
        ...enabledPackageWorkflowResources(resolvedPaths.workflows),
        ...enabledPackageWorkflowResources(builtinPackagePaths.workflows),
    ];
}
export function createWorkflowResourceProvider(loader) {
    const state = resourceInternals(loader);
    return {
        get: () => state.workflowResources,
        refresh: () => state.refreshWorkflowResources(),
    };
}
export function createInheritanceSnapshotProvider(loader) {
    const state = resourceInternals(loader);
    return () => state.getInheritanceSnapshot();
}
export function normalizeExtensionPaths(loader, entries) {
    const state = resourceInternals(loader);
    return entries.map((entry) => {
        const metadata = entry.metadata.baseDir
            ? { ...entry.metadata, baseDir: resolveResourcePath(state.cwd, entry.metadata.baseDir) }
            : entry.metadata;
        return {
            path: resolveResourcePath(state.cwd, entry.path),
            metadata,
        };
    });
}
export function recordExtensionSourceInfo(loader, paths, kind) {
    const state = resourceInternals(loader);
    const target = kind === "skill"
        ? state.extensionSkillSourceInfos
        : kind === "prompt"
            ? state.extensionPromptSourceInfos
            : state.extensionThemeSourceInfos;
    for (const entry of paths) {
        target.set(entry.path, createSourceInfo(entry.path, entry.metadata));
    }
}
//# sourceMappingURL=resource-loader-package-resources.js.map