"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = create;
exports.get = get;
exports.set = set;
const fs = require("fs");
const path = require("path");
/**
 * This implementation of ScreenshotManager Interface will not take any screenshot
 */
class NoOpScreenShotManager {
    beforeTest() { }
    afterTest() { }
    takeAndSaveScreenShot() {
        return Promise.resolve();
    }
}
/**
 * This implementation of ScreenshotManager Interface will take screenshot
 */
class TakeScreenshotManager {
    /**
     * This function will replace all invalid characters with underscore
     * @param name string test name may having some invalid characters
     * @private
     */
    replaceCharacters(name) {
        var output = name.replace(/[^a-z0-9]/gi, '_');
        return output;
    }
    /**
     * This constructor will set screenshot root directory on the file system, like <report-root>/screenshots
     * @param screenshotRoot screenshot root directory
     */
    constructor(screenshotRoot) {
        this._screenshotLocation = '';
        var mochawesomeReportDir = screenshotRoot;
        try {
            if (!fs.existsSync(mochawesomeReportDir))
                fs.mkdirSync(mochawesomeReportDir);
        }
        catch (err) {
            console.error(err);
        }
        this._screenshotsDir = mochawesomeReportDir + path.sep + 'screenshots';
        try {
            if (!fs.existsSync(this._screenshotsDir))
                fs.mkdirSync(this._screenshotsDir);
        }
        catch (err) {
            console.error(err);
        }
    }
    /**
     * This function will set screenshot location based on test suite name and test name
     * @param suiteName test case's suite name
     * @param testName test case's name
     */
    beforeTest(suiteName, testName) {
        var suiteN = this.replaceCharacters(suiteName);
        var testN = this.replaceCharacters(testName);
        var suiteF = this._screenshotsDir + path.sep + suiteN;
        var testF = suiteF + path.sep + testN;
        this._screenshotLocation = testF;
        try {
            if (!fs.existsSync(suiteF))
                fs.mkdirSync(suiteF);
        }
        catch (err) {
            console.error(err);
        }
        try {
            if (!fs.existsSync(testF))
                fs.mkdirSync(testF);
        }
        catch (err) {
            console.error(err);
        }
    }
    /**
     * This function will do cleanup after test execution. It will puerge screenshot for passed test and will generate final state
     * screenshot for failed test
     * @param passed boolean to check test state
     */
    afterTest(passed) {
        if (passed) {
            try {
                var FOLDER_PATH = this._screenshotLocation;
                if (fs.existsSync(FOLDER_PATH)) {
                    var files = fs.readdirSync(FOLDER_PATH);
                    files.forEach((element) => {
                        fs.unlinkSync(FOLDER_PATH + path.sep + element);
                    });
                    fs.rmdirSync(FOLDER_PATH);
                }
            }
            catch (err) {
                console.error(err);
            }
        }
    }
    /**
     * This function will take screenshot and save it on the file system
     * @param screenshotTaker a screenshotTaker instance
     * @param name screeshot image name
     */
    async takeAndSaveScreenShot(screenshotTaker, name) {
        var counter = 1;
        var FOLDER_PATH = this._screenshotLocation;
        if (fs.existsSync(FOLDER_PATH)) {
            var files = fs.readdirSync(FOLDER_PATH);
            files.forEach((element) => {
                if (element.includes(name))
                    counter++;
            });
        }
        try {
            const screenshotImg = await screenshotTaker.takeScreenshot();
            fs.writeFileSync(this._screenshotLocation + path.sep + name + counter + '.jpg', screenshotImg, 'base64');
        }
        catch (err) {
            console.error(err);
        }
        return Promise.resolve();
    }
}
//Below are the top-level methods of the ScreenshotManager module
var currentInstance = new NoOpScreenShotManager();
/**
 * This function will create an instance ScreenshotManager
 * @param screenshotRoot screenshot root location
 */
function create(screenshotRoot) {
    if (screenshotRoot) {
        return new TakeScreenshotManager(screenshotRoot);
    }
    else {
        return new NoOpScreenShotManager();
    }
}
/**
 * This function will get an active ScreenshotManager instance
 * @return an active ScreenshotManager instance
 */
function get() {
    return currentInstance;
}
/**
 * This function will set an active ScreenshotManager instance
 * @param screenshotManager an active ScreenshotManager instance
 */
function set(screenshotManager) {
    currentInstance = screenshotManager;
}
//# sourceMappingURL=ScreenshotManager.js.map