"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.pluginKey = exports["default"] = exports.PmListPlugin = void 0;

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

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

var _utils = require("../utils");

var _mark = require("../../../../utils/mark");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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; }

var pluginKey = new _prosemirrorState.PluginKey('listsPlugin');
exports.pluginKey = pluginKey;

var PmListPlugin = function PmListPlugin(dispatch) {
  return new _prosemirrorState.Plugin({
    state: {
      init: function init() {
        return {
          bulletListActive: false,
          bulletListDisabled: false,
          orderedListActive: false,
          orderedListDisabled: false
        };
      },
      apply: function apply(tr, pluginState, _, state) {
        var _state$schema$nodes = state.schema.nodes,
            bulletList = _state$schema$nodes.bulletList,
            orderedList = _state$schema$nodes.orderedList;
        var listParent = (0, _prosemirrorUtils.findParentNodeOfType)([bulletList, orderedList])(tr.selection);
        var bulletListActive = !!listParent && listParent.node.type === bulletList;
        var orderedListActive = !!listParent && listParent.node.type === orderedList;
        var bulletListDisabled = !(bulletListActive || orderedListActive || (0, _utils.isWrappingPossible)(bulletList, state));
        var orderedListDisabled = !(bulletListActive || orderedListActive || (0, _utils.isWrappingPossible)(orderedList, state));

        if (bulletListActive !== pluginState.bulletListActive || orderedListActive !== pluginState.orderedListActive || bulletListDisabled !== pluginState.bulletListDisabled || orderedListDisabled !== pluginState.orderedListDisabled) {
          var nextPluginState = _objectSpread(_objectSpread({}, pluginState), {}, {
            bulletListActive: bulletListActive,
            orderedListActive: orderedListActive,
            bulletListDisabled: bulletListDisabled,
            orderedListDisabled: orderedListDisabled
          });

          dispatch(pluginKey, nextPluginState);
          return nextPluginState;
        }

        return pluginState;
      }
    },
    view: function view() {
      return {
        update: function update(_ref) {
          var state = _ref.state,
              dispatch = _ref.dispatch;
          var _state$schema$nodes2 = state.schema.nodes,
              bulletList = _state$schema$nodes2.bulletList,
              orderedList = _state$schema$nodes2.orderedList;
          var _state$schema$marks = state.schema.marks,
              alignment = _state$schema$marks.alignment,
              indentation = _state$schema$marks.indentation;
          var listParent = (0, _prosemirrorUtils.findParentNodeOfType)([bulletList, orderedList])(state.tr.selection);

          if (!listParent) {
            return;
          }
          /** Block mark if exists should be removed when toggled to list items */


          var removeMarks = (0, _mark.removeBlockMarks)(state, [alignment, indentation]);

          if (removeMarks) {
            dispatch(removeMarks);
          }
        }
      };
    },
    key: pluginKey
  });
};

exports.PmListPlugin = PmListPlugin;
var _default = PmListPlugin;
exports["default"] = _default;