export class OpenCodeRuntimeProvider {
    binaryManager;
    processManager;
    constructor(binaryManager, processManager) {
        this.binaryManager = binaryManager;
        this.processManager = processManager;
    }
    async status() {
        const binary = await this.binaryManager.status();
        const process = this.processManager?.status();
        const state = runtimeStateFrom(binary, process);
        const base = {
            provider: "opencode",
            state,
            binary,
            activeSessions: 0,
        };
        if (process)
            base.process = process;
        if (binary.state !== "verified") {
            base.lastError =
                "OpenCode runtime is intentionally disabled until a verified binary is available.";
        }
        else if (process?.lastError) {
            base.lastError = process.lastError;
        }
        return base;
    }
    async start(options = {}) {
        if (!this.processManager) {
            await this.binaryManager.resolveVerifiedBinary();
            throw new Error("OpenCode process manager is not configured.");
        }
        await this.processManager.start(options);
        return this.status();
    }
    async stop() {
        await this.processManager?.stop();
        return this.status();
    }
    async createSession(input = {}) {
        return this.runningClient().createSession(input);
    }
    async sendPromptAsync(sessionID, input) {
        await this.runningClient().sendPromptAsync(sessionID, input);
    }
    subscribeEvents(signal) {
        return this.runningClient().subscribeEvents(signal);
    }
    async respondToSessionPermission(sessionID, permissionID, response) {
        return this.runningClient().respondToSessionPermission(sessionID, permissionID, response);
    }
    async replyToPermission(requestID, reply, message) {
        return this.runningClient().replyToPermission(requestID, reply, message);
    }
    runningClient() {
        if (!this.processManager)
            throw new Error("OpenCode process manager is not configured.");
        return this.processManager.client();
    }
}
function runtimeStateFrom(binary, process) {
    if (binary.state !== "verified")
        return "blocked_unverified_binary";
    if (process?.state === "starting")
        return "starting";
    if (process?.state === "running")
        return "running";
    if (process?.state === "failed")
        return "failed";
    return "idle";
}
