"use strict";

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

var _prosemirrorModel = require("prosemirror-model");

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

var MarkNames = _interopRequireWildcard(require("./MarkNames"));

var _TextAlignCommand = require("./TextAlignCommand");

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

var _TextLineSpacingCommand = require("./TextLineSpacingCommand");

var _ParagraphSpacingCommand = require("./ParagraphSpacingCommand");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const {
  MARK_EM,
  MARK_FONT_SIZE,
  MARK_FONT_TYPE,
  MARK_STRIKE,
  MARK_STRONG,
  MARK_TEXT_COLOR,
  MARK_TEXT_HIGHLIGHT,
  MARK_UNDERLINE
} = MarkNames;
const FORMAT_MARK_NAMES = [MARK_EM, MARK_FONT_SIZE, MARK_FONT_TYPE, MARK_STRIKE, MARK_STRONG, MARK_TEXT_COLOR, MARK_TEXT_HIGHLIGHT, MARK_UNDERLINE]; // [FS] IRAD-1053 2020-11-13
// Issue fix: Line spacing and paragrapgh spacing not removed when select Remove style.

function removeTextAlignAndLineSpacing(tr, schema) {
  tr = (0, _TextAlignCommand.setTextAlign)(tr, schema, null); // to remove the applied line spacing

  tr = (0, _TextLineSpacingCommand.setTextLineSpacing)(tr, schema, null); // to remove the paragrapgh spacing after format

  tr = (0, _ParagraphSpacingCommand.setParagraphSpacing)(tr, schema, '0', true); // to remove the paragrapgh spacing before format

  tr = (0, _ParagraphSpacingCommand.setParagraphSpacing)(tr, schema, '0', false);
  return tr;
} // [FS] IRAD-1053 2020-10-08
// to clear the custom styles in the selected paragrapgh


function clearCustomStyleMarks(tr, schema, state) {
  const {
    doc,
    selection
  } = tr;

  if (!selection || !doc) {
    return tr;
  }

  let {
    from,
    to
  } = selection;
  const {
    empty
  } = selection;

  if (empty) {
    // [FS] IRAD-1053 2020-10-09
    // to get the start and end position of a paragrapgh
    from = selection.$from.before(1);
    to = selection.$to.after(1);
  }

  const markTypesToRemove = new Set(FORMAT_MARK_NAMES.map(n => schema.marks[n]).filter(Boolean));

  if (!markTypesToRemove.size) {
    return tr;
  }

  const tasks = [];
  const textAlignNode = [];
  doc.nodesBetween(from, to, (node, pos) => {
    if (node.content && node.content.content && node.content.content.length) {
      if (node.content && node.content.content && node.content.content[0].marks && node.content.content[0].marks.length) {
        node.content.content[0].marks.some(mark => {
          if (markTypesToRemove.has(mark.type)) {
            tasks.push({
              node,
              pos,
              mark
            });
          }
        });
        return true;
      } // [FS] IRAD-1053 2020-10-21
      // Clear Style not removes the text align styles
      else {
          textAlignNode.push({
            node,
            pos
          });
        }
    }

    return true;
  });

  if (!tasks.length) {
    // [FS] IRAD-1053 2020-10-21
    // Clear Style not removes the text align styles
    textAlignNode.forEach(eachnode => {
      const {
        node
      } = eachnode;
      node.attrs.styleName = 'None';
    }); // to remove both text align format and line spacing

    tr = removeTextAlignAndLineSpacing(tr, schema);
    return tr;
  }

  tasks.forEach(job => {
    const {
      node,
      mark,
      pos
    } = job;
    tr = tr.removeMark(pos, pos + node.nodeSize, mark.type); // reset the custom style name to NONE after remove the styles

    node.attrs.styleName = 'None';
  }); // to remove both text align format and line spacing

  tr = removeTextAlignAndLineSpacing(tr, schema); // [FS] IRAD-1053 2020-10-21
  // On reload editor, shows the removed custom style name using Clear Style menu.

  state.doc.nodesBetween(from, to, (node, startPos) => {
    if (node.type.name === 'paragraph') {
      tr = tr.setNodeMarkup(startPos, undefined, node.attrs);
    }
  });
  return tr;
}