import { createExtensionRuntime, ExtensionRunner as ExtensionRunnerClass, } from "@sheason/pi-coding-agent";
const REMOTE_MODE_ERROR = "not available in remote mode";
function createStubSessionManager(getView, cwd) {
    return {
        getCwd: () => cwd,
        getSessionDir: () => cwd,
        getSessionId: () => getView().footer.sessionId ?? "remote",
        getSessionFile: () => getView().session?.sessionFile,
        getLeafId: () => null,
        getLeafEntry: () => undefined,
        getEntry: () => undefined,
        getLabel: () => undefined,
        getBranch: () => [],
        getHeader: () => getView().session?.header ?? null,
        getEntries: () => [],
        getTree: () => [],
        getSessionName: () => undefined,
    };
}
function createStubModelRegistry() {
    return {
        registerProvider: () => { },
        unregisterProvider: () => { },
        getAll: () => [],
        getAvailable: () => [],
        find: () => undefined,
        hasConfiguredAuth: () => false,
        getApiKeyAndHeaders: async () => {
            throw new Error(`Auth ${REMOTE_MODE_ERROR}`);
        },
        getProviderAuthStatus: () => ({ configured: false }),
        getProviderDisplayName: (p) => p,
        getApiKeyForProvider: async () => {
            throw new Error(`Auth ${REMOTE_MODE_ERROR}`);
        },
        isUsingOAuth: () => false,
        refresh: () => {
            throw new Error(`Model refresh ${REMOTE_MODE_ERROR}`);
        },
        getError: () => undefined,
    };
}
function createExtensionActions(deps) {
    const { getView, actions } = deps;
    return {
        sendMessage: (message, _options) => {
            const text = typeof message.content === "string" ? message.content : JSON.stringify(message.content);
            void (actions.queueWrite ?? actions.submitPrompt)(text);
        },
        sendUserMessage: (content, _options) => {
            const text = typeof content === "string" ? content : JSON.stringify(content);
            void (actions.queueWrite ?? actions.submitPrompt)(text);
        },
        appendEntry: () => {
            throw new Error(`appendEntry ${REMOTE_MODE_ERROR}`);
        },
        setSessionName: () => {
            throw new Error(`setSessionName ${REMOTE_MODE_ERROR}`);
        },
        getSessionName: () => getView().session?.header?.id ?? undefined,
        setLabel: () => {
            throw new Error(`setLabel ${REMOTE_MODE_ERROR}`);
        },
        getActiveTools: () => [],
        getAllTools: () => [],
        setActiveTools: () => {
            throw new Error(`setActiveTools ${REMOTE_MODE_ERROR}`);
        },
        refreshTools: () => {
            throw new Error(`refreshTools ${REMOTE_MODE_ERROR}`);
        },
        getCommands: () => getView().commands.map((cmd) => ({
            name: cmd.name,
            description: cmd.description,
            source: "extension",
            sourceInfo: { path: "remote", source: "remote", scope: "temporary", origin: "package" },
        })),
        setModel: async (model) => {
            await actions.setModel(`${model.provider}/${model.id}`);
            return true;
        },
        getThinkingLevel: () => (getView().session?.thinkingLevel ?? "medium"),
        setThinkingLevel: (level) => {
            void actions.setThinkingLevel(level);
        },
    };
}
function createExtensionContextActions(deps, abortController, onShutdown) {
    const { getView, actions } = deps;
    return {
        getModel: () => getView().session?.model,
        isIdle: () => !getView().session?.isRunning,
        getSignal: () => abortController.signal,
        abort: () => {
            abortController.abort();
            void actions.abort();
        },
        hasPendingMessages: () => (getView().session?.queuedMessages?.length ?? 0) > 0,
        shutdown: onShutdown,
        getContextUsage: () => getView().footer.contextUsage ?? undefined,
        compact: (_options) => {
            void actions.invokeCommand("compact");
        },
        getSystemPrompt: () => {
            throw new Error(`systemPrompt ${REMOTE_MODE_ERROR}`);
        },
    };
}
export async function createPeerExtensionRunner(deps) {
    const runtime = createExtensionRuntime();
    // Load the d-pi peer extension (empty for now — will be populated in later phases)
    // For MVP, we create the runner infrastructure without loading any extension factories.
    // Extensions will be loaded via loadExtensionFromFactory() in subsequent iterations.
    const extensions = [];
    const stubSessionManager = createStubSessionManager(deps.getView, deps.cwd);
    const stubModelRegistry = createStubModelRegistry();
    const runner = new ExtensionRunnerClass(extensions, runtime, deps.cwd, stubSessionManager, stubModelRegistry);
    const abortController = new AbortController();
    const extensionActions = createExtensionActions(deps);
    const extensionContextActions = createExtensionContextActions(deps, abortController, () => {
        runner.invalidate("Peer session shutdown");
    });
    runner.bindCore(extensionActions, extensionContextActions);
    return runner;
}
//# sourceMappingURL=peer-extension-runner.js.map