"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _prosemirrorState = require("prosemirror-state");

var _prosemirrorTransform = require("prosemirror-transform");

var _prosemirrorView = require("prosemirror-view");

var _clearMarks = require("./clearMarks");

var _licitDocAttrsStep = require("@modusoperandi/licit-doc-attrs-step");

var _CustomStyleCommand = require("./CustomStyleCommand");

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class MarksClearCommand extends _licitDocAttrsStep.UICommand {
  constructor() {
    super(...arguments);

    _defineProperty(this, "isActive", state => {
      return false;
    });

    _defineProperty(this, "isEnabled", state => {
      const {
        selection
      } = state;
      return !selection.empty && (selection instanceof _prosemirrorState.TextSelection || selection instanceof _prosemirrorState.AllSelection);
    });

    _defineProperty(this, "execute", (state, dispatch, view) => {
      let tr = (0, _clearMarks.clearMarks)(state.tr.setSelection(state.selection), state.schema); // [FS] IRAD-948 2021-02-22
      // Issue fix: after apply clear format on custom style applied paragraph, the custom style is not continuing

      const startPos = state.selection.$from.before(1);
      const endPos = state.selection.$to.after(1);
      const node = (0, _CustomStyleCommand.getNode)(state, startPos, endPos, state.tr);
      let styleName = '';

      if (node && node.attrs) {
        styleName = node.attrs.styleName;
      }

      const storedmarks = (0, _CustomStyleCommand.getMarkByStyleName)(styleName, state.schema); // [FS] IRAD-948 2021-02-22
      // Clear Header formatting

      tr = (0, _clearMarks.clearHeading)(tr, state.schema); // [FS] IRAD-948 2020-05-22
      // Issue fix: after apply clear format on custom style applied paragraph, the custom style is not continuing

      tr.storedMarks = storedmarks;

      if (dispatch && tr.docChanged) {
        dispatch(tr);
        return true;
      }

      return false;
    });
  }

}

var _default = MarksClearCommand;
exports.default = _default;