"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const table_constants_1 = require("../utils/table-constants"); const table_helpers_1 = require("../utils/table-helpers"); const input_converter_1 = require("./input-converter"); const internal_table_printer_1 = require("./internal-table-printer"); const DEFAULT_ROW_SORT_FUNC = () => 0; const DEFAULT_ROW_FILTER_FUNC = () => true; class TableInternal { constructor(options) { // default construction this.rows = []; this.columns = []; this.title = undefined; this.tableStyle = table_constants_1.DEFAULT_TABLE_STYLE; this.filterFunction = DEFAULT_ROW_FILTER_FUNC; this.sortFunction = DEFAULT_ROW_SORT_FUNC; this.enabledColumns = []; this.disabledColumns = []; this.computedColumns = []; if (options instanceof Array) { this.initSimple(options); } else if (typeof options === 'object') { this.initDetailed(options); } } initSimple(columns) { this.columns = columns.map((column) => ({ name: column, title: column, alignment: table_constants_1.DEFAULT_ROW_ALIGNMENT, })); } initDetailed(options) { var _a; this.title = options.title || undefined; this.tableStyle = (options === null || options === void 0 ? void 0 : options.style) || table_constants_1.DEFAULT_TABLE_STYLE; this.sortFunction = (options === null || options === void 0 ? void 0 : options.sort) || DEFAULT_ROW_SORT_FUNC; this.filterFunction = (options === null || options === void 0 ? void 0 : options.filter) || DEFAULT_ROW_FILTER_FUNC; this.enabledColumns = (options === null || options === void 0 ? void 0 : options.enabledColumns) || []; this.disabledColumns = (options === null || options === void 0 ? void 0 : options.disabledColumns) || []; this.computedColumns = (options === null || options === void 0 ? void 0 : options.computedColumns) || []; this.columns = ((_a = options.columns) === null || _a === void 0 ? void 0 : _a.map(input_converter_1.rawColumnToInternalColumn)) || []; } createColumnFromRow(text) { const colNames = this.columns.map((col) => col.name); Object.keys(text).forEach((key) => { if (!colNames.includes(key)) { this.columns.push(table_helpers_1.createColumFromOnlyName(key)); } }); } addColumn(textOrObj) { if (typeof textOrObj === 'string') { this.columns.push(table_helpers_1.createColumFromOnlyName(textOrObj)); } else { this.columns.push(table_helpers_1.createColumFromComputedColumn(textOrObj)); } } addColumns(toBeInsertedColumns) { toBeInsertedColumns.forEach((toBeInsertedColumn) => { this.addColumn(toBeInsertedColumn); }); } addRow(text, options) { this.createColumnFromRow(text); this.rows.push(table_helpers_1.createRow((options === null || options === void 0 ? void 0 : options.color) || table_constants_1.DEFAULT_ROW_FONT_COLOR, text)); } addRows(toBeInsertedRows, options) { toBeInsertedRows.forEach((toBeInsertedRow) => { this.addRow(toBeInsertedRow, options); }); } renderTable() { return internal_table_printer_1.renderTable(this); } } exports.default = TableInternal;