'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var IDialogPropTypes = require('../../vc-dialog/src/IDialogPropTypes.cjs');
var DialogWrap = require('../../vc-dialog/src/DialogWrap.cjs');
var useFrameSetState = require('./hooks/useFrameSetState.cjs');
var getFixScaleEleTransPosition = require('./getFixScaleEleTransPosition.cjs');
var context = require('./context.cjs');
var _class = require('../../../utils/dom/class.cjs');
var css = require('../../../utils/dom/css.cjs');
var KeyCode = require('../../../utils/KeyCode.cjs');
var addEventListener = require('../../../utils/event/addEventListener.cjs');
var log = require('../../../utils/log.cjs');

const initialPosition = {
  x: 0,
  y: 0
};
const previewProps = {
  ...IDialogPropTypes.dialogPropTypes(),
  src: String,
  alt: String,
  rootClassName: String,
  icons: {
    type: Object,
    default: () => ({})
  }
};
const Preview = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "Preview",
  inheritAttrs: false,
  props: previewProps,
  emits: ["close", "afterClose"],
  setup(props, {
    emit,
    attrs
  }) {
    const {
      rotateLeft,
      rotateRight,
      zoomIn,
      zoomOut,
      close,
      left,
      right,
      flipX,
      flipY
    } = vue.reactive(props.icons);
    const scale = vue.shallowRef(1);
    const rotate = vue.shallowRef(0);
    const flip = vue.reactive({
      x: 1,
      y: 1
    });
    const [position, setPosition] = useFrameSetState.default(initialPosition);
    const onClose = () => emit("close");
    const imgRef = vue.shallowRef();
    const originPositionRef = vue.reactive({
      originX: 0,
      originY: 0,
      deltaX: 0,
      deltaY: 0
    });
    const isMoving = vue.shallowRef(false);
    const groupContext = context.imageContext.inject();
    const {
      previewUrls,
      current,
      isPreviewGroup,
      setCurrent
    } = groupContext;
    const previewGroupCount = vue.computed(() => previewUrls.value.size);
    const previewUrlsKeys = vue.computed(() => Array.from(previewUrls.value.keys()));
    const currentPreviewIndex = vue.computed(() => previewUrlsKeys.value.indexOf(current.value));
    const combinationSrc = vue.computed(() => {
      return isPreviewGroup.value ? previewUrls.value.get(current.value) : props.src;
    });
    const showLeftOrRightSwitches = vue.computed(() => isPreviewGroup.value && previewGroupCount.value > 1);
    const lastWheelZoomDirection = vue.shallowRef({
      wheelDirection: 0
    });
    const onAfterClose = () => {
      scale.value = 1;
      rotate.value = 0;
      flip.x = 1;
      flip.y = 1;
      setPosition(initialPosition);
      emit("afterClose");
    };
    const onZoomIn = (isWheel) => {
      if (!isWheel)
        scale.value++;
      else
        scale.value += 0.5;
      setPosition(initialPosition);
    };
    const onZoomOut = (isWheel) => {
      if (scale.value > 1) {
        if (!isWheel)
          scale.value--;
        else
          scale.value -= 0.5;
      }
      setPosition(initialPosition);
    };
    const onRotateRight = () => {
      rotate.value += 90;
    };
    const onRotateLeft = () => {
      rotate.value -= 90;
    };
    const onFlipX = () => {
      flip.x = -flip.x;
    };
    const onFlipY = () => {
      flip.y = -flip.y;
    };
    const onSwitchLeft = (event) => {
      event.preventDefault();
      event.stopPropagation();
      if (currentPreviewIndex.value > 0)
        setCurrent(previewUrlsKeys.value[currentPreviewIndex.value - 1]);
    };
    const onSwitchRight = (event) => {
      event.preventDefault();
      event.stopPropagation();
      if (currentPreviewIndex.value < previewGroupCount.value - 1)
        setCurrent(previewUrlsKeys.value[currentPreviewIndex.value + 1]);
    };
    const wrapClassName = _class.classNames({
      [`${props.prefixCls}-moving`]: isMoving.value
    });
    const toolClassName = `${props.prefixCls}-operations-operation`;
    const iconClassName = `${props.prefixCls}-operations-icon`;
    const tools = [{
      icon: close,
      onClick: onClose,
      type: "close"
    }, {
      icon: zoomIn,
      onClick: () => onZoomIn(),
      type: "zoomIn"
    }, {
      icon: zoomOut,
      onClick: () => onZoomOut(),
      type: "zoomOut",
      disabled: vue.computed(() => scale.value === 1)
    }, {
      icon: rotateRight,
      onClick: onRotateRight,
      type: "rotateRight"
    }, {
      icon: rotateLeft,
      onClick: onRotateLeft,
      type: "rotateLeft"
    }, {
      icon: flipX,
      onClick: onFlipX,
      type: "flipX"
    }, {
      icon: flipY,
      onClick: onFlipY,
      type: "flipY"
    }];
    const onMouseUp = () => {
      if (props.visible && isMoving.value) {
        const width = imgRef.value.offsetWidth * scale.value;
        const height = imgRef.value.offsetHeight * scale.value;
        const {
          left: left2,
          top
        } = css.getOffset(imgRef.value);
        const isRotate = rotate.value % 180 !== 0;
        isMoving.value = false;
        const fixState = getFixScaleEleTransPosition.default(isRotate ? height : width, isRotate ? width : height, left2, top);
        if (fixState)
          setPosition({
            ...fixState
          });
      }
    };
    const onMouseDown = (event) => {
      if (event.button !== 0)
        return;
      event.preventDefault();
      event.stopPropagation();
      originPositionRef.deltaX = event.pageX - position.x;
      originPositionRef.deltaY = event.pageY - position.y;
      originPositionRef.originX = position.x;
      originPositionRef.originY = position.y;
      isMoving.value = true;
    };
    const onMouseMove = (event) => {
      if (props.visible && isMoving.value) {
        setPosition({
          x: event.pageX - originPositionRef.deltaX,
          y: event.pageY - originPositionRef.deltaY
        });
      }
    };
    const onWheelMove = (event) => {
      if (!props.visible)
        return;
      event.preventDefault();
      const wheelDirection = event.deltaY;
      lastWheelZoomDirection.value = {
        wheelDirection
      };
    };
    const onKeyDown = (event) => {
      if (!props.visible || !showLeftOrRightSwitches.value)
        return;
      event.preventDefault();
      if (event.keyCode === KeyCode.KeyCode.LEFT) {
        if (currentPreviewIndex.value > 0)
          setCurrent(previewUrlsKeys.value[currentPreviewIndex.value - 1]);
      } else if (event.keyCode === KeyCode.KeyCode.RIGHT) {
        if (currentPreviewIndex.value < previewGroupCount.value - 1)
          setCurrent(previewUrlsKeys.value[currentPreviewIndex.value + 1]);
      }
    };
    const onDoubleClick = () => {
      if (props.visible) {
        if (scale.value !== 1)
          scale.value = 1;
        if (position.x !== initialPosition.x || position.y !== initialPosition.y)
          setPosition(initialPosition);
      }
    };
    let removeListeners = () => {
    };
    vue.onMounted(() => {
      vue.watch([() => props.visible, isMoving], () => {
        removeListeners();
        let onTopMouseUpListener;
        let onTopMouseMoveListener;
        const onMouseUpListener = addEventListener.addEventListenerWrap(window, "mouseup", onMouseUp, false);
        const onMouseMoveListener = addEventListener.addEventListenerWrap(window, "mousemove", onMouseMove, false);
        const onScrollWheelListener = addEventListener.addEventListenerWrap(window, "wheel", onWheelMove, {
          passive: false
        });
        const onKeyDownListener = addEventListener.addEventListenerWrap(window, "keydown", onKeyDown, false);
        try {
          if (window.top !== window.self) {
            onTopMouseUpListener = addEventListener.addEventListenerWrap(window.top, "mouseup", onMouseUp, false);
            onTopMouseMoveListener = addEventListener.addEventListenerWrap(window.top, "mousemove", onMouseMove, false);
          }
        } catch (error) {
          log.warning(false, `[vc-image] ${error}`);
        }
        removeListeners = () => {
          onMouseUpListener.remove();
          onMouseMoveListener.remove();
          onScrollWheelListener.remove();
          onKeyDownListener.remove();
          if (onTopMouseUpListener)
            onTopMouseUpListener.remove();
          if (onTopMouseMoveListener)
            onTopMouseMoveListener.remove();
        };
      }, {
        flush: "post",
        immediate: true
      });
      vue.watch([lastWheelZoomDirection], () => {
        const {
          wheelDirection
        } = lastWheelZoomDirection.value;
        if (wheelDirection > 0)
          onZoomOut(true);
        else if (wheelDirection < 0)
          onZoomIn(true);
      });
    });
    vue.onUnmounted(() => {
      removeListeners();
    });
    return () => {
      const {
        visible,
        prefixCls,
        rootClassName
      } = props;
      return vue.createVNode(DialogWrap.default, {
        ...attrs,
        "transitionName": props.transitionName,
        "maskTransitionName": props.maskTransitionName,
        "closable": false,
        "keyboard": true,
        "prefixCls": prefixCls,
        "onClose": onClose,
        "afterClose": onAfterClose,
        "visible": visible,
        "wrapClassName": wrapClassName,
        "rootClassName": rootClassName,
        "getContainer": props.getContainer
      }, {
        default: () => [vue.createVNode("div", {
          "class": [`${props.prefixCls}-operations-wrapper`, rootClassName]
        }, [vue.createVNode("ul", {
          "class": `${props.prefixCls}-operations`
        }, [tools.map(({
          icon: IconType,
          onClick,
          type,
          disabled
        }) => vue.createVNode("li", {
          "class": _class.classNames(toolClassName, {
            [`${props.prefixCls}-operations-operation-disabled`]: disabled && (disabled == null ? void 0 : disabled.value)
          }),
          "onClick": onClick,
          "key": type
        }, [vue.cloneVNode(IconType, {
          class: iconClassName
        })]))])]), vue.createVNode("div", {
          "class": `${props.prefixCls}-img-wrapper`,
          "style": {
            transform: `translate3d(${position.x}px, ${position.y}px, 0)`
          }
        }, [vue.createVNode("img", {
          "onMousedown": onMouseDown,
          "onDblclick": onDoubleClick,
          "ref": imgRef,
          "class": `${props.prefixCls}-img`,
          "src": combinationSrc.value,
          "alt": props.alt,
          "style": {
            transform: `scale3d(${flip.x * scale.value}, ${flip.y * scale.value}, 1) rotate(${rotate.value}deg)`
          }
        }, null)]), showLeftOrRightSwitches.value && vue.createVNode("div", {
          "class": _class.classNames(`${props.prefixCls}-switch-left`, {
            [`${props.prefixCls}-switch-left-disabled`]: currentPreviewIndex.value <= 0
          }),
          "onClick": onSwitchLeft
        }, [left]), showLeftOrRightSwitches.value && vue.createVNode("div", {
          "class": _class.classNames(`${props.prefixCls}-switch-right`, {
            [`${props.prefixCls}-switch-right-disabled`]: currentPreviewIndex.value >= previewGroupCount.value - 1
          }),
          "onClick": onSwitchRight
        }, [right])]
      });
    };
  }
});

exports.default = Preview;
exports.previewProps = previewProps;
//# sourceMappingURL=Preview.cjs.map
