"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__, {
    ReportMergingTool: ()=>ReportMergingTool
});
const external_node_fs_namespaceObject = require("node:fs");
const external_node_path_namespaceObject = require("node:path");
const common_namespaceObject = require("@midscene/shared/common");
const utils_namespaceObject = require("@midscene/shared/utils");
const index_js_namespaceObject = require("./agent/index.js");
const html_utils_js_namespaceObject = require("./dump/html-utils.js");
const external_utils_js_namespaceObject = require("./utils.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 ReportMergingTool {
    append(reportInfo) {
        this.reportInfos.push(reportInfo);
    }
    clear() {
        this.reportInfos = [];
    }
    isDirectoryModeReport(reportFilePath) {
        const reportDir = external_node_path_namespaceObject.dirname(reportFilePath);
        return 'index.html' === external_node_path_namespaceObject.basename(reportFilePath) && (0, external_node_fs_namespaceObject.existsSync)(external_node_path_namespaceObject.join(reportDir, 'screenshots'));
    }
    mergeReports(reportFileName = 'AUTO', opts) {
        if (this.reportInfos.length <= 1) {
            (0, utils_namespaceObject.logMsg)('Not enough reports to merge');
            return null;
        }
        const { rmOriginalReports = false, overwrite = false } = opts ?? {};
        const targetDir = (0, common_namespaceObject.getMidsceneRunSubDir)('report');
        const hasDirectoryModeReport = this.reportInfos.some((info)=>this.isDirectoryModeReport(info.reportFilePath));
        const resolvedName = 'AUTO' === reportFileName ? (0, index_js_namespaceObject.getReportFileName)('merged-report') : reportFileName;
        const outputFilePath = hasDirectoryModeReport ? external_node_path_namespaceObject.resolve(targetDir, resolvedName, 'index.html') : external_node_path_namespaceObject.resolve(targetDir, `${resolvedName}.html`);
        if ('AUTO' !== reportFileName && (0, external_node_fs_namespaceObject.existsSync)(outputFilePath)) {
            if (!overwrite) throw new Error(`Report file already exists: ${outputFilePath}\nSet overwrite to true to overwrite this file.`);
            if (hasDirectoryModeReport) (0, external_node_fs_namespaceObject.rmSync)(external_node_path_namespaceObject.dirname(outputFilePath), {
                recursive: true,
                force: true
            });
            else (0, external_node_fs_namespaceObject.unlinkSync)(outputFilePath);
        }
        if (hasDirectoryModeReport) (0, external_node_fs_namespaceObject.mkdirSync)(external_node_path_namespaceObject.dirname(outputFilePath), {
            recursive: true
        });
        (0, utils_namespaceObject.logMsg)(`Start merging ${this.reportInfos.length} reports...\nCreating template file...`);
        try {
            (0, external_node_fs_namespaceObject.appendFileSync)(outputFilePath, (0, external_utils_js_namespaceObject.getReportTpl)());
            if (hasDirectoryModeReport) (0, external_node_fs_namespaceObject.appendFileSync)(outputFilePath, html_utils_js_namespaceObject.BASE_URL_FIX_SCRIPT);
            for(let i = 0; i < this.reportInfos.length; i++){
                const reportInfo = this.reportInfos[i];
                (0, utils_namespaceObject.logMsg)(`Processing report ${i + 1}/${this.reportInfos.length}`);
                if (this.isDirectoryModeReport(reportInfo.reportFilePath)) {
                    const reportDir = external_node_path_namespaceObject.dirname(reportInfo.reportFilePath);
                    const screenshotsDir = external_node_path_namespaceObject.join(reportDir, 'screenshots');
                    const mergedScreenshotsDir = external_node_path_namespaceObject.join(external_node_path_namespaceObject.dirname(outputFilePath), 'screenshots');
                    (0, external_node_fs_namespaceObject.mkdirSync)(mergedScreenshotsDir, {
                        recursive: true
                    });
                    for (const file of (0, external_node_fs_namespaceObject.readdirSync)(screenshotsDir)){
                        const src = external_node_path_namespaceObject.join(screenshotsDir, file);
                        const dest = external_node_path_namespaceObject.join(mergedScreenshotsDir, file);
                        (0, external_node_fs_namespaceObject.copyFileSync)(src, dest);
                    }
                } else (0, html_utils_js_namespaceObject.streamImageScriptsToFile)(reportInfo.reportFilePath, outputFilePath);
                const dumpString = (0, html_utils_js_namespaceObject.extractLastDumpScriptSync)(reportInfo.reportFilePath);
                const { reportAttributes } = reportInfo;
                const reportHtmlStr = `${(0, external_utils_js_namespaceObject.reportHTMLContent)({
                    dumpString,
                    attributes: {
                        playwright_test_duration: reportAttributes.testDuration,
                        playwright_test_status: reportAttributes.testStatus,
                        playwright_test_title: reportAttributes.testTitle,
                        playwright_test_id: reportAttributes.testId,
                        playwright_test_description: reportAttributes.testDescription
                    }
                }, void 0, void 0, false)}\n`;
                (0, external_node_fs_namespaceObject.appendFileSync)(outputFilePath, reportHtmlStr);
            }
            (0, utils_namespaceObject.logMsg)(`Successfully merged new report: ${outputFilePath}`);
            if (rmOriginalReports) {
                for (const info of this.reportInfos)try {
                    if (this.isDirectoryModeReport(info.reportFilePath)) {
                        const reportDir = external_node_path_namespaceObject.dirname(info.reportFilePath);
                        (0, external_node_fs_namespaceObject.rmSync)(reportDir, {
                            recursive: true,
                            force: true
                        });
                    } else (0, external_node_fs_namespaceObject.unlinkSync)(info.reportFilePath);
                } catch (error) {
                    (0, utils_namespaceObject.logMsg)(`Error deleting report ${info.reportFilePath}: ${error}`);
                }
                (0, utils_namespaceObject.logMsg)(`Removed ${this.reportInfos.length} original reports`);
            }
            return outputFilePath;
        } catch (error) {
            (0, utils_namespaceObject.logMsg)(`Error in mergeReports: ${error}`);
            throw error;
        }
    }
    constructor(){
        _define_property(this, "reportInfos", []);
    }
}
exports.ReportMergingTool = __webpack_exports__.ReportMergingTool;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ReportMergingTool"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=report.js.map