text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.useMove = useMove;
  
  var _textSelection = require("./textSelection");
  
  var _react = require("react");
  
  var _utils = require("@react-aria/utils");
  
  /*
   * Copyright 2020 Adobe. All rights reserved.
   * This file is licensed to you under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License. You may obtain a copy
   * of the License at http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under
   * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
   * OF ANY KIND, either express or implied. See the License for the specific language
   * governing permissions and limitations under the License.
   */
  
  /**
   * Handles move interactions across mouse, touch, and keyboard, including dragging with
   * the mouse or touch, and using the arrow keys. Normalizes behavior across browsers and
   * platforms, and ignores emulated mouse events on touch devices.
   */
  function useMove(props) {
    let {
      onMoveStart,
      onMove,
      onMoveEnd
    } = props;
    let state = (0, _react.useRef)({
      didMove: false,
      lastPosition: null,
      id: null
    });
    let {
      addGlobalListener,
      removeGlobalListener
    } = (0, _utils.useGlobalListeners)();
    let moveProps = (0, _react.useMemo)(() => {
      let moveProps = {};
  
      let start = () => {
        (0, _textSelection.disableTextSelection)();
        state.current.didMove = false;
      };
  
      let move = (pointerType, deltaX, deltaY) => {
        if (deltaX === 0 && deltaY === 0) {
          return;
        }
  
        if (!state.current.didMove) {
          state.current.didMove = true;
          onMoveStart === null || onMoveStart === void 0 ? void 0 : onMoveStart({
            type: 'movestart',
            pointerType
          });
        }
  
        onMove({
          type: 'move',
          pointerType,
          deltaX: deltaX,
          deltaY: deltaY
        });
      };
  
      let end = pointerType => {
        (0, _textSelection.restoreTextSelection)();
  
        if (state.current.didMove) {
          onMoveEnd === null || onMoveEnd === void 0 ? void 0 : onMoveEnd({
            type: 'moveend',
            pointerType
          });
        }
      };
  
      if (typeof PointerEvent !== 'undefined') {
        let onMouseMove = e => {
          if (e.button === 0) {
            move('mouse', e.pageX - state.current.lastPosition.pageX, e.pageY - state.current.lastPosition.pageY);
            state.current.lastPosition = {
              pageX: e.pageX,
              pageY: e.pageY
            };
          }
        };
  
        let onMouseUp = e => {
          if (e.button === 0) {
            end('mouse');
            removeGlobalListener(window, 'mousemove', onMouseMove, false);
            removeGlobalListener(window, 'mouseup', onMouseUp, false);
          }
        };
  
        moveProps.onMouseDown = e => {
          if (e.button === 0) {
            start();
            e.stopPropagation();
            e.preventDefault();
            state.current.lastPosition = {
              pageX: e.pageX,
              pageY: e.pageY
            };
            addGlobalListener(window, 'mousemove', onMouseMove, false);
            addGlobalListener(window, 'mouseup', onMouseUp, false);
          }
        };
  
        let onTouchMove = e => {
          // @ts-ignore
          let touch = [...e.changedTouches].findIndex(({
            identifier
          }) => identifier === state.current.id);
  
          if (touch >= 0) {
            let {
              pageX,
              pageY
            } = e.changedTouches[touch];
            move('touch', pageX - state.current.lastPosition.pageX, pageY - state.current.lastPosition.pageY);
            state.current.lastPosition = {
              pageX,
              pageY
            };
          }
        };
  
        let onTouchEnd = e => {
          // @ts-ignore
          let touch = [...e.changedTouches].findIndex(({
            identifier
          }) => identifier === state.current.id);
  
          if (touch >= 0) {
            end('touch');
            state.current.id = null;
            removeGlobalListener(window, 'touchmove', onTouchMove);
            removeGlobalListener(window, 'touchend', onTouchEnd);
            removeGlobalListener(window, 'touchcancel', onTouchEnd);
          }
        };
  
        moveProps.onTouchStart = e => {
          if (e.changedTouches.length === 0 || state.current.id != null) {
            return;
          }
  
          let {
            pageX,
            pageY,
            identifier
          } = e.changedTouches[0];
          start();
          e.stopPropagation();
          e.preventDefault();
          state.current.lastPosition = {
            pageX,
            pageY
          };
          state.current.id = identifier;
          addGlobalListener(window, 'touchmove', onTouchMove, false);
          addGlobalListener(window, 'touchend', onTouchEnd, false);
          addGlobalListener(window, 'touchcancel', onTouchEnd, false);
        };
      }
  
      let triggerKeyboardMove = (deltaX, deltaY) => {
        start();
        move('keyboard', deltaX, deltaY);
        end('keyboard');
      };
  
      moveProps.onKeyDown = e => {
        switch (e.key) {
          case 'Left':
          case 'ArrowLeft':
            e.preventDefault();
            e.stopPropagation();
            triggerKeyboardMove(-1, 0);
            break;
  
          case 'Right':
          case 'ArrowRight':
            e.preventDefault();
            e.stopPropagation();
            triggerKeyboardMove(1, 0);
            break;
  
          case 'Up':
          case 'ArrowUp':
            e.preventDefault();
            e.stopPropagation();
            triggerKeyboardMove(0, -1);
            break;
  
          case 'Down':
          case 'ArrowDown':
            e.preventDefault();
            e.stopPropagation();
            triggerKeyboardMove(0, 1);
            break;
        }
      };
  
      return moveProps;
    }, [state, onMoveStart, onMove, onMoveEnd, addGlobalListener, removeGlobalListener]);
    return {
      moveProps
    };
  }
  //# sourceMappingURL=useMove.web.js.map