import { execCommand } from "../exec.js";
import { emptyWorkflowResourceProvider, normalizeWorkflowResourceProvider, } from "./loader-resources.js";
/**
 * Create the ExtensionAPI for an extension.
 * Registration methods write to the extension object.
 * Action methods delegate to the shared runtime.
 */
export function createExtensionAPI(extension, runtime, cwd, eventBus, workflowResourceProvider = emptyWorkflowResourceProvider, resourceLoaderInheritanceSnapshotProvider) {
    const workflowResources = normalizeWorkflowResourceProvider(workflowResourceProvider);
    const api = {
        on(event, handler) {
            runtime.assertActive();
            const list = extension.handlers.get(event) ?? [];
            list.push(handler);
            extension.handlers.set(event, list);
        },
        registerTool(tool) {
            runtime.assertActive();
            extension.tools.set(tool.name, {
                definition: tool,
                sourceInfo: extension.sourceInfo,
            });
            runtime.refreshTools();
        },
        registerCommand(name, options) {
            runtime.assertActive();
            extension.commands.set(name, {
                name,
                sourceInfo: extension.sourceInfo,
                ...options,
            });
        },
        registerShortcut(shortcut, options) {
            runtime.assertActive();
            extension.shortcuts.set(shortcut, {
                shortcut,
                extensionPath: extension.path,
                ...options,
            });
        },
        registerFlag(name, options) {
            runtime.assertActive();
            extension.flags.set(name, {
                name,
                extensionPath: extension.path,
                ...options,
            });
            if (options.default !== undefined && !runtime.flagValues.has(name)) {
                runtime.flagValues.set(name, options.default);
            }
        },
        registerMessageRenderer(customType, renderer) {
            runtime.assertActive();
            extension.messageRenderers.set(customType, renderer);
        },
        getFlag(name) {
            runtime.assertActive();
            if (!extension.flags.has(name))
                return undefined;
            return runtime.flagValues.get(name);
        },
        getWorkflowResources() {
            runtime.assertActive();
            return [...workflowResources.get()];
        },
        async refreshWorkflowResources() {
            runtime.assertActive();
            const refreshed = await workflowResources.refresh?.();
            return [...(refreshed ?? workflowResources.get())];
        },
        getResourceLoaderInheritanceSnapshot() {
            runtime.assertActive();
            return resourceLoaderInheritanceSnapshotProvider?.() ?? {};
        },
        sendMessage(message, options) {
            runtime.assertActive();
            runtime.sendMessage(message, options);
        },
        sendUserMessage(content, options) {
            runtime.assertActive();
            runtime.sendUserMessage(content, options);
        },
        appendEntry(customType, data) {
            runtime.assertActive();
            runtime.appendEntry(customType, data);
        },
        setSessionName(name) {
            runtime.assertActive();
            runtime.setSessionName(name);
        },
        getSessionName() {
            runtime.assertActive();
            return runtime.getSessionName();
        },
        setLabel(entryId, label) {
            runtime.assertActive();
            runtime.setLabel(entryId, label);
        },
        exec(command, args, options) {
            runtime.assertActive();
            return execCommand(command, args, options?.cwd ?? cwd, options);
        },
        getActiveTools() {
            runtime.assertActive();
            return runtime.getActiveTools();
        },
        getAllTools() {
            runtime.assertActive();
            return runtime.getAllTools();
        },
        setActiveTools(toolNames) {
            runtime.assertActive();
            runtime.setActiveTools(toolNames);
        },
        getCommands() {
            runtime.assertActive();
            return runtime.getCommands();
        },
        setModel(model) {
            runtime.assertActive();
            return runtime.setModel(model);
        },
        getThinkingLevel() {
            runtime.assertActive();
            return runtime.getThinkingLevel();
        },
        setThinkingLevel(level) {
            runtime.assertActive();
            runtime.setThinkingLevel(level);
        },
        registerProvider(name, config) {
            runtime.assertActive();
            runtime.registerProvider(name, config, extension.path);
        },
        unregisterProvider(name) {
            runtime.assertActive();
            runtime.unregisterProvider(name, extension.path);
        },
        events: eventBus,
    };
    return api;
}
//# sourceMappingURL=loader-api.js.map