import { PLAYGROUND_SERVER_PORT } from "@midscene/shared/constants";
import { LocalExecutionAdapter } from "../adapters/local-execution.mjs";
import { RemoteExecutionAdapter } from "../adapters/remote-execution.mjs";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
class PlaygroundSDK {
    createAdapter(type, serverUrl, agent, agentFactory) {
        switch(type){
            case 'local-execution':
                if (!agent && !agentFactory) throw new Error('Agent or agentFactory is required for local execution');
                return new LocalExecutionAdapter(agent, agentFactory);
            case 'remote-execution':
                {
                    const finalServerUrl = serverUrl || ('undefined' != typeof window && window.location.protocol.includes('http') ? window.location.origin : `http://localhost:${PLAYGROUND_SERVER_PORT}`);
                    return new RemoteExecutionAdapter(finalServerUrl);
                }
            default:
                throw new Error(`Unsupported execution type: ${type}`);
        }
    }
    runtimeMetadataAdapter() {
        if (this.adapter instanceof LocalExecutionAdapter || this.adapter instanceof RemoteExecutionAdapter) return this.adapter;
        return null;
    }
    async executeAction(actionType, value, options) {
        await this.beforeActionHook?.(actionType, value, options);
        const result = await this.adapter.executeAction(actionType, value, options);
        return result;
    }
    setBeforeActionHook(hook) {
        this.beforeActionHook = hook;
    }
    async getActionSpace(context) {
        return this.adapter.getActionSpace(context);
    }
    validateStructuredParams(value, action) {
        return this.adapter.validateParams(value, action);
    }
    formatErrorMessage(error) {
        return this.adapter.formatErrorMessage(error);
    }
    createDisplayContent(value, needsStructuredParams, action) {
        return this.adapter.createDisplayContent(value, needsStructuredParams, action);
    }
    get id() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.id;
        if (this.adapter instanceof LocalExecutionAdapter) return this.adapter.id;
    }
    async checkStatus() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.checkStatus();
        return true;
    }
    async overrideConfig(aiConfig) {
        return this.adapter.overrideConfig(aiConfig);
    }
    async runConnectivityTest() {
        return this.adapter.runConnectivityTest();
    }
    async getTaskProgress(requestId) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getTaskProgress(requestId);
        return {};
    }
    async cancelTask(requestId) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.cancelTask(requestId);
        return {
            error: 'Cancel task not supported in local execution mode'
        };
    }
    onDumpUpdate(callback) {
        if (this.adapter instanceof LocalExecutionAdapter) this.adapter.onDumpUpdate(callback);
        else if (this.adapter instanceof RemoteExecutionAdapter) this.adapter.onDumpUpdate(callback);
    }
    onProgressUpdate(callback) {
        if (this.adapter instanceof LocalExecutionAdapter) this.adapter.setProgressCallback(callback);
    }
    async cancelExecution(requestId) {
        if (this.adapter instanceof RemoteExecutionAdapter) {
            const result = await this.adapter.cancelTask(requestId);
            if (result.success) return {
                dump: result.dump || null,
                reportHTML: result.reportHTML || null
            };
        } else if (this.adapter instanceof LocalExecutionAdapter) {
            const result = await this.adapter.cancelTask(requestId);
            if (result.success) return {
                dump: result.dump || null,
                reportHTML: result.reportHTML || null
            };
        }
        return null;
    }
    async getCurrentExecutionData() {
        if (this.adapter instanceof LocalExecutionAdapter && this.adapter.getCurrentExecutionData) return await this.adapter.getCurrentExecutionData();
        return {
            dump: null,
            reportHTML: null
        };
    }
    async getScreenshot() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getScreenshot();
        return null;
    }
    async interact(payload) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.interact(payload);
        return {
            ok: false,
            error: 'Direct interaction requires remote execution'
        };
    }
    async startRecorderSession(sessionId) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.startRecorderSession(sessionId);
        return {
            ok: false,
            supported: false,
            error: 'Recorder requires remote execution'
        };
    }
    async getRecorderCapabilities() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getRecorderCapabilities();
        return {
            supported: false,
            source: 'unsupported',
            error: 'Recorder requires remote execution'
        };
    }
    async stopRecorderSession() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.stopRecorderSession();
        return {
            ok: true
        };
    }
    async getRecorderEvents(since = 0) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getRecorderEvents(since);
        return {
            events: [],
            nextIndex: since
        };
    }
    async getInterfaceInfo() {
        const adapter = this.runtimeMetadataAdapter();
        if (!adapter) return null;
        return adapter.getInterfaceInfo();
    }
    async getRuntimeInfo() {
        const adapter = this.runtimeMetadataAdapter();
        if (!adapter) return null;
        return adapter.getRuntimeInfo();
    }
    async getSessionInfo() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getSessionInfo();
        return null;
    }
    async getSessionSetup(input) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getSessionSetup(input);
        return null;
    }
    async listSessionTargets() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.listSessionTargets();
        return [];
    }
    async createSession(input) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.createSession(input);
        throw new Error('Session creation is only supported in server mode');
    }
    async destroySession() {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.destroySession();
        throw new Error('Session destruction is only supported in server mode');
    }
    getServiceMode() {
        if (this.adapter instanceof LocalExecutionAdapter) return 'In-Browser-Extension';
        return 'Server';
    }
    constructor(config){
        _define_property(this, "adapter", void 0);
        _define_property(this, "beforeActionHook", void 0);
        this.adapter = this.createAdapter(config.type, config.serverUrl, config.agent, config.agentFactory);
    }
}
export { PlaygroundSDK };

//# sourceMappingURL=index.mjs.map