"use strict";

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

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

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

var _findActiveMark = _interopRequireDefault(require("../findActiveMark"));

var _ParagraphNodeSpec = require("../ParagraphNodeSpec");

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

// [FS] IRAD-1042 2020-09-17
// To find the selected custom style
function findActiveCustomStyle(state) {
  const {
    schema,
    doc,
    selection,
    tr
  } = state;
  const markType = schema.marks[_MarkNames.MARK_CUSTOMSTYLES];

  if (!markType) {
    return _ParagraphNodeSpec.RESERVED_STYLE_NONE;
  }

  const {
    from,
    to,
    empty
  } = selection;

  if (empty) {
    const storedMarks = tr.storedMarks || state.storedMarks || selection.$cursor && selection.$cursor.marks && selection.$cursor.marks() || [];
    const sm = storedMarks.find(m => m.type === markType);
    return sm && sm.attrs.styleName || _ParagraphNodeSpec.RESERVED_STYLE_NONE;
  }

  const mark = (0, _findActiveMark.default)(doc, from, to, markType);
  const name = mark && mark.attrs.styleName;

  if (!name) {
    return _ParagraphNodeSpec.RESERVED_STYLE_NONE;
  }

  return name;
}