"use strict";

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

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

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

var _prosemirrorUtils = require("prosemirror-utils");

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

var _NodeNames = require("./NodeNames");

var _noop = _interopRequireDefault(require("./noop"));

var _toggleList = _interopRequireDefault(require("./toggleList"));

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

var _customStyle = require("./customStyle");

var _isNodeSelectionForNodeType = _interopRequireDefault(require("./isNodeSelectionForNodeType"));

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 ListToggleCommand extends _licitDocAttrsStep.UICommand {
  constructor(ordered, type) {
    super();

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

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

    _defineProperty(this, "isActive", state => {
      let bOK = false;

      if (this._ordered) {
        bOK = !!this._findList(state, _NodeNames.ORDERED_LIST);
      } else {
        bOK = !!this._findList(state, _NodeNames.BULLET_LIST);
      }

      return bOK;
    });

    _defineProperty(this, "isEnabled", (state, view) => {
      let bOK = false;
      bOK = hasCustomNumberedList(state) || hasImageNode(state);
      return !bOK;
    });

    _defineProperty(this, "execute", (state, dispatch, view) => {
      const {
        selection,
        schema
      } = state;
      const nodeType = schema.nodes[this._ordered ? _NodeNames.ORDERED_LIST : _NodeNames.BULLET_LIST];
      let {
        tr
      } = state;
      tr = tr.setSelection(selection);

      if (!nodeType) {
        return tr;
      }

      tr = (0, _toggleList.default)(tr, schema, nodeType, this._orderedListType);

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

    _defineProperty(this, "executeCustom", (state, tr, from, to) => {
      const {
        schema
      } = state;
      const nodeType = schema.nodes[this._ordered ? _NodeNames.ORDERED_LIST : _NodeNames.BULLET_LIST];

      if (!nodeType) {
        return tr;
      } // tr = tr.setSelection(TextSelection.create(tr.doc, from, to));


      tr = (0, _toggleList.default)(tr, schema, nodeType, this._orderedListType);
      return tr;
    });

    this._ordered = ordered;
    this._orderedListType = type;
  }

  _findList(state, type) {
    const {
      nodes
    } = state.schema;
    const list = nodes[type];
    const findList = list ? (0, _prosemirrorUtils.findParentNodeOfType)(list) : _noop.default;
    return findList(state.selection);
  } // [FS] IRAD-1087 2020-11-11
  // New method to execute new styling implementation for List
  //only x.x.x is handled here need to handle indent


} // [FS] IRAD-1216 2020-02-24
// Disable the List when menu when the cursor is in custom numbered style


exports.ListToggleCommand = ListToggleCommand;

function hasCustomNumberedList(state) {
  let isNumberedList = false;
  const {
    selection,
    schema
  } = state;
  const text = schema.nodes[_NodeNames.PARAGRAPH];
  const result = (0, _prosemirrorUtils.findParentNodeOfType)(text)(selection);

  if (result && result.node.attrs) {
    if (result.node.attrs.styleName && 'None' !== result.node.attrs.styleName) {
      const style = (0, _customStyle.getCustomStyleByName)(result.node.attrs.styleName);
      if (style && style.styles && style.styles.styleLevel && style.styles.hasNumbering) isNumberedList = true;
    }
  }

  return isNumberedList;
} // [FS] IRAD-1317 2021-05-06
// To disable the list menu in toolbar when select an image


function hasImageNode(state) {
  const {
    selection,
    schema
  } = state;
  const imageNodeType = schema.nodes[_NodeNames.IMAGE];
  return imageNodeType && (0, _isNodeSelectionForNodeType.default)(selection, imageNodeType);
}