"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__, {
    ReportGenerator: ()=>ReportGenerator,
    nullReportGenerator: ()=>nullReportGenerator
});
const external_node_fs_namespaceObject = require("node:fs");
const external_node_path_namespaceObject = require("node:path");
const common_namespaceObject = require("@midscene/shared/common");
const env_namespaceObject = require("@midscene/shared/env");
const utils_namespaceObject = require("@midscene/shared/utils");
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;
}
const nullReportGenerator = {
    onDumpUpdate: ()=>{},
    flush: async ()=>{},
    finalize: async ()=>void 0,
    getReportPath: ()=>void 0
};
class ReportGenerator {
    static create(reportFileName, opts) {
        if (false === opts.generateReport) return nullReportGenerator;
        if (utils_namespaceObject.ifInBrowser) return nullReportGenerator;
        if ('html-and-external-assets' === opts.outputFormat) {
            const outputDir = (0, external_node_path_namespaceObject.join)((0, common_namespaceObject.getMidsceneRunSubDir)('report'), reportFileName);
            return new ReportGenerator({
                reportPath: (0, external_node_path_namespaceObject.join)(outputDir, 'index.html'),
                screenshotMode: 'directory',
                autoPrint: opts.autoPrintReportMsg
            });
        }
        return new ReportGenerator({
            reportPath: (0, external_node_path_namespaceObject.join)((0, common_namespaceObject.getMidsceneRunSubDir)('report'), `${reportFileName}.html`),
            screenshotMode: 'inline',
            autoPrint: opts.autoPrintReportMsg
        });
    }
    onDumpUpdate(dump) {
        this.writeQueue = this.writeQueue.then(()=>{
            if (this.destroyed) return;
            this.doWrite(dump);
        });
    }
    async flush() {
        await this.writeQueue;
    }
    async finalize(dump) {
        this.onDumpUpdate(dump);
        await this.flush();
        this.destroyed = true;
        this.printReportPath('finalized');
        return this.reportPath;
    }
    getReportPath() {
        return this.reportPath;
    }
    printReportPath(verb) {
        if (!this.autoPrint || !this.reportPath) return;
        if (env_namespaceObject.globalConfigManager.getEnvConfigInBoolean(env_namespaceObject.MIDSCENE_REPORT_QUIET)) return;
        if ('directory' === this.screenshotMode) (0, utils_namespaceObject.logMsg)(`Midscene - report ${verb}: npx serve ${(0, external_node_path_namespaceObject.dirname)(this.reportPath)}`);
        else (0, utils_namespaceObject.logMsg)(`Midscene - report ${verb}: ${this.reportPath}`);
    }
    doWrite(dump) {
        if ('inline' === this.screenshotMode) this.writeInlineReport(dump);
        else this.writeDirectoryReport(dump);
        if (!this.firstWriteDone) {
            this.firstWriteDone = true;
            this.printReportPath('generated');
        }
    }
    writeInlineReport(dump) {
        const dir = (0, external_node_path_namespaceObject.dirname)(this.reportPath);
        if (!(0, external_node_fs_namespaceObject.existsSync)(dir)) (0, external_node_fs_namespaceObject.mkdirSync)(dir, {
            recursive: true
        });
        if (!this.initialized) {
            (0, external_node_fs_namespaceObject.writeFileSync)(this.reportPath, (0, external_utils_js_namespaceObject.getReportTpl)());
            this.imageEndOffset = (0, external_node_fs_namespaceObject.statSync)(this.reportPath).size;
            this.initialized = true;
        }
        (0, external_node_fs_namespaceObject.truncateSync)(this.reportPath, this.imageEndOffset);
        const screenshots = dump.collectAllScreenshots();
        for (const screenshot of screenshots)if (!this.writtenScreenshots.has(screenshot.id)) {
            (0, external_utils_js_namespaceObject.appendFileSync)(this.reportPath, `\n${(0, html_utils_js_namespaceObject.generateImageScriptTag)(screenshot.id, screenshot.base64)}`);
            this.writtenScreenshots.add(screenshot.id);
            screenshot.markPersistedInline(this.reportPath);
        }
        this.imageEndOffset = (0, external_node_fs_namespaceObject.statSync)(this.reportPath).size;
        const serialized = dump.serialize();
        (0, external_utils_js_namespaceObject.appendFileSync)(this.reportPath, `\n${(0, html_utils_js_namespaceObject.generateDumpScriptTag)(serialized)}`);
    }
    writeDirectoryReport(dump) {
        const dir = (0, external_node_path_namespaceObject.dirname)(this.reportPath);
        if (!(0, external_node_fs_namespaceObject.existsSync)(dir)) (0, external_node_fs_namespaceObject.mkdirSync)(dir, {
            recursive: true
        });
        const screenshotsDir = (0, external_node_path_namespaceObject.join)(dir, 'screenshots');
        if (!(0, external_node_fs_namespaceObject.existsSync)(screenshotsDir)) (0, external_node_fs_namespaceObject.mkdirSync)(screenshotsDir, {
            recursive: true
        });
        const screenshots = dump.collectAllScreenshots();
        for (const screenshot of screenshots)if (!this.writtenScreenshots.has(screenshot.id)) {
            const ext = screenshot.extension;
            const absolutePath = (0, external_node_path_namespaceObject.join)(screenshotsDir, `${screenshot.id}.${ext}`);
            const buffer = Buffer.from(screenshot.rawBase64, 'base64');
            (0, external_node_fs_namespaceObject.writeFileSync)(absolutePath, buffer);
            this.writtenScreenshots.add(screenshot.id);
            screenshot.markPersistedToPath(`./screenshots/${screenshot.id}.${ext}`, absolutePath);
        }
        const serialized = dump.serialize();
        (0, external_node_fs_namespaceObject.writeFileSync)(this.reportPath, `${(0, external_utils_js_namespaceObject.getReportTpl)()}${(0, html_utils_js_namespaceObject.getBaseUrlFixScript)()}${(0, html_utils_js_namespaceObject.generateDumpScriptTag)(serialized)}`);
    }
    constructor(options){
        _define_property(this, "reportPath", void 0);
        _define_property(this, "screenshotMode", void 0);
        _define_property(this, "autoPrint", void 0);
        _define_property(this, "writtenScreenshots", new Set());
        _define_property(this, "firstWriteDone", false);
        _define_property(this, "imageEndOffset", 0);
        _define_property(this, "initialized", false);
        _define_property(this, "writeQueue", Promise.resolve());
        _define_property(this, "destroyed", false);
        this.reportPath = options.reportPath;
        this.screenshotMode = options.screenshotMode;
        this.autoPrint = options.autoPrint ?? true;
        this.printReportPath('will be generated at');
    }
}
exports.ReportGenerator = __webpack_exports__.ReportGenerator;
exports.nullReportGenerator = __webpack_exports__.nullReportGenerator;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ReportGenerator",
    "nullReportGenerator"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=report-generator.js.map