import { Fragment, Schema } from 'prosemirror-model';
import { TextSelection } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { PARAGRAPH, TABLE, TABLE_CELL, TABLE_ROW } from './NodeNames.js';

// const ZERO_WIDTH_SPACE_CHAR = '\u200b';

export default function insertTable(tr, schema, rows, cols) {
  if (!tr.selection || !tr.doc) {
    return tr;
  }
  var _tr$selection = tr.selection,
    from = _tr$selection.from,
    to = _tr$selection.to;
  if (from !== to) {
    return tr;
  }
  var nodes = schema.nodes;
  var cell = nodes[TABLE_CELL];
  var paragraph = nodes[PARAGRAPH];
  var row = nodes[TABLE_ROW];
  var table = nodes[TABLE];
  if (!(cell && paragraph && row && table)) {
    return tr;
  }
  var rowNodes = [];
  for (var rr = 0; rr < rows; rr++) {
    var cellNodes = [];
    for (var cc = 0; cc < cols; cc++) {
      // [FS] IRAD-950 2020-05-25
      // Fix:Extra arrow key required for cell navigation using arrow right/Left
      var cellNode = cell.create(undefined, Fragment.fromArray([paragraph.create()]));
      cellNodes.push(cellNode);
    }
    var rowNode = row.create({}, Fragment.from(cellNodes));
    rowNodes.push(rowNode);
  }
  var tableNode = table.create({}, Fragment.from(rowNodes));
  tr = tr.insert(from, Fragment.from(tableNode));
  var selection = TextSelection.create(tr.doc, from + 5, from + 5);
  tr = tr.setSelection(selection);
  return tr;
}