/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { defineComponent, ref, computed, toRefs, watch, createVNode, Fragment, Teleport, Transition, withDirectives, vShow } from 'vue';
import { ChevronDownIcon, ChevronLeftIcon, CloseIcon } from '@ldesign/icons-vue-next';
import { useTNodeJSX } from '../hooks/tnode.mjs';
import useVModel from '../hooks/useVModel.mjs';
import useDefaultValue from '../hooks/useDefaultValue.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import props from './props.mjs';
import TImageViewerIcon from './base/ImageModalIcon.mjs';
import TImageViewerUtils from './base/ImageViewerUtils.mjs';
import TImageItem from './base/ImageItem.mjs';
import TImageViewerModal from './base/ImageViewerModal.mjs';
import { useMirror, useScale, useRotate } from './hooks.mjs';
import { getOverlay, formatImages } from './utils.mjs';
import { EVENT_CODE } from './const.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../tooltip/index.mjs';
import '../utils/withInstall.mjs';
import '../tooltip/tooltip.mjs';
import '../_chunks/dep-350764bc.mjs';
import '../_chunks/dep-0d3dd4b5.mjs';
import '../_chunks/dep-6884627f.mjs';
import '../popup/props.mjs';
import '../popup/index.mjs';
import '../popup/popup.mjs';
import '@popperjs/core';
import '../utils/dom.mjs';
import '../utils/easing.mjs';
import '../_common/js/utils/set-style.mjs';
import '../popup/container.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-78847b2e.mjs';
import '../hooks/useResizeObserver.mjs';
import '../popup/style/css.mjs';
import '../popup/type.mjs';
import '../tooltip/util.mjs';
import '../tooltip/props.mjs';
import '../tooltip/style/css.mjs';
import '../tooltip/type.mjs';
import '../dialog/index.mjs';
import '../dialog/dialog.mjs';
import '../hooks/useGlobalIcon.mjs';
import '../hooks/useDestroyOnClose.mjs';
import '../_common/js/utils/getScrollbarWidth.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-60fae668.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../_chunks/dep-3f21269a.mjs';
import '../hooks/useTeleport.mjs';
import '../dialog/stack.mjs';
import '../dialog/hooks.mjs';
import '../button/index.mjs';
import '../button/button.mjs';
import '../loading/index.mjs';
import '../loading/directive.mjs';
import '../loading/plugin.mjs';
import '../loading/loading.mjs';
import '../loading/props.mjs';
import '../loading/icon/gradient.mjs';
import '../_common/js/loading/circle-adapter.mjs';
import '../loading/style/css.mjs';
import '../loading/type.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../form/hooks.mjs';
import '../button/props.mjs';
import '../button/style/css.mjs';
import '../button/type.mjs';
import '../dialog/props.mjs';
import '../dialog/style/css.mjs';
import '../dialog/type.mjs';
import '../dialog/plugin.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _ImageViewer = defineComponent({
  name: "TImageViewer",
  props: _objectSpread({}, props),
  setup: function setup(props2) {
    var _props2$defaultIndex;
    var classPrefix = usePrefixClass();
    var COMPONENT_NAME = usePrefixClass("image-viewer");
    var renderTNodeJSX = useTNodeJSX();
    var isExpand = ref(true);
    var showOverlayValue = computed(function () {
      return getOverlay(props2);
    });
    var _toRefs = toRefs(props2),
      index = _toRefs.index,
      visible = _toRefs.visible,
      modelValue = _toRefs.modelValue;
    var _useDefaultValue = useDefaultValue(index, (_props2$defaultIndex = props2.defaultIndex) !== null && _props2$defaultIndex !== void 0 ? _props2$defaultIndex : 0, props2.onIndexChange, "index"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
      indexValue = _useDefaultValue2[0],
      setIndexValue = _useDefaultValue2[1];
    var _useVModel = useVModel(visible, modelValue, props2.defaultVisible, function () {}, "visible"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      visibleValue = _useVModel2[0],
      setVisibleValue = _useVModel2[1];
    var animationEnd = ref(true);
    var animationTimer = ref();
    var wrapClass = computed(function () {
      return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "-preview-image"), _defineProperty({}, "".concat(classPrefix.value, "-is-hide"), !visibleValue.value)];
    });
    var headerClass = computed(function () {
      return ["".concat(classPrefix.value, "-image-viewer__modal-header"), _defineProperty({}, "".concat(classPrefix.value, "-is-show"), isExpand.value)];
    });
    var zIndexValue = computed(function () {
      var _props2$zIndex;
      return (_props2$zIndex = props2.zIndex) !== null && _props2$zIndex !== void 0 ? _props2$zIndex : 2600;
    });
    var toggleExpand = function toggleExpand() {
      isExpand.value = !isExpand.value;
    };
    var _useMirror = useMirror(),
      mirror = _useMirror.mirror,
      onMirror = _useMirror.onMirror,
      resetMirror = _useMirror.resetMirror;
    var _useScale = useScale(props2.imageScale),
      scale = _useScale.scale,
      onZoomIn = _useScale.onZoomIn,
      onZoomOut = _useScale.onZoomOut,
      resetScale = _useScale.resetScale;
    var _useRotate = useRotate(),
      rotate = _useRotate.rotate,
      onRotate = _useRotate.onRotate,
      resetRotate = _useRotate.resetRotate;
    var onRest = function onRest() {
      resetMirror();
      resetScale();
      resetRotate();
    };
    var images = computed(function () {
      return formatImages(props2.images);
    });
    var currentImage = computed(function () {
      var _images$value$indexVa;
      return (_images$value$indexVa = images.value[indexValue.value]) !== null && _images$value$indexVa !== void 0 ? _images$value$indexVa : {
        mainImage: ""
      };
    });
    var prevImage = function prevImage() {
      var newIndex = indexValue.value - 1;
      setIndexValue(newIndex < 0 ? 0 : newIndex, {
        trigger: "prev"
      });
    };
    var nextImage = function nextImage() {
      var newIndex = indexValue.value + 1;
      setIndexValue(newIndex >= images.value.length ? indexValue.value : newIndex, {
        trigger: "next"
      });
    };
    var onImgClick = function onImgClick(i) {
      setIndexValue(i);
    };
    var openHandler = function openHandler() {
      setVisibleValue(true);
    };
    var onClose = function onClose(ctx) {
      var _props2$onClose;
      setVisibleValue(false, ctx);
      (_props2$onClose = props2.onClose) === null || _props2$onClose === void 0 ? void 0 : _props2$onClose.call(props2, ctx);
    };
    var closeBtnAction = function closeBtnAction(e) {
      onClose({
        e: e,
        trigger: "close-btn"
      });
    };
    var clickOverlayHandler = function clickOverlayHandler(e) {
      if (props2.closeOnOverlay) onClose({
        e: e,
        trigger: "overlay"
      });
    };
    var keydownHandler = function keydownHandler(e) {
      switch (e.code) {
        case EVENT_CODE.left:
          prevImage();
          break;
        case EVENT_CODE.right:
          nextImage();
          break;
        case EVENT_CODE.up:
          onZoomIn();
          break;
        case EVENT_CODE.down:
          onZoomOut();
          break;
        case EVENT_CODE.esc:
          onClose({
            e: e,
            trigger: "esc"
          });
          break;
        default:
          break;
      }
    };
    watch(function () {
      return visibleValue.value;
    }, function (val) {
      clearTimeout(animationTimer.value);
      if (val) {
        animationEnd.value = false;
        window.addEventListener("keydown", keydownHandler);
      } else {
        animationTimer.value = setTimeout(function () {
          animationEnd.value = true;
        }, 200);
        window.removeEventListener("keydown", keydownHandler);
      }
    });
    var onWheel = function onWheel(e) {
      e.preventDefault();
      var deltaY = e.deltaY,
        ctrlKey = e.ctrlKey;
      if (ctrlKey) return deltaY > 0 ? onZoomOut() : onZoomIn();
      deltaY > 0 ? onZoomIn() : onZoomOut();
    };
    var transStyle = computed(function () {
      return {
        transform: "translateX(calc(-".concat(indexValue.value, " * (40px / 9 * 16 + 4px)))")
      };
    });
    var renderHeader = function renderHeader() {
      return createVNode("div", {
        "class": headerClass.value
      }, [createVNode(TImageViewerIcon, {
        "icon": function icon() {
          return createVNode(ChevronDownIcon, null, null);
        },
        "class": "".concat(COMPONENT_NAME.value, "__header-pre-bt"),
        "onClick": toggleExpand
      }, null), createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__header-prev")
      }, [createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__header-trans"),
        "style": transStyle.value
      }, [images.value.map(function (image, index2) {
        return createVNode("div", {
          "key": index2,
          "class": ["".concat(COMPONENT_NAME.value, "__header-box"), _defineProperty({}, "".concat(classPrefix.value, "-is-active"), index2 === indexValue.value)]
        }, [createVNode("img", {
          "alt": "",
          "src": image.thumbnail || image.mainImage,
          "class": "".concat(COMPONENT_NAME.value, "__header-img"),
          "onClick": function onClick() {
            return onImgClick(index2);
          }
        }, null)]);
      })])])]);
    };
    var renderNavigationArrow = function renderNavigationArrow(type) {
      var rotateDeg = type === "prev" ? 0 : 180;
      var _icon = renderTNodeJSX("navigationArrow", createVNode(ChevronLeftIcon, {
        "style": {
          transform: "rotate(".concat(rotateDeg, "deg)")
        },
        "size": "24px"
      }, null));
      return createVNode(TImageViewerIcon, {
        "class": "".concat(COMPONENT_NAME.value, "__modal-").concat(type, "-bt"),
        "onClick": type === "prev" ? prevImage : nextImage,
        "icon": function icon() {
          return _icon;
        }
      }, null);
    };
    return function () {
      if (props2.mode === "modeless") {
        return createVNode(Fragment, null, [renderTNodeJSX("trigger", {
          params: {
            open: openHandler
          }
        }), createVNode(TImageViewerModal, {
          "zIndex": zIndexValue.value,
          "visible": visibleValue.value,
          "index": indexValue.value,
          "images": images.value,
          "scale": scale.value,
          "rotate": rotate.value,
          "mirror": mirror.value,
          "currentImage": currentImage.value,
          "onRotate": onRotate,
          "onZoomIn": onZoomIn,
          "onZoomOut": onZoomOut,
          "onMirror": onMirror,
          "onReset": onRest,
          "onClose": onClose,
          "draggable": props2.draggable,
          "showOverlay": showOverlayValue.value,
          "title": props2.title
        }, null)]);
      }
      return createVNode(Fragment, null, [renderTNodeJSX("trigger", {
        params: {
          open: openHandler
        }
      }), createVNode(Teleport, {
        "to": "body"
      }, {
        "default": function _default() {
          return [createVNode(Transition, null, {
            "default": function _default() {
              return [(visibleValue.value || !animationEnd.value) && withDirectives(createVNode("div", {
                "class": wrapClass.value,
                "style": {
                  zIndex: zIndexValue.value
                },
                "onWheel": onWheel
              }, [!!showOverlayValue.value && createVNode("div", {
                "class": "".concat(COMPONENT_NAME.value, "__modal-mask"),
                "onClick": clickOverlayHandler
              }, null), images.value.length > 1 && createVNode(Fragment, null, [renderHeader(), createVNode("div", {
                "class": "".concat(COMPONENT_NAME.value, "__modal-index")
              }, [props2.title && renderTNodeJSX("title"), "".concat(indexValue.value + 1, "/").concat(images.value.length)]), renderNavigationArrow("prev"), renderNavigationArrow("next")]), createVNode("div", {
                "class": ["".concat(COMPONENT_NAME.value, "__modal-icon"), "".concat(COMPONENT_NAME.value, "__modal-close-bt")],
                "onClick": closeBtnAction
              }, [renderTNodeJSX("closeBtn", createVNode(CloseIcon, {
                "size": "24px"
              }, null))]), createVNode(TImageViewerUtils, {
                "onZoomIn": onZoomIn,
                "onZoomOut": onZoomOut,
                "onMirror": onMirror,
                "onReset": onRest,
                "onRotate": onRotate,
                "scale": scale.value,
                "currentImage": currentImage.value
              }, null), createVNode(TImageItem, {
                "scale": scale.value,
                "rotate": rotate.value,
                "mirror": mirror.value,
                "src": currentImage.value.mainImage,
                "placementSrc": currentImage.value.thumbnail
              }, null)]), [[vShow, visibleValue.value]])];
            }
          })];
        }
      })]);
    };
  }
});

export { _ImageViewer as default };
//# sourceMappingURL=image-viewer.mjs.map
