"use strict";
var __webpack_modules__ = {
    "./dump/image-restoration" (module) {
        module.exports = require("./dump/image-restoration.js");
    },
    "./screenshot-item" (module) {
        module.exports = require("./screenshot-item.js");
    },
    "./yaml" (module) {
        module.exports = require("./yaml.js");
    },
    "node:fs" (module) {
        module.exports = require("node:fs");
    },
    "node:path" (module) {
        module.exports = require("node:path");
    }
};
var __webpack_module_cache__ = {};
function __webpack_require__(moduleId) {
    var cachedModule = __webpack_module_cache__[moduleId];
    if (void 0 !== cachedModule) return cachedModule.exports;
    var module = __webpack_module_cache__[moduleId] = {
        exports: {}
    };
    __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
    return module.exports;
}
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
        ExecutionDump: ()=>ExecutionDump,
        GroupedActionDump: ()=>GroupedActionDump,
        ServiceError: ()=>ServiceError,
        UIContext: ()=>UIContext
    });
    var node_fs__rspack_import_0 = __webpack_require__("node:fs");
    var node_path__rspack_import_1 = __webpack_require__("node:path");
    var _dump_image_restoration__rspack_import_2 = __webpack_require__("./dump/image-restoration");
    var _screenshot_item__rspack_import_3 = __webpack_require__("./screenshot-item");
    var _yaml__rspack_import_4 = __webpack_require__("./yaml");
    var __rspack_reexport = {};
    for(const __rspack_import_key in _yaml__rspack_import_4)if ([
        "UIContext",
        "GroupedActionDump",
        "default",
        "ExecutionDump",
        "ServiceError"
    ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_yaml__rspack_import_4[__rspack_import_key];
    __webpack_require__.d(__webpack_exports__, __rspack_reexport);
    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 UIContext {
    }
    class ServiceError extends Error {
        constructor(message, dump){
            super(message), _define_property(this, "dump", void 0);
            this.name = 'ServiceError';
            this.dump = dump;
        }
    }
    function replacerForDumpSerialization(_key, value) {
        if (value && value.constructor?.name === 'Page') return '[Page object]';
        if (value && value.constructor?.name === 'Browser') return '[Browser object]';
        if (value && 'function' == typeof value.toSerializable) return value.toSerializable();
        return value;
    }
    function reviverForDumpDeserialization(key, value) {
        if ('screenshot' !== key || 'object' != typeof value || null === value) return value;
        if (_screenshot_item__rspack_import_3.ScreenshotItem.isSerialized(value)) return value;
        'base64' in value && value.base64;
        return value;
    }
    class ExecutionDump {
        serialize(indents) {
            return JSON.stringify(this.toJSON(), replacerForDumpSerialization, indents);
        }
        toJSON() {
            return {
                logTime: this.logTime,
                name: this.name,
                description: this.description,
                tasks: this.tasks.map((task)=>({
                        ...task,
                        recorder: task.recorder || []
                    })),
                aiActContext: this.aiActContext
            };
        }
        static fromSerializedString(serialized) {
            const parsed = JSON.parse(serialized, reviverForDumpDeserialization);
            return new ExecutionDump(parsed);
        }
        static fromJSON(data) {
            return new ExecutionDump(data);
        }
        collectScreenshots() {
            const screenshots = [];
            for (const task of this.tasks){
                if (task.uiContext?.screenshot instanceof _screenshot_item__rspack_import_3.ScreenshotItem) screenshots.push(task.uiContext.screenshot);
                if (task.recorder) {
                    for (const record of task.recorder)if (record.screenshot instanceof _screenshot_item__rspack_import_3.ScreenshotItem) screenshots.push(record.screenshot);
                }
            }
            return screenshots;
        }
        constructor(data){
            _define_property(this, "logTime", void 0);
            _define_property(this, "name", void 0);
            _define_property(this, "description", void 0);
            _define_property(this, "tasks", void 0);
            _define_property(this, "aiActContext", void 0);
            this.logTime = data.logTime;
            this.name = data.name;
            this.description = data.description;
            this.tasks = data.tasks;
            this.aiActContext = data.aiActContext;
        }
    }
    class GroupedActionDump {
        serialize(indents) {
            return JSON.stringify(this.toJSON(), replacerForDumpSerialization, indents);
        }
        serializeWithInlineScreenshots(indents) {
            const processValue = (obj)=>{
                if (obj instanceof _screenshot_item__rspack_import_3.ScreenshotItem) return {
                    base64: obj.base64
                };
                if (Array.isArray(obj)) return obj.map(processValue);
                if (obj && 'object' == typeof obj) {
                    const entries = Object.entries(obj).map(([key, value])=>[
                            key,
                            processValue(value)
                        ]);
                    return Object.fromEntries(entries);
                }
                return obj;
            };
            const data = processValue(this.toJSON());
            return JSON.stringify(data, null, indents);
        }
        toJSON() {
            return {
                sdkVersion: this.sdkVersion,
                groupName: this.groupName,
                groupDescription: this.groupDescription,
                modelBriefs: this.modelBriefs,
                executions: this.executions.map((exec)=>exec.toJSON())
            };
        }
        static fromSerializedString(serialized) {
            const parsed = JSON.parse(serialized, reviverForDumpDeserialization);
            return new GroupedActionDump(parsed);
        }
        static fromJSON(data) {
            return new GroupedActionDump(data);
        }
        collectAllScreenshots() {
            const screenshots = [];
            for (const execution of this.executions)screenshots.push(...execution.collectScreenshots());
            return screenshots;
        }
        serializeToFiles(basePath) {
            const screenshotsDir = `${basePath}.screenshots`;
            if (!(0, node_fs__rspack_import_0.existsSync)(screenshotsDir)) (0, node_fs__rspack_import_0.mkdirSync)(screenshotsDir, {
                recursive: true
            });
            const screenshotMap = {};
            const screenshots = this.collectAllScreenshots();
            for (const screenshot of screenshots)if (screenshot.hasBase64()) {
                const imagePath = (0, node_path__rspack_import_1.join)(screenshotsDir, `${screenshot.id}.${screenshot.extension}`);
                const rawBase64 = screenshot.rawBase64;
                (0, node_fs__rspack_import_0.writeFileSync)(imagePath, Buffer.from(rawBase64, 'base64'));
                screenshotMap[screenshot.id] = imagePath;
            }
            (0, node_fs__rspack_import_0.writeFileSync)(`${basePath}.screenshots.json`, JSON.stringify(screenshotMap), 'utf-8');
            (0, node_fs__rspack_import_0.writeFileSync)(basePath, this.serialize(), 'utf-8');
        }
        static fromFilesAsInlineJson(basePath) {
            const dumpString = (0, node_fs__rspack_import_0.readFileSync)(basePath, 'utf-8');
            const screenshotsMapPath = `${basePath}.screenshots.json`;
            if (!(0, node_fs__rspack_import_0.existsSync)(screenshotsMapPath)) return dumpString;
            const screenshotMap = JSON.parse((0, node_fs__rspack_import_0.readFileSync)(screenshotsMapPath, 'utf-8'));
            const imageMap = {};
            for (const [id, filePath] of Object.entries(screenshotMap))if ((0, node_fs__rspack_import_0.existsSync)(filePath)) {
                const data = (0, node_fs__rspack_import_0.readFileSync)(filePath);
                const mime = filePath.endsWith('.jpeg') || filePath.endsWith('.jpg') ? 'jpeg' : 'png';
                imageMap[id] = `data:image/${mime};base64,${data.toString('base64')}`;
            }
            const dumpData = JSON.parse(dumpString);
            const processedData = (0, _dump_image_restoration__rspack_import_2.restoreImageReferences)(dumpData, imageMap);
            return JSON.stringify(processedData);
        }
        static cleanupFiles(basePath) {
            const filesToClean = [
                basePath,
                `${basePath}.screenshots.json`,
                `${basePath}.screenshots`
            ];
            for (const filePath of filesToClean)try {
                (0, node_fs__rspack_import_0.rmSync)(filePath, {
                    force: true,
                    recursive: true
                });
            } catch  {}
        }
        static getFilePaths(basePath) {
            return [
                basePath,
                `${basePath}.screenshots.json`,
                `${basePath}.screenshots`
            ];
        }
        constructor(data){
            _define_property(this, "sdkVersion", void 0);
            _define_property(this, "groupName", void 0);
            _define_property(this, "groupDescription", void 0);
            _define_property(this, "modelBriefs", void 0);
            _define_property(this, "executions", void 0);
            this.sdkVersion = data.sdkVersion;
            this.groupName = data.groupName;
            this.groupDescription = data.groupDescription;
            this.modelBriefs = data.modelBriefs;
            this.executions = data.executions.map((exec)=>exec instanceof ExecutionDump ? exec : ExecutionDump.fromJSON(exec));
        }
    }
})();
exports.ExecutionDump = __webpack_exports__.ExecutionDump;
exports.GroupedActionDump = __webpack_exports__.GroupedActionDump;
exports.ServiceError = __webpack_exports__.ServiceError;
exports.UIContext = __webpack_exports__.UIContext;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ExecutionDump",
    "GroupedActionDump",
    "ServiceError",
    "UIContext"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=types.js.map