import toCSSColor from './ui/toCSSColor.js';
import { Node } from 'prosemirror-model';
import { tableNodes } from 'prosemirror-tables';
var NO_VISIBLE_BORDER_WIDTH = new Set(['0pt', '0px']);

// https://github.com/ProseMirror/prosemirror-tables/blob/master/demo.js
var TableNodesSpecs = tableNodes({
  tableGroup: 'block',
  cellContent: 'block+',
  cellAttributes: {
    borderColor: {
      "default": null,
      getFromDOM: function getFromDOM(dom) {
        var _dom$style = dom.style,
          borderColor = _dom$style.borderColor,
          borderWidth = _dom$style.borderWidth;
        if (NO_VISIBLE_BORDER_WIDTH.has(borderWidth)) {
          return 'transparent';
        }
        return borderColor && toCSSColor(borderColor) || null;
      },
      setDOMAttr: function setDOMAttr(value, attrs) {
        if (value) {
          attrs.style = (attrs.style || '') + ";border-color: ".concat(value, ";");
        }
      }
    },
    background: {
      "default": null,
      // TODO: Move these to a table helper.
      getFromDOM: function getFromDOM(dom) {
        return dom.style.backgroundColor || null;
      },
      setDOMAttr: function setDOMAttr(value, attrs) {
        if (value) {
          attrs.style = (attrs.style || '') + ";background-color: ".concat(value, ";");
        }
      }
    }
  }
});

// Override the default table node spec to support custom attributes.
var TableNodeSpec = Object.assign({}, TableNodesSpecs.table, {
  attrs: {
    marginLeft: {
      "default": null
    }
  },
  parseDOM: [{
    tag: 'table',
    getAttrs: function getAttrs(dom) {
      var marginLeft = dom.style.marginLeft;
      if (marginLeft && /\d+px/.test(marginLeft)) {
        return {
          marginLeft: parseFloat(marginLeft)
        };
      }
      return null;
    }
  }],
  toDOM: function toDOM(node) {
    // Normally, the DOM structure of the table node is rendered by
    // `TableNodeView`. This method is only called when user selects a
    // table node and copies it, which triggers the "serialize to HTML" flow
    //  that calles this method.
    var marginLeft = node.attrs.marginLeft;
    var domAttrs = {};
    if (marginLeft) {
      domAttrs.style = "margin-left: ".concat(marginLeft, "px");
    }
    return ['table', domAttrs, 0];
  }
});
Object.assign(TableNodesSpecs, {
  table: TableNodeSpec
});
export default TableNodesSpecs;