"use strict";

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

var _prosemirrorCommands = require("prosemirror-commands");

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

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

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

var _findNodesWithSameMark = _interopRequireDefault(require("./findNodesWithSameMark"));

var _UICommand = _interopRequireDefault(require("./ui/UICommand"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 MarkToggleCommand extends _UICommand.default {
  constructor(markName) {
    super();

    _defineProperty(this, "_markName", void 0);

    _defineProperty(this, "isActive", state => {
      const {
        schema,
        doc,
        selection
      } = state;
      const {
        from,
        to
      } = selection;
      const markType = schema.marks[this._markName];

      if (markType && from < to) {
        return !!(0, _findNodesWithSameMark.default)(doc, from, to - 1, markType);
      }

      return false;
    });

    _defineProperty(this, "execute", (state, dispatch, view) => {
      const {
        schema,
        selection,
        tr
      } = state;
      const markType = schema.marks[this._markName];

      if (!markType) {
        return false;
      }

      if (selection.empty && !(selection instanceof _prosemirrorState.TextSelection)) {
        return false;
      }

      const {
        from,
        to
      } = selection;

      if (tr && to === from + 1) {
        const node = tr.doc.nodeAt(from);

        if (node.isAtom && !node.isText && node.isLeaf) {
          // An atomic node (e.g. Image) is selected.
          return false;
        }
      } // TODO: Replace `toggleMark` with transform that does not change scroll
      // position.


      return (0, _prosemirrorCommands.toggleMark)(markType)(state, dispatch, view);
    });

    _defineProperty(this, "executeCustom", (state, tr) => {
      const {
        schema,
        selection
      } = state;
      const markType = schema.marks[this._markName];

      if (!markType) {
        return false;
      }

      if (selection.empty && !(selection instanceof _prosemirrorState.TextSelection)) {
        return false;
      }

      const {
        from,
        to
      } = selection;

      if (tr && to === from + 1) {
        const node = tr.doc.nodeAt(from);

        if (node.isAtom && !node.isText && node.isLeaf) {
          // An atomic node (e.g. Image) is selected.
          return false;
        }
      }

      return toggleCustomStyle(markType, null, state, tr);
    });

    this._markName = markName;
  }

} // [FS] IRAD-1042 2020-09-30
// Fix: overrided the toggleMarks for custom style implementation
// Return Transform object


function toggleCustomStyle(markType, attrs, state, tr) {
  const ref = state.selection;
  const empty = ref.empty;
  const $cursor = ref.$cursor;
  const ranges = ref.ranges;
  const startPos = ref.$from.before(1);
  const endPos = ref.$to.after(1);

  if (empty && !$cursor || !markApplies(state.doc, ranges, markType)) {
    return tr;
  } // [FS] IRAD-1043 2020-10-27
  // No need to remove the applied custom style, if user select the same style multiple times.


  tr.addMark(startPos, endPos, markType.create(attrs));
  return tr;
} //overrided method from prosemirror Transform


function markApplies(doc, ranges, type) {
  let returned = false;

  const loop = function (i) {
    const ref = ranges[i];
    const $from = ref.$from;
    const $to = ref.$to;
    let can = $from.depth == 0 ? doc.type.allowsMarkType(type) : false;
    let bOk = false;
    doc.nodesBetween($from.pos, $to.pos, function (node) {
      if (can) {
        return false;
      }

      can = node.inlineContent && node.type.allowsMarkType(type);
      return true;
    });

    if (can) {
      bOk = true;
    }

    return bOk;
  };

  for (let i = 0; i < ranges.length; i++) {
    returned = loop(i);

    if (returned) {
      return returned;
    }
  }

  return returned;
}

var _default = MarkToggleCommand;
exports.default = _default;