/**
 * ldesign v0.4.6
 * (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 } from '@vue/composition-api';
import { ChevronDownIcon, ChevronLeftIcon, CloseIcon } from '@ldesign/icons-vue';
import useVModel from '../hooks/useVModel.js';
import useDefaultValue from '../hooks/useDefaultValue.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { setTransform } from '../utils/helper.js';
import props from './props.js';
import Container from './base/Container.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 '@babel/runtime/helpers/typeof';
import 'lodash/kebabCase';
import '../config-provider/useConfig.js';
import '../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-2cb61c79.js';
import 'dayjs';
import 'vue';
import 'lodash/camelCase';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../hooks/tnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../hooks/render-tnode.js';
import 'lodash/isObject';
import '../tooltip/index.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../tooltip/tooltip.js';
import '../_chunks/dep-b6ac6e96.js';
import '../popup/props.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../_common/js/utils/set-style.js';
import '../config-provider/config-receiver.js';
import '../utils/mixins.js';
import '../utils/event.js';
import '../popup/container.js';
import '../popup/utils.js';
import '../popup/trigger.js';
import '../tooltip/props.js';
import '../dialog/index.js';
import '../dialog/dialog.js';
import 'lodash/isNumber';
import 'lodash/throttle';
import '../button/index.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import 'lodash/mapKeys';
import 'lodash/isEqual';
import '../loading/plugin.js';
import '../config.js';
import '../loading/loading.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isArray';
import '../utils/ripple.js';
import '../button/props.js';
import '../hooks/useDestroyOnClose.js';
import '../_common/js/utils/getScrollbarWidth.js';
import '../dialog/stack.js';
import '../dialog/props.js';
import '../dialog/actions.js';
import '../dialog/plugin.js';
import '../config-provider/config-provider.js';
import 'lodash/cloneDeep';

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",
  model: {
    prop: "visible",
    event: "change"
  },
  props: _objectSpread({}, props),
  setup: function setup(props2, _ref) {
    var _props2$defaultIndex;
    var emit = _ref.emit;
    var classPrefix = usePrefixClass();
    var COMPONENT_NAME = usePrefixClass("image-viewer");
    var isExpand = ref(true);
    var showOverlayValue = computed(function () {
      return getOverlay(props2);
    });
    var _toRefs = toRefs(props2),
      index = _toRefs.index,
      visible = _toRefs.visible;
    var _useDefaultValue = useDefaultValue(index, (_props2$defaultIndex = props2.defaultIndex) !== null && _props2$defaultIndex !== void 0 ? _props2$defaultIndex : 0, props2.onIndexChange, "index", "index-change"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
      indexValue = _useDefaultValue2[0],
      setIndexValue = _useDefaultValue2[1];
    var _useVModel = useVModel(visible, props2.defaultVisible, function () {}, "change", "visible"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      visibleValue = _useVModel2[0],
      setVisibleValue = _useVModel2[1];
    var rootClass = computed(function () {
      return ["".concat(COMPONENT_NAME.value)];
    });
    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 imagesList = computed(function () {
      return formatImages(props2.images);
    });
    var currentImage = computed(function () {
      var _imagesList$value$ind;
      return (_imagesList$value$ind = imagesList.value[indexValue.value]) !== null && _imagesList$value$ind !== void 0 ? _imagesList$value$ind : {
        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 >= imagesList.value.length ? indexValue.value : newIndex, {
        trigger: "next"
      });
    };
    var onImgClick = function onImgClick(i) {
      setIndexValue(i, {
        trigger: i > indexValue.value ? "next" : "prev"
      });
    };
    var openHandler = function openHandler() {
      setVisibleValue(true);
    };
    var onCloseHandle = function onCloseHandle(ctx) {
      var _props2$onClose;
      setVisibleValue(false);
      (_props2$onClose = props2.onClose) === null || _props2$onClose === void 0 ? void 0 : _props2$onClose.call(props2, ctx);
      emit("close", ctx);
    };
    var closeBtnAction = function closeBtnAction(e) {
      onCloseHandle({
        e: e,
        trigger: "close-btn"
      });
    };
    var clickOverlayHandler = function clickOverlayHandler(e) {
      if (props2.closeOnOverlay) onCloseHandle({
        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:
          onCloseHandle({
            e: e,
            trigger: "esc"
          });
          break;
      }
    };
    var containerRef = ref();
    var mountContent = function mountContent() {
      if (containerRef.value) containerRef.value.mountContent();
    };
    var unmountContent = function unmountContent() {
      if (containerRef.value) containerRef.value.unmountContent();
    };
    watch(function () {
      return visibleValue.value;
    }, function (val) {
      if (val) {
        window.addEventListener("keydown", keydownHandler);
        mountContent();
        return;
      }
      window.removeEventListener("keydown", keydownHandler);
      unmountContent();
    });
    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 setTransform("translateX(calc(-".concat(indexValue.value, " * (40px / 9 * 16 + 4px)))"));
    });
    var isMultipleImg = computed(function () {
      return imagesList.value.length > 1;
    });
    return {
      COMPONENT_NAME: COMPONENT_NAME,
      rootClass: rootClass,
      classPrefix: classPrefix,
      prevImage: prevImage,
      nextImage: nextImage,
      zIndexValue: zIndexValue,
      visibleValue: visibleValue,
      indexValue: indexValue,
      imagesList: imagesList,
      showOverlayValue: showOverlayValue,
      wrapClass: wrapClass,
      rotate: rotate,
      mirror: mirror,
      currentImage: currentImage,
      onRotate: onRotate,
      onZoomIn: onZoomIn,
      onZoomOut: onZoomOut,
      onMirror: onMirror,
      onRest: onRest,
      openHandler: openHandler,
      onCloseHandle: onCloseHandle,
      onWheel: onWheel,
      clickOverlayHandler: clickOverlayHandler,
      headerClass: headerClass,
      toggleExpand: toggleExpand,
      transStyle: transStyle,
      onImgClick: onImgClick,
      closeBtnAction: closeBtnAction,
      scale: scale,
      isMultipleImg: isMultipleImg,
      containerRef: containerRef
    };
  },
  methods: {
    renderHeader: function renderHeader() {
      var _this = this;
      var h = this.$createElement;
      return h("div", {
        "class": this.headerClass
      }, [h(TImageViewerIcon, {
        "attrs": {
          "icon": function icon() {
            return h(ChevronDownIcon);
          },
          "clickHandler": this.toggleExpand
        },
        "class": "".concat(this.COMPONENT_NAME, "__header-pre-bt")
      }), h("div", {
        "class": "".concat(this.COMPONENT_NAME, "__header-prev")
      }, [h("div", {
        "class": "".concat(this.COMPONENT_NAME, "__header-trans"),
        "style": this.transStyle
      }, [this.imagesList.map(function (image, index) {
        return h("div", {
          "key": index,
          "class": ["".concat(_this.COMPONENT_NAME, "__header-box"), _defineProperty({}, "".concat(_this.classPrefix, "-is-active"), index === _this.indexValue)]
        }, [h("img", {
          "attrs": {
            "alt": "",
            "src": image.thumbnail || image.mainImage
          },
          "class": "".concat(_this.COMPONENT_NAME, "__header-img"),
          "on": {
            "click": function click() {
              return _this.onImgClick(index);
            }
          }
        })]);
      })])])]);
    },
    renderNavigationArrow: function renderNavigationArrow(type) {
      var h = this.$createElement;
      var rotateDeg = type === "prev" ? 0 : 180;
      var _icon = renderTNodeJSX(this, "navigationArrow", h(ChevronLeftIcon, {
        "style": setTransform("rotate(".concat(rotateDeg, "deg)")),
        "attrs": {
          "size": "24px"
        }
      }));
      return h(TImageViewerIcon, {
        "class": "".concat(this.COMPONENT_NAME, "__modal-").concat(type, "-bt"),
        "attrs": {
          "clickHandler": type === "prev" ? this.prevImage : this.nextImage,
          "icon": function icon() {
            return _icon;
          }
        }
      });
    },
    renderModal: function renderModal() {
      var h = this.$createElement;
      return h(TImageViewerModal, {
        "attrs": {
          "zIndex": this.zIndexValue,
          "visible": this.visibleValue,
          "index": this.indexValue,
          "images": this.imagesList,
          "scale": this.scale,
          "rotate": this.rotate,
          "mirror": this.mirror,
          "currentImage": this.currentImage,
          "rotateHandler": this.onRotate,
          "zoomInHandler": this.onZoomIn,
          "zoomOutHandler": this.onZoomOut,
          "mirrorHandler": this.onMirror,
          "resetHandler": this.onRest,
          "closeHandler": this.onCloseHandle,
          "draggable": this.draggable,
          "showOverlay": this.showOverlayValue,
          "closeBtn": this.closeBtn
        }
      });
    },
    renderViewer: function renderViewer() {
      var h = this.$createElement;
      return h("div", {
        "class": this.wrapClass,
        "style": {
          zIndex: this.zIndexValue
        },
        "on": {
          "wheel": this.onWheel
        }
      }, [!!this.showOverlayValue && h("div", {
        "class": "".concat(this.COMPONENT_NAME, "__modal-mask"),
        "on": {
          "click": this.clickOverlayHandler
        }
      }), this.isMultipleImg && this.renderHeader(), this.isMultipleImg && this.renderNavigationArrow("prev"), this.isMultipleImg && this.renderNavigationArrow("next"), this.isMultipleImg && h("div", {
        "class": "".concat(this.COMPONENT_NAME, "__modal-index")
      }, [renderTNodeJSX(this, "title"), "".concat(this.indexValue + 1, "/").concat(this.imagesList.length)]), h("div", {
        "class": ["".concat(this.COMPONENT_NAME, "__modal-icon"), "".concat(this.COMPONENT_NAME, "__modal-close-bt")],
        "on": {
          "click": this.closeBtnAction
        }
      }, [renderTNodeJSX(this, "closeBtn", h(CloseIcon, {
        "attrs": {
          "size": "24px"
        }
      }))]), h(TImageViewerUtils, {
        "attrs": {
          "zoomInHandler": this.onZoomIn,
          "zoomOutHandler": this.onZoomOut,
          "mirrorHandler": this.onMirror,
          "resetHandler": this.onRest,
          "rotateHandler": this.onRotate,
          "scale": this.scale,
          "currentImage": this.currentImage
        }
      }), h(TImageItem, {
        "attrs": {
          "scale": this.scale,
          "rotate": this.rotate,
          "mirror": this.mirror,
          "src": this.currentImage.mainImage,
          "placementSrc": this.currentImage.thumbnail
        }
      })]);
    }
  },
  render: function render() {
    var h = arguments[0];
    return h(Container, {
      "ref": "containerRef",
      "attrs": {
        "mode": this.mode,
        "renderModal": this.renderModal,
        "renderViewer": this.renderViewer
      }
    }, [renderTNodeJSX(this, "trigger", {
      params: {
        open: this.openHandler
      }
    })]);
  }
});

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