"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.CollectJsons = void 0;
const CommonFunctions = __importStar(require("../common-functions/common-functions"));
class CollectJsons {
    constructor(userProperties) {
        this.getJsonReportFiles = async () => {
            let files = await CommonFunctions.fileSystemFunctions.getFilesAsync(this.userProperties.jsonDir);
            files = files.sort();
            const validReportsList = files.filter(file => file.endsWith('.json'));
            const validReports = await Promise.all(validReportsList.map(async (element) => {
                const result = await CommonFunctions.fileSystemFunctions.readJsonFeatureFile(element, 'utf8');
                const resultFiltered = Array.isArray(result) && result
                    ? result.filter(x => typeof x.id !== 'undefined' && x.id !== null && typeof x.keyword !== 'undefined' && x.keyword !== null)
                    : null;
                return Array.isArray(resultFiltered) && resultFiltered.length > 0 ? resultFiltered : null;
            }));
            const filteredReports = validReports.filter((x) => x !== null);
            return filteredReports;
        };
        this.userProperties = userProperties;
    }
    async createJoinedJson() {
        const jsonOutput = Array();
        await CommonFunctions.fileSystemFunctions.checkFolder(this.userProperties.jsonDir);
        const fileList = await this.getJsonReportFiles();
        if (fileList.length === 0) {
            throw new Error(`No JSON files found in '${this.userProperties.jsonDir}'. NO REPORT CAN BE CREATED!`);
        }
        fileList.forEach(json => {
            json.forEach(feature => {
                if (feature.metadata === null || typeof feature.metadata === 'undefined') {
                    feature.metadata = [];
                }
                if (!this.isMetadata(feature.metadata)) {
                    feature.metadata = [];
                }
                if (this.isMetadata(this.userProperties.metadata)) {
                    feature.metadata.concat(this.userProperties.metadata);
                }
                feature.id = feature.id.toString().replace('/', '_');
                jsonOutput.push(feature);
            });
        });
        if (this.userProperties.saveCollectedJSON) {
            await CommonFunctions.fileSystemFunctions.saveJsonFile(this.userProperties.reportPath, '/merged-output.json', jsonOutput);
        }
        return jsonOutput;
    }
    isMetadata(metadata) {
        return !Array.isArray(metadata) ? false : metadata.filter(metadataElement => (metadataElement).name === undefined || (metadataElement).value === undefined).length === 0;
    }
}
exports.CollectJsons = CollectJsons;
//# sourceMappingURL=collect-jsons.js.map