"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ListToggleCommand = void 0;
exports.hasImageNode = hasImageNode;
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 _licitUiCommands = require("@modusoperandi/licit-ui-commands");
var _licitDocAttrsStep = require("@modusoperandi/licit-doc-attrs-step");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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, _licitUiCommands.toggleList)(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) : _licitUiCommands.noop;
    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, _licitUiCommands.isNodeSelectionForNodeType)(selection, imageNodeType);
}