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

'use strict';

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

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var imageViewer_props = require('./props.js');
var imageViewer_base_ImageModalIcon = require('./base/ImageModalIcon.js');
var imageViewer_base_ImageViewerUtils = require('./base/ImageViewerUtils.js');
var imageViewer_base_ImageItem = require('./base/ImageItem.js');
var imageViewer_base_ImageViewerModal = require('./base/ImageViewerModal.js');
var imageViewer_hooks = require('./hooks.js');
var imageViewer_utils = require('./utils.js');
var imageViewer_const = require('./const.js');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isObject');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7f0559c3.js');
require('dayjs');
require('../tooltip/index.js');
require('../utils/withInstall.js');
require('../tooltip/tooltip.js');
require('lodash/omit');
require('../popup/props.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../utils/dom.js');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../popup/container.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../hooks/useResizeObserver.js');
require('../tooltip/util.js');
require('../tooltip/props.js');
require('../dialog/index.js');
require('../dialog/dialog.js');
require('../hooks/useGlobalIcon.js');
require('../hooks/useDestroyOnClose.js');
require('../_common/js/utils/getScrollbarWidth.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('../hooks/useTeleport.js');
require('../dialog/stack.js');
require('../dialog/hooks.js');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../form/hooks.js');
require('../button/props.js');
require('../dialog/props.js');
require('../dialog/plugin.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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__default["default"](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 = vue.defineComponent({
  name: "TImageViewer",
  props: _objectSpread({}, imageViewer_props["default"]),
  setup: function setup(props2) {
    var _props2$defaultIndex;
    var classPrefix = hooks_useConfig.usePrefixClass();
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("image-viewer");
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var isExpand = vue.ref(true);
    var showOverlayValue = vue.computed(function () {
      return imageViewer_utils.getOverlay(props2);
    });
    var _toRefs = vue.toRefs(props2),
      index = _toRefs.index,
      visible = _toRefs.visible,
      modelValue = _toRefs.modelValue;
    var _useDefaultValue = hooks_useDefaultValue["default"](index, (_props2$defaultIndex = props2.defaultIndex) !== null && _props2$defaultIndex !== void 0 ? _props2$defaultIndex : 0, props2.onIndexChange, "index"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      indexValue = _useDefaultValue2[0],
      setIndexValue = _useDefaultValue2[1];
    var _useVModel = hooks_useVModel["default"](visible, modelValue, props2.defaultVisible, function () {}, "visible"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      visibleValue = _useVModel2[0],
      setVisibleValue = _useVModel2[1];
    var animationEnd = vue.ref(true);
    var animationTimer = vue.ref();
    var wrapClass = vue.computed(function () {
      return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "-preview-image"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-hide"), !visibleValue.value)];
    });
    var headerClass = vue.computed(function () {
      return ["".concat(classPrefix.value, "-image-viewer__modal-header"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-show"), isExpand.value)];
    });
    var zIndexValue = vue.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 = imageViewer_hooks.useMirror(),
      mirror = _useMirror.mirror,
      onMirror = _useMirror.onMirror,
      resetMirror = _useMirror.resetMirror;
    var _useScale = imageViewer_hooks.useScale(props2.imageScale),
      scale = _useScale.scale,
      onZoomIn = _useScale.onZoomIn,
      onZoomOut = _useScale.onZoomOut,
      resetScale = _useScale.resetScale;
    var _useRotate = imageViewer_hooks.useRotate(),
      rotate = _useRotate.rotate,
      onRotate = _useRotate.onRotate,
      resetRotate = _useRotate.resetRotate;
    var onRest = function onRest() {
      resetMirror();
      resetScale();
      resetRotate();
    };
    var images = vue.computed(function () {
      return imageViewer_utils.formatImages(props2.images);
    });
    var currentImage = vue.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 imageViewer_const.EVENT_CODE.left:
          prevImage();
          break;
        case imageViewer_const.EVENT_CODE.right:
          nextImage();
          break;
        case imageViewer_const.EVENT_CODE.up:
          onZoomIn();
          break;
        case imageViewer_const.EVENT_CODE.down:
          onZoomOut();
          break;
        case imageViewer_const.EVENT_CODE.esc:
          onClose({
            e: e,
            trigger: "esc"
          });
          break;
      }
    };
    vue.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 = vue.computed(function () {
      return {
        transform: "translateX(calc(-".concat(indexValue.value, " * (40px / 9 * 16 + 4px)))")
      };
    });
    var renderHeader = function renderHeader() {
      return vue.createVNode("div", {
        "class": headerClass.value
      }, [vue.createVNode(imageViewer_base_ImageModalIcon["default"], {
        "icon": function icon() {
          return vue.createVNode(iconsVueNext.ChevronDownIcon, null, null);
        },
        "class": "".concat(COMPONENT_NAME.value, "__header-pre-bt"),
        "onClick": toggleExpand
      }, null), vue.createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__header-prev")
      }, [vue.createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__header-trans"),
        "style": transStyle.value
      }, [images.value.map(function (image, index2) {
        return vue.createVNode("div", {
          "key": index2,
          "class": ["".concat(COMPONENT_NAME.value, "__header-box"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-active"), index2 === indexValue.value)]
        }, [vue.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", vue.createVNode(iconsVueNext.ChevronLeftIcon, {
        "style": {
          transform: "rotate(".concat(rotateDeg, "deg)")
        },
        "size": "24px"
      }, null));
      return vue.createVNode(imageViewer_base_ImageModalIcon["default"], {
        "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 vue.createVNode(vue.Fragment, null, [renderTNodeJSX("trigger", {
          params: {
            open: openHandler
          }
        }), vue.createVNode(imageViewer_base_ImageViewerModal["default"], {
          "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 vue.createVNode(vue.Fragment, null, [renderTNodeJSX("trigger", {
        params: {
          open: openHandler
        }
      }), vue.createVNode(vue.Teleport, {
        "to": "body"
      }, {
        "default": function _default() {
          return [vue.createVNode(vue.Transition, null, {
            "default": function _default() {
              return [(visibleValue.value || !animationEnd.value) && vue.withDirectives(vue.createVNode("div", {
                "class": wrapClass.value,
                "style": {
                  zIndex: zIndexValue.value
                },
                "onWheel": onWheel
              }, [!!showOverlayValue.value && vue.createVNode("div", {
                "class": "".concat(COMPONENT_NAME.value, "__modal-mask"),
                "onClick": clickOverlayHandler
              }, null), images.value.length > 1 && vue.createVNode(vue.Fragment, null, [renderHeader(), vue.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")]), vue.createVNode("div", {
                "class": ["".concat(COMPONENT_NAME.value, "__modal-icon"), "".concat(COMPONENT_NAME.value, "__modal-close-bt")],
                "onClick": closeBtnAction
              }, [renderTNodeJSX("closeBtn", vue.createVNode(iconsVueNext.CloseIcon, {
                "size": "24px"
              }, null))]), vue.createVNode(imageViewer_base_ImageViewerUtils["default"], {
                "onZoomIn": onZoomIn,
                "onZoomOut": onZoomOut,
                "onMirror": onMirror,
                "onReset": onRest,
                "onRotate": onRotate,
                "scale": scale.value,
                "currentImage": currentImage.value
              }, null), vue.createVNode(imageViewer_base_ImageItem["default"], {
                "scale": scale.value,
                "rotate": rotate.value,
                "mirror": mirror.value,
                "src": currentImage.value.mainImage,
                "placementSrc": currentImage.value.thumbnail
              }, null)]), [[vue.vShow, visibleValue.value]])];
            }
          })];
        }
      })]);
    };
  }
});

exports["default"] = _ImageViewer;
//# sourceMappingURL=image-viewer.js.map
