Liferay.Loader.define('document-library-preview-image$dnd-core@11.1.3/dist/esm/actions/dragDrop/beginDrag', ['module', 'exports', 'require'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    import { invariant } from '@react-dnd/invariant';
    import { setClientOffset } from './local/setClientOffset';
    import { isObject } from '../../utils/js_utils';
    import { BEGIN_DRAG, INIT_COORDS } from './types';
    var ResetCoordinatesAction = {
      type: INIT_COORDS,
      payload: {
        clientOffset: null,
        sourceClientOffset: null
      }
    };
    export function createBeginDrag(manager) {
      return function beginDrag() {
        var sourceIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
        var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
          publishSource: true
        };
        var _options$publishSourc = options.publishSource,
            publishSource = _options$publishSourc === void 0 ? true : _options$publishSourc,
            clientOffset = options.clientOffset,
            getSourceClientOffset = options.getSourceClientOffset;
        var monitor = manager.getMonitor();
        var registry = manager.getRegistry(); // Initialize the coordinates using the client offset

        manager.dispatch(setClientOffset(clientOffset));
        verifyInvariants(sourceIds, monitor, registry); // Get the draggable source

        var sourceId = getDraggableSource(sourceIds, monitor);

        if (sourceId === null) {
          manager.dispatch(ResetCoordinatesAction);
          return;
        } // Get the source client offset


        var sourceClientOffset = null;

        if (clientOffset) {
          if (!getSourceClientOffset) {
            throw new Error('getSourceClientOffset must be defined');
          }

          verifyGetSourceClientOffsetIsFunction(getSourceClientOffset);
          sourceClientOffset = getSourceClientOffset(sourceId);
        } // Initialize the full coordinates


        manager.dispatch(setClientOffset(clientOffset, sourceClientOffset));
        var source = registry.getSource(sourceId);
        var item = source.beginDrag(monitor, sourceId);
        verifyItemIsObject(item);
        registry.pinSource(sourceId);
        var itemType = registry.getSourceType(sourceId);
        return {
          type: BEGIN_DRAG,
          payload: {
            itemType: itemType,
            item: item,
            sourceId: sourceId,
            clientOffset: clientOffset || null,
            sourceClientOffset: sourceClientOffset || null,
            isSourcePublic: !!publishSource
          }
        };
      };
    }

    function verifyInvariants(sourceIds, monitor, registry) {
      invariant(!monitor.isDragging(), 'Cannot call beginDrag while dragging.');
      sourceIds.forEach(function (sourceId) {
        invariant(registry.getSource(sourceId), 'Expected sourceIds to be registered.');
      });
    }

    function verifyGetSourceClientOffsetIsFunction(getSourceClientOffset) {
      invariant(typeof getSourceClientOffset === 'function', 'When clientOffset is provided, getSourceClientOffset must be a function.');
    }

    function verifyItemIsObject(item) {
      invariant(isObject(item), 'Item must be an object.');
    }

    function getDraggableSource(sourceIds, monitor) {
      var sourceId = null;

      for (var i = sourceIds.length - 1; i >= 0; i--) {
        if (monitor.canDragSource(sourceIds[i])) {
          sourceId = sourceIds[i];
          break;
        }
      }

      return sourceId;
    }
  }
});
//# sourceMappingURL=beginDrag.js.map