"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsvHelper = void 0;
const handlebars_1 = __importDefault(require("handlebars"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const csv = require("convert-csv-to-json");
/**
 * Defines and registers custom handlebar helper - csv
 */
class CsvHelper {
    constructor() {
        /**
         * Registers csv helper
         * - Define request and logger in the scope of the code helper context, allowing user to use request, logger in their mock files
         * - Fetch the file path, key, value and random variables from the the helper
         * - Throw error if file path not defined log and return appropriate error
         * - if random is true, evaluate response for one random row from csv file
         * - else, evaluate response for all rows from csv file matching a search pattern using specified key and value
         * @returns {void}
         */
        this.register = () => {
            handlebars_1.default.registerHelper("csv", (context) => {
                /* eslint-disable no-unused-vars */
                const request = context.data.root.request;
                /* eslint-disable no-unused-vars */
                const logger = context.data.root.logger;
                const src = context.hash.src;
                const key = context.hash.key;
                const value = context.hash.value;
                const random = context.hash.random;
                if (typeof src === "undefined") {
                    logger.error("'src' is a required parameter and has not been set.");
                    return "'src' is a required parameter and has not been set.";
                }
                else {
                    const file = path_1.default.resolve(src);
                    if (fs_1.default.existsSync(file)) {
                        const jsonArr = csv.getJsonFromCsv(file);
                        /* eslint-disable no-unused-vars */
                        let result = {};
                        /* eslint-disable no-unused-vars */
                        if (random) {
                            result = jsonArr[Math.floor(Math.random() * jsonArr.length)];
                        }
                        else {
                            if (typeof key === "undefined" || typeof value === "undefined") {
                                logger.error("If random is false, 'key' & 'value' are required parameters");
                                return "If random is false, 'key' & 'value' are required parameters";
                            }
                            result = jsonArr.filter((jsonObj) => {
                                return jsonObj[key] === value;
                            });
                        }
                        const output = eval(context.fn(this)).trim();
                        return output;
                    }
                    else {
                        logger.error("CSV file not found");
                        return "CSV file not found";
                    }
                }
            });
        };
    }
}
exports.CsvHelper = CsvHelper;
//# sourceMappingURL=CsvHelper.js.map