"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataReader = void 0;
var d3_1 = require("d3");
var fs_1 = require("fs");
var path_1 = require("path");
var DataReader = /** @class */ (function () {
    function DataReader() {
    }
    DataReader.prototype.readData = function (path) {
        if (fs_1.lstatSync(path).isFile()) {
            return this._readFile(path);
        }
        else if (fs_1.lstatSync(path).isDirectory()) {
            return this._getDataFromDirectory(path);
        }
        else {
            throw Error('Path must be a directory or a file.');
        }
    };
    DataReader.prototype._readFile = function (path) {
        var extension = this._getExtension(path);
        switch (extension) {
            case 'csv':
                return this._readCSV(path);
            default:
                throw Error('File must be a csv.');
        }
    };
    DataReader.prototype._getExtension = function (path) {
        return path.split('.').pop();
    };
    DataReader.prototype._readCSV = function (path) {
        var text = fs_1.readFileSync(path, 'utf-8');
        var info = d3_1.csvParse(text);
        return info.map(function (sample) {
            return { label: sample.label, feature: sample.feature };
        });
    };
    DataReader.prototype._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 Error('Directory must contain a txt file for each intent.');
        }
        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 DataReader;
}());
exports.DataReader = DataReader;
//# sourceMappingURL=data-reader.js.map