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) {
        switch(type){
            case 'local-execution':
                if (!agent) throw new Error('Agent is required for local execution');
                return new LocalExecutionAdapter(agent);
            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}`);
        }
    }
    async executeAction(actionType, value, options) {
        const result = await this.adapter.executeAction(actionType, value, options);
        return result;
    }
    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) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.overrideConfig(aiConfig);
    }
    async getTaskProgress(requestId) {
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getTaskProgress(requestId);
        return {
            tip: void 0
        };
    }
    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);
    }
    onProgressUpdate(callback) {
        if (this.adapter instanceof RemoteExecutionAdapter) this.adapter.setProgressCallback(callback);
        else if (this.adapter instanceof LocalExecutionAdapter) this.adapter.setProgressCallback(callback);
    }
    async cancelExecution(requestId) {
        if (this.adapter instanceof RemoteExecutionAdapter) await this.adapter.cancelTask(requestId);
        else if (this.adapter instanceof LocalExecutionAdapter) console.warn('Local execution cancellation not fully implemented');
    }
    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 getInterfaceInfo() {
        if (this.adapter instanceof LocalExecutionAdapter) return this.adapter.getInterfaceInfo();
        if (this.adapter instanceof RemoteExecutionAdapter) return this.adapter.getInterfaceInfo();
        return null;
    }
    getServiceMode() {
        if (this.adapter instanceof LocalExecutionAdapter) return 'In-Browser-Extension';
        return 'Server';
    }
    constructor(config){
        _define_property(this, "adapter", void 0);
        this.adapter = this.createAdapter(config.type, config.serverUrl, config.agent);
    }
}
export { PlaygroundSDK };

//# sourceMappingURL=index.mjs.map