"use strict";

Liferay.Loader.define("frontend-js-components-web$dnd-core@11.1.3/dist/cjs/DragDropMonitorImpl", ['module', 'exports', 'require', '@frontend-js-components-web$react-dnd/invariant', './utils/matchesType', './utils/coords', './utils/dirtiness'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.DragDropMonitorImpl = void 0;

    var _invariant = require("@frontend-js-components-web$react-dnd/invariant");

    var _matchesType = require("./utils/matchesType");

    var _coords = require("./utils/coords");

    var _dirtiness = require("./utils/dirtiness");

    function _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }

    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    var DragDropMonitorImpl = /*#__PURE__*/function () {
      function DragDropMonitorImpl(store, registry) {
        _classCallCheck(this, DragDropMonitorImpl);

        this.store = store;
        this.registry = registry;
      }

      _createClass(DragDropMonitorImpl, [{
        key: "subscribeToStateChange",
        value: function subscribeToStateChange(listener) {
          var _this = this;

          var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
            handlerIds: undefined
          };
          var handlerIds = options.handlerIds;
          (0, _invariant.invariant)(typeof listener === 'function', 'listener must be a function.');
          (0, _invariant.invariant)(typeof handlerIds === 'undefined' || Array.isArray(handlerIds), 'handlerIds, when specified, must be an array of strings.');
          var prevStateId = this.store.getState().stateId;

          var handleChange = function handleChange() {
            var state = _this.store.getState();

            var currentStateId = state.stateId;

            try {
              var canSkipListener = currentStateId === prevStateId || currentStateId === prevStateId + 1 && !(0, _dirtiness.areDirty)(state.dirtyHandlerIds, handlerIds);

              if (!canSkipListener) {
                listener();
              }
            } finally {
              prevStateId = currentStateId;
            }
          };

          return this.store.subscribe(handleChange);
        }
      }, {
        key: "subscribeToOffsetChange",
        value: function subscribeToOffsetChange(listener) {
          var _this2 = this;

          (0, _invariant.invariant)(typeof listener === 'function', 'listener must be a function.');
          var previousState = this.store.getState().dragOffset;

          var handleChange = function handleChange() {
            var nextState = _this2.store.getState().dragOffset;

            if (nextState === previousState) {
              return;
            }

            previousState = nextState;
            listener();
          };

          return this.store.subscribe(handleChange);
        }
      }, {
        key: "canDragSource",
        value: function canDragSource(sourceId) {
          if (!sourceId) {
            return false;
          }

          var source = this.registry.getSource(sourceId);
          (0, _invariant.invariant)(source, 'Expected to find a valid source.');

          if (this.isDragging()) {
            return false;
          }

          return source.canDrag(this, sourceId);
        }
      }, {
        key: "canDropOnTarget",
        value: function canDropOnTarget(targetId) {
          // undefined on initial render
          if (!targetId) {
            return false;
          }

          var target = this.registry.getTarget(targetId);
          (0, _invariant.invariant)(target, 'Expected to find a valid target.');

          if (!this.isDragging() || this.didDrop()) {
            return false;
          }

          var targetType = this.registry.getTargetType(targetId);
          var draggedItemType = this.getItemType();
          return (0, _matchesType.matchesType)(targetType, draggedItemType) && target.canDrop(this, targetId);
        }
      }, {
        key: "isDragging",
        value: function isDragging() {
          return Boolean(this.getItemType());
        }
      }, {
        key: "isDraggingSource",
        value: function isDraggingSource(sourceId) {
          // undefined on initial render
          if (!sourceId) {
            return false;
          }

          var source = this.registry.getSource(sourceId, true);
          (0, _invariant.invariant)(source, 'Expected to find a valid source.');

          if (!this.isDragging() || !this.isSourcePublic()) {
            return false;
          }

          var sourceType = this.registry.getSourceType(sourceId);
          var draggedItemType = this.getItemType();

          if (sourceType !== draggedItemType) {
            return false;
          }

          return source.isDragging(this, sourceId);
        }
      }, {
        key: "isOverTarget",
        value: function isOverTarget(targetId) {
          var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
            shallow: false
          };

          // undefined on initial render
          if (!targetId) {
            return false;
          }

          var shallow = options.shallow;

          if (!this.isDragging()) {
            return false;
          }

          var targetType = this.registry.getTargetType(targetId);
          var draggedItemType = this.getItemType();

          if (draggedItemType && !(0, _matchesType.matchesType)(targetType, draggedItemType)) {
            return false;
          }

          var targetIds = this.getTargetIds();

          if (!targetIds.length) {
            return false;
          }

          var index = targetIds.indexOf(targetId);

          if (shallow) {
            return index === targetIds.length - 1;
          } else {
            return index > -1;
          }
        }
      }, {
        key: "getItemType",
        value: function getItemType() {
          return this.store.getState().dragOperation.itemType;
        }
      }, {
        key: "getItem",
        value: function getItem() {
          return this.store.getState().dragOperation.item;
        }
      }, {
        key: "getSourceId",
        value: function getSourceId() {
          return this.store.getState().dragOperation.sourceId;
        }
      }, {
        key: "getTargetIds",
        value: function getTargetIds() {
          return this.store.getState().dragOperation.targetIds;
        }
      }, {
        key: "getDropResult",
        value: function getDropResult() {
          return this.store.getState().dragOperation.dropResult;
        }
      }, {
        key: "didDrop",
        value: function didDrop() {
          return this.store.getState().dragOperation.didDrop;
        }
      }, {
        key: "isSourcePublic",
        value: function isSourcePublic() {
          return Boolean(this.store.getState().dragOperation.isSourcePublic);
        }
      }, {
        key: "getInitialClientOffset",
        value: function getInitialClientOffset() {
          return this.store.getState().dragOffset.initialClientOffset;
        }
      }, {
        key: "getInitialSourceClientOffset",
        value: function getInitialSourceClientOffset() {
          return this.store.getState().dragOffset.initialSourceClientOffset;
        }
      }, {
        key: "getClientOffset",
        value: function getClientOffset() {
          return this.store.getState().dragOffset.clientOffset;
        }
      }, {
        key: "getSourceClientOffset",
        value: function getSourceClientOffset() {
          return (0, _coords.getSourceClientOffset)(this.store.getState().dragOffset);
        }
      }, {
        key: "getDifferenceFromInitialOffset",
        value: function getDifferenceFromInitialOffset() {
          return (0, _coords.getDifferenceFromInitialOffset)(this.store.getState().dragOffset);
        }
      }]);

      return DragDropMonitorImpl;
    }();

    exports.DragDropMonitorImpl = DragDropMonitorImpl;
  }
});
//# sourceMappingURL=DragDropMonitorImpl.js.map