11cb0ef41Sopenharmony_ciconst debug = require('./debug');
21cb0ef41Sopenharmony_ciconst utils = require('./utils');
31cb0ef41Sopenharmony_ciconst tableLayout = require('./layout-manager');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciclass Table extends Array {
61cb0ef41Sopenharmony_ci  constructor(opts) {
71cb0ef41Sopenharmony_ci    super();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci    const options = utils.mergeOptions(opts);
101cb0ef41Sopenharmony_ci    Object.defineProperty(this, 'options', {
111cb0ef41Sopenharmony_ci      value: options,
121cb0ef41Sopenharmony_ci      enumerable: options.debug,
131cb0ef41Sopenharmony_ci    });
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci    if (options.debug) {
161cb0ef41Sopenharmony_ci      switch (typeof options.debug) {
171cb0ef41Sopenharmony_ci        case 'boolean':
181cb0ef41Sopenharmony_ci          debug.setDebugLevel(debug.WARN);
191cb0ef41Sopenharmony_ci          break;
201cb0ef41Sopenharmony_ci        case 'number':
211cb0ef41Sopenharmony_ci          debug.setDebugLevel(options.debug);
221cb0ef41Sopenharmony_ci          break;
231cb0ef41Sopenharmony_ci        case 'string':
241cb0ef41Sopenharmony_ci          debug.setDebugLevel(parseInt(options.debug, 10));
251cb0ef41Sopenharmony_ci          break;
261cb0ef41Sopenharmony_ci        default:
271cb0ef41Sopenharmony_ci          debug.setDebugLevel(debug.WARN);
281cb0ef41Sopenharmony_ci          debug.warn(`Debug option is expected to be boolean, number, or string. Received a ${typeof options.debug}`);
291cb0ef41Sopenharmony_ci      }
301cb0ef41Sopenharmony_ci      Object.defineProperty(this, 'messages', {
311cb0ef41Sopenharmony_ci        get() {
321cb0ef41Sopenharmony_ci          return debug.debugMessages();
331cb0ef41Sopenharmony_ci        },
341cb0ef41Sopenharmony_ci      });
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  toString() {
391cb0ef41Sopenharmony_ci    let array = this;
401cb0ef41Sopenharmony_ci    let headersPresent = this.options.head && this.options.head.length;
411cb0ef41Sopenharmony_ci    if (headersPresent) {
421cb0ef41Sopenharmony_ci      array = [this.options.head];
431cb0ef41Sopenharmony_ci      if (this.length) {
441cb0ef41Sopenharmony_ci        array.push.apply(array, this);
451cb0ef41Sopenharmony_ci      }
461cb0ef41Sopenharmony_ci    } else {
471cb0ef41Sopenharmony_ci      this.options.style.head = [];
481cb0ef41Sopenharmony_ci    }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    let cells = tableLayout.makeTableLayout(array);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci    cells.forEach(function (row) {
531cb0ef41Sopenharmony_ci      row.forEach(function (cell) {
541cb0ef41Sopenharmony_ci        cell.mergeTableOptions(this.options, cells);
551cb0ef41Sopenharmony_ci      }, this);
561cb0ef41Sopenharmony_ci    }, this);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    tableLayout.computeWidths(this.options.colWidths, cells);
591cb0ef41Sopenharmony_ci    tableLayout.computeHeights(this.options.rowHeights, cells);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    cells.forEach(function (row) {
621cb0ef41Sopenharmony_ci      row.forEach(function (cell) {
631cb0ef41Sopenharmony_ci        cell.init(this.options);
641cb0ef41Sopenharmony_ci      }, this);
651cb0ef41Sopenharmony_ci    }, this);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci    let result = [];
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    for (let rowIndex = 0; rowIndex < cells.length; rowIndex++) {
701cb0ef41Sopenharmony_ci      let row = cells[rowIndex];
711cb0ef41Sopenharmony_ci      let heightOfRow = this.options.rowHeights[rowIndex];
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci      if (rowIndex === 0 || !this.options.style.compact || (rowIndex == 1 && headersPresent)) {
741cb0ef41Sopenharmony_ci        doDraw(row, 'top', result);
751cb0ef41Sopenharmony_ci      }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci      for (let lineNum = 0; lineNum < heightOfRow; lineNum++) {
781cb0ef41Sopenharmony_ci        doDraw(row, lineNum, result);
791cb0ef41Sopenharmony_ci      }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci      if (rowIndex + 1 == cells.length) {
821cb0ef41Sopenharmony_ci        doDraw(row, 'bottom', result);
831cb0ef41Sopenharmony_ci      }
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    return result.join('\n');
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  get width() {
901cb0ef41Sopenharmony_ci    let str = this.toString().split('\n');
911cb0ef41Sopenharmony_ci    return str[0].length;
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciTable.reset = () => debug.reset();
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cifunction doDraw(row, lineNum, result) {
981cb0ef41Sopenharmony_ci  let line = [];
991cb0ef41Sopenharmony_ci  row.forEach(function (cell) {
1001cb0ef41Sopenharmony_ci    line.push(cell.draw(lineNum));
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci  let str = line.join('');
1031cb0ef41Sopenharmony_ci  if (str.length) result.push(str);
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_cimodule.exports = Table;
107