"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
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 _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 = 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;
      }
    });

    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-1317 2021-05-06
// To disable the list menu in toolbar when select an image


exports.ListToggleCommand = ListToggleCommand;

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