"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveScreenshotsLocally = saveScreenshotsLocally;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const createDebugger_1 = __importDefault(require("../createDebugger"));
const debugLogger = (0, createDebugger_1.default)('saveScreenshotsLocally');
function saveScreenshotsLocally(results, screenshotDir) {
    try {
        fs_1.default.mkdirSync(screenshotDir, { recursive: true });
    }
    catch (error) {
        debugLogger('Failed to create screenshot directory: %s', error instanceof Error ? error.message : String(error));
        return;
    }
    for (const result of results) {
        if (result.screenshot_path) {
            debugLogger('Screenshot already saved: %s', result.screenshot_path);
            continue;
        }
        if (!result.screenshot || !result.screenshot_id) {
            continue;
        }
        const filepath = path_1.default.join(screenshotDir, `${result.screenshot_id}.png`);
        try {
            fs_1.default.writeFileSync(filepath, Buffer.from(result.screenshot, 'base64'));
            result.screenshot_path = filepath;
            debugLogger('Screenshot saved: %s', filepath);
        }
        catch (error) {
            debugLogger('Failed to save screenshot %s locally: %s', result.screenshot_id, error instanceof Error ? error.message : String(error));
        }
    }
}
//# sourceMappingURL=saveScreenshotsLocally.js.map