import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { ref, computed, unref } from 'vue';
import { throttle } from 'lodash-es';
import { useEventListener } from '@vueuse/core';
import { depx } from '../_util/utils';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
const useDrag = (transform, domRef, domHeaderRef, getContainer, defaultPosition) => {
  const isDragging = ref(false);
  let startX;
  let startY;
  let imgOffsetX;
  let imgOffsetY;
  const limitTransformOffsetY = computed(() => {
    var _unref, _defaultPosition$bott, _defaultPosition$top;
    const containerEl = (_unref = unref(getContainer)) === null || _unref === void 0 ? void 0 : _unref();
    // 不考虑非整个窗口拖拽超出边界的情况
    if (containerEl !== document.body) {
      return null;
    }
    // 仅考虑以 px 为单位的初始位置进行边界碰撞计算
    if (defaultPosition.bottom && (_defaultPosition$bott = defaultPosition.bottom) !== null && _defaultPosition$bott !== void 0 && _defaultPosition$bott.endsWith('px')) {
      const bottom = depx(defaultPosition.bottom);
      return {
        min: -(window.innerHeight - bottom - domRef.value.offsetHeight),
        max: bottom + (domRef.value.offsetHeight - domHeaderRef.value.offsetHeight)
      };
    } else if (defaultPosition.top && (_defaultPosition$top = defaultPosition.top) !== null && _defaultPosition$top !== void 0 && _defaultPosition$top.endsWith('px')) {
      const top = depx(defaultPosition.top);
      return {
        min: top > 0 ? -top : 0,
        max: window.innerHeight - top - domHeaderRef.value.offsetHeight
      };
    }
    return null;
  });
  const limitTransformOffsetX = computed(() => {
    var _unref2;
    const containerEl = (_unref2 = unref(getContainer)) === null || _unref2 === void 0 ? void 0 : _unref2();
    // 不考虑非整个窗口拖拽超出边界的情况
    if (containerEl !== document.body) {
      return null;
    }
    // 仅考虑以 px 为单位的初始位置进行边界碰撞计算
    // 左右保留 100px 的缓冲区
    if (defaultPosition.right && defaultPosition.right.endsWith('px')) {
      const right = depx(defaultPosition.right);
      return {
        min: -(window.innerWidth - right - 100),
        max: right + (domRef.value.offsetWidth - 100)
      };
    } else if (defaultPosition.left && defaultPosition.left.endsWith('px')) {
      const left = depx(defaultPosition.left);
      return {
        min: left > 0 ? -left : 0,
        max: window.innerWidth - left - 100
      };
    }
    return null;
  });
  const handleMouseDown = event => {
    // 取消默认图片拖拽的行为
    event.preventDefault();
    isDragging.value = true;
    // 存储鼠标按下的偏移量和事件发生坐标
    const {
      offsetX,
      offsetY
    } = transform.value;
    startX = event.pageX;
    startY = event.pageY;
    imgOffsetX = offsetX;
    imgOffsetY = offsetY;
  };
  const handleDrag = throttle(event => {
    // 避免移动到窗口外
    if (event.clientY <= 0 || event.clientX <= 0 || event.clientX >= window.innerWidth || event.clientY >= window.innerHeight) {
      return;
    }
    const offsetX = imgOffsetX + event.pageX - startX;
    const offsetY = imgOffsetY + event.pageY - startY;
    if (limitTransformOffsetY.value) {
      if (offsetY < limitTransformOffsetY.value.min) {
        return;
      }
      if (offsetY > limitTransformOffsetY.value.max) {
        return;
      }
    }
    if (limitTransformOffsetX.value) {
      if (offsetX < limitTransformOffsetX.value.min) {
        return;
      }
      if (offsetX > limitTransformOffsetX.value.max) {
        return;
      }
    }
    transform.value = _objectSpread(_objectSpread({}, transform.value), {}, {
      offsetX,
      offsetY
    });
  });

  // mousemove 事件监听 document 拖拽效果更流畅
  useEventListener(document, 'mousemove', event => {
    if (!isDragging.value) {
      return;
    }
    handleDrag(event);
  });
  useEventListener(document, 'mouseup', () => {
    if (!isDragging.value) {
      return;
    }
    isDragging.value = false;
  });
  return {
    handleMouseDown,
    isDragging
  };
};

export { useDrag };
