function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { EditorState } from 'prosemirror-state';
import { setCellAttr } from 'prosemirror-tables';
import { Transform } from 'prosemirror-transform';
import { EditorView } from 'prosemirror-view';
import { ColorEditor } from '@modusoperandi/color-picker';
import { atAnchorRight, createPopUp, findNodesWithSameMark, MARK_TEXT_COLOR } from '@modusoperandi/licit-ui-commands';
import { UICommand } from '@modusoperandi/licit-doc-attrs-step';
var setCellBorderBlack = setCellAttr('borderColor', '#000000');
var TableBorderColorCommand = /*#__PURE__*/function (_UICommand) {
  function TableBorderColorCommand() {
    var _this;
    _classCallCheck(this, TableBorderColorCommand);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, TableBorderColorCommand, [].concat(args));
    _defineProperty(_this, "_popUp", null);
    _defineProperty(_this, "shouldRespondToUIEvent", function (e) {
      return e.type === UICommand.EventType.MOUSEENTER;
    });
    _defineProperty(_this, "isEnabled", function (state) {
      return setCellBorderBlack(state.tr);
    });
    _defineProperty(_this, "waitForUserInput", function (state, dispatch, view, event) {
      if (_this._popUp) {
        return Promise.resolve(undefined);
      }
      var target = event === null || event === void 0 ? void 0 : event.currentTarget;
      if (!(target instanceof HTMLElement)) {
        return Promise.resolve(undefined);
      }
      var doc = state.doc,
        selection = state.selection,
        schema = state.schema;
      var markType = schema.marks[MARK_TEXT_COLOR];
      var from = selection.from,
        to = selection.to;
      var result = findNodesWithSameMark(doc, from, to, markType);
      var anchor = event ? event.currentTarget : null;
      var hex = result ? result.mark.attrs.color : null;
      return new Promise(function (resolve) {
        _this._popUp = createPopUp(ColorEditor, {
          hex: hex,
          runtime: view.runtime,
          showCheckbox: true
        }, {
          anchor: anchor,
          autoDismiss: false,
          position: atAnchorRight,
          onClose: function onClose(val) {
            if (_this._popUp) {
              _this._popUp = null;
              resolve(val);
            }
          }
        });
      });
    });
    _defineProperty(_this, "executeWithUserInput", function (state, dispatch, view, color) {
      if (dispatch && color !== undefined) {
        var pos = _this.findCellPosFromSelection(view.state);
        if (pos !== null) {
          _this.setCellBorder(view, pos, color.selectedPosition, color.color);
        }
        return true;
      }
      return false;
    });
    return _this;
  }
  _inherits(TableBorderColorCommand, _UICommand);
  return _createClass(TableBorderColorCommand, [{
    key: "cancel",
    value: function cancel() {
      var _this$_popUp;
      (_this$_popUp = this._popUp) === null || _this$_popUp === void 0 || _this$_popUp.close(undefined);
    }
  }, {
    key: "setCellBorder",
    value: function setCellBorder(view, pos, sides, color) {
      var width = '0.25px';
      var style = 'solid';
      var state = view.state,
        dispatch = view.dispatch;
      var node = state.doc.nodeAt(pos);
      if (!node || !['table_cell', 'table_header'].includes(node.type.name)) return;
      var newAttrs = _objectSpread({}, node.attrs);
      var cssValue = "".concat(width, " ").concat(style, " ").concat(color);
      sides.forEach(function (side) {
        var attrName = "border".concat(side.charAt(0).toUpperCase() + side.slice(1));
        newAttrs[attrName] = cssValue;
      });
      dispatch(state.tr.setNodeMarkup(pos, null, newAttrs));
    }
  }, {
    key: "findCellPosFromSelection",
    value: function findCellPosFromSelection(state) {
      var $from = state.selection.$from;
      for (var d = $from.depth; d > 0; d--) {
        var node = $from.node(d);
        if (['cell', 'header_cell'].includes(node.type.spec.tableRole)) {
          return $from.before(d);
        }
      }
      return null;
    }
  }]);
}(UICommand);
export default TableBorderColorCommand;