"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSystemFunctions = void 0;
const fs = __importStar(require("fs"));
const fse = __importStar(require("fs-extra"));
const jsonFuture = __importStar(require("json-future"));
const path = __importStar(require("path"));
const fsp = fs.promises;
class FileSystemFunctions {
    constructor() {
        this.getFilesAsync = async (dir) => {
            let files = [];
            const getFiles = async (folder) => {
                files = await fsp.readdir(folder);
                const result = files.map((file) => {
                    const p = path.join(folder, file);
                    return fsp.stat(p).then((stat) => (stat.isDirectory() ? getFiles(p) : [p]));
                });
                return Array.prototype.concat(...(await Promise.all(result))); // Flatten
            };
            return getFiles(dir);
        };
        this.checkFolder = async (file) => {
            if (typeof file === 'undefined') {
                return false;
            }
            await fsp.stat(file).catch(() => {
                throw new Error(`The path provided: ${file} is invalid`);
            });
            return true;
        };
        this.readJsonFeatureFile = async (file, encoding) => fse.readJson(file, { encoding }).catch((error) => {
            console.log(`The file ${file} is not a valid json, it has the error message ${error.message}`);
            return null;
        });
        this.readJsonExtendedReportFile = async (file, encoding = 'utf8') => {
            const report = await fse.readJson(file, { encoding }).catch((error) => {
                console.log(`The file ${file} is not a valid json, it has the error message ${error.message}`);
                throw new Error(`The file ${file} is not a valid json, it has the error message ${error.message}`);
            });
            report.results.overview.date = new Date(report.results.overview.date);
            return report;
        };
        this.writeJson = async (destinationFile, jsonObject) => fse.writeFile(destinationFile, jsonObject);
        this.saveJsonFile = async (filePath, fileName, json) => {
            const fileNameAndPath = path.join(filePath, fileName);
            const jsonString = jsonFuture.stringify(json);
            await this.writeJson(fileNameAndPath, jsonString);
            return true;
        };
    }
}
exports.FileSystemFunctions = FileSystemFunctions;
//# sourceMappingURL=file-system-functions.js.map