"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 _UICommand = _interopRequireDefault(require("./ui/UICommand"));

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 _UICommand.default {
  constructor(ordered, type) {
    super();

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

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

    _defineProperty(this, "isActive", state => {
      if (this._ordered) {
        return !!this._findList(state, _NodeNames.ORDERED_LIST);
      } else {
        return !!this._findList(state, _NodeNames.BULLET_LIST);
      }
    });

    _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) => {
      const {
        schema
      } = state;
      const nodeType = schema.nodes[this._ordered ? _NodeNames.ORDERED_LIST : _NodeNames.BULLET_LIST];

      if (!nodeType) {
        return tr;
      }

      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


}

var _default = ListToggleCommand;
exports.default = _default;