"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatasetReader = void 0;
var fs_1 = require("fs");
var path_1 = require("path");
var DatasetReader = /** @class */ (function () {
    function DatasetReader() {
    }
    DatasetReader.readData = function (path, config) {
        if (fs_1.lstatSync(path).isFile()) {
            return this.readFile(path, config);
        }
        else if (fs_1.lstatSync(path).isDirectory()) {
            return this.getDataFromDirectory(path);
        }
        else {
            throw new Error("Path must be a directory or a file. Path: \"" + path + "\".");
        }
    };
    DatasetReader.readFile = function (path, config) {
        var extension = this.getExtension(path);
        switch (extension) {
            case 'csv':
                return this.readCSV(path, config === null || config === void 0 ? void 0 : config.csvSeparator);
            default:
                throw new Error("Unable to read " + extension + " files.");
        }
    };
    DatasetReader.getExtension = function (path) {
        return path.split('.').pop();
    };
    DatasetReader.readCSV = function (path, separator) {
        if (separator === void 0) { separator = ';'; }
        var content = fs_1.readFileSync(path, 'utf-8');
        var lines = content.split('\n');
        var columns = lines[0].split(separator);
        var data = lines.slice(1).map(function (line, lineNum) {
            var values = line.split(separator);
            if (values.length != columns.length) {
                throw Error("Invalid number of columns at line: " + (lineNum + 2) + " (Expected: " + columns.length + ", Recieved: " + values.length + ").");
            }
            else {
                var lineData_1 = {};
                columns.forEach(function (column, i) {
                    lineData_1[column] = values[i];
                });
                return lineData_1;
            }
        });
        return data;
    };
    DatasetReader.getDataFromDirectory = function (path) {
        var _this = this;
        var data = [];
        var files = fs_1.readdirSync(path).filter(function (fileName) { return _this.getExtension(fileName) == 'txt'; });
        if (files.length == 0) {
            throw new Error("Directory must contain a txt file for each intent. Path: \"" + path + "\".");
        }
        files.forEach(function (fileName) {
            var intentName = fileName.split('.').shift();
            var filePath = path_1.join(path, fileName);
            var fileText = fs_1.readFileSync(filePath, 'utf-8');
            var lines = fileText.split('\n');
            lines.forEach(function (line) {
                data.push({ label: intentName, feature: line });
            });
        });
        return data;
    };
    return DatasetReader;
}());
exports.DatasetReader = DatasetReader;
//# sourceMappingURL=data-reader.js.map