"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    PlaygroundSDK: ()=>PlaygroundSDK
});
const constants_namespaceObject = require("@midscene/shared/constants");
const local_execution_js_namespaceObject = require("../adapters/local-execution.js");
const remote_execution_js_namespaceObject = require("../adapters/remote-execution.js");
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 local_execution_js_namespaceObject.LocalExecutionAdapter(agent, agentFactory);
            case 'remote-execution':
                {
                    const finalServerUrl = serverUrl || ('undefined' != typeof window && window.location.protocol.includes('http') ? window.location.origin : `http://localhost:${constants_namespaceObject.PLAYGROUND_SERVER_PORT}`);
                    return new remote_execution_js_namespaceObject.RemoteExecutionAdapter(finalServerUrl);
                }
            default:
                throw new Error(`Unsupported execution type: ${type}`);
        }
    }
    runtimeMetadataAdapter() {
        if (this.adapter instanceof local_execution_js_namespaceObject.LocalExecutionAdapter || this.adapter instanceof remote_execution_js_namespaceObject.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 remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.id;
        if (this.adapter instanceof local_execution_js_namespaceObject.LocalExecutionAdapter) return this.adapter.id;
    }
    async checkStatus() {
        if (this.adapter instanceof remote_execution_js_namespaceObject.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 remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.getTaskProgress(requestId);
        return {};
    }
    async cancelTask(requestId) {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.cancelTask(requestId);
        return {
            error: 'Cancel task not supported in local execution mode'
        };
    }
    onDumpUpdate(callback) {
        if (this.adapter instanceof local_execution_js_namespaceObject.LocalExecutionAdapter) this.adapter.onDumpUpdate(callback);
        else if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) this.adapter.onDumpUpdate(callback);
    }
    onProgressUpdate(callback) {
        if (this.adapter instanceof local_execution_js_namespaceObject.LocalExecutionAdapter) this.adapter.setProgressCallback(callback);
    }
    async cancelExecution(requestId) {
        if (this.adapter instanceof remote_execution_js_namespaceObject.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 local_execution_js_namespaceObject.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 local_execution_js_namespaceObject.LocalExecutionAdapter && this.adapter.getCurrentExecutionData) return await this.adapter.getCurrentExecutionData();
        return {
            dump: null,
            reportHTML: null
        };
    }
    async getScreenshot() {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.getScreenshot();
        return null;
    }
    async interact(payload) {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.interact(payload);
        return {
            ok: false,
            error: 'Direct interaction requires remote execution'
        };
    }
    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 remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.getSessionInfo();
        return null;
    }
    async getSessionSetup(input) {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.getSessionSetup(input);
        return null;
    }
    async listSessionTargets() {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.listSessionTargets();
        return [];
    }
    async createSession(input) {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.createSession(input);
        throw new Error('Session creation is only supported in server mode');
    }
    async destroySession() {
        if (this.adapter instanceof remote_execution_js_namespaceObject.RemoteExecutionAdapter) return this.adapter.destroySession();
        throw new Error('Session destruction is only supported in server mode');
    }
    getServiceMode() {
        if (this.adapter instanceof local_execution_js_namespaceObject.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);
    }
}
exports.PlaygroundSDK = __webpack_exports__.PlaygroundSDK;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "PlaygroundSDK"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=index.js.map