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

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, computed, toRefs, watch, createVNode, Fragment, Teleport, Transition, withDirectives, vShow } from 'vue';
import { CloseIcon, ChevronDownIcon, ChevronLeftIcon } from '@ldesign/icons-vue-next';
import { useTNodeJSX } from '../hooks/tnode.js';
import useVModel from '../hooks/useVModel.js';
import useDefaultValue from '../hooks/useDefaultValue.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import props from './props.js';
import TImageViewerIcon from './base/ImageModalIcon.js';
import TImageViewerUtils from './base/ImageViewerUtils.js';
import TImageItem from './base/ImageItem.js';
import TImageViewerModal from './base/ImageViewerModal.js';
import { useMirror, useScale, useRotate } from './hooks.js';
import { getOverlay, formatImages } from './utils.js';
import { EVENT_CODE } from './const.js';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isString';
import 'lodash/isObject';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-e041f5b0.js';
import 'dayjs';
import '../tooltip/index.js';
import '../utils/withInstall.js';
import '../tooltip/tooltip.js';
import 'lodash/omit';
import '../popup/props.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../utils/dom.js';
import '../utils/easing.js';
import '../_common/js/utils/set-style.js';
import '../popup/container.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/isUndefined';
import '../hooks/useResizeObserver.js';
import '../tooltip/util.js';
import '../tooltip/props.js';
import '../dialog/index.js';
import '../dialog/dialog.js';
import '../hooks/useGlobalIcon.js';
import '../hooks/useDestroyOnClose.js';
import '../_common/js/utils/getScrollbarWidth.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/isNull';
import 'lodash/isNumber';
import '../hooks/useTeleport.js';
import '../dialog/stack.js';
import '../dialog/hooks.js';
import '../button/index.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../loading/loading.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../form/hooks.js';
import '../button/props.js';
import '../dialog/props.js';
import '../dialog/plugin.js';

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;
      }
    };
    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.js.map
