/**
 * ldesign v0.4.6
 * (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 VueCompositionAPI = require('@vue/composition-api');
var iconsVue = require('@ldesign/icons-vue');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var utils_helper = require('../utils/helper.js');
var imageViewer_props = require('./props.js');
var imageViewer_base_Container = require('./base/Container.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('@babel/runtime/helpers/typeof');
require('lodash/kebabCase');
require('../config-provider/useConfig.js');
require('../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../_chunks/dep-7c884501.js');
require('dayjs');
require('vue');
require('lodash/camelCase');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../hooks/tnode.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('../hooks/render-tnode.js');
require('lodash/isObject');
require('../tooltip/index.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../tooltip/tooltip.js');
require('../_chunks/dep-c3e1ffac.js');
require('../popup/props.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../_common/js/utils/set-style.js');
require('../config-provider/config-receiver.js');
require('../utils/mixins.js');
require('../utils/event.js');
require('../popup/container.js');
require('../popup/utils.js');
require('../popup/trigger.js');
require('../tooltip/props.js');
require('../dialog/index.js');
require('../dialog/dialog.js');
require('lodash/isNumber');
require('lodash/throttle');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('lodash/mapKeys');
require('lodash/isEqual');
require('../loading/plugin.js');
require('../config.js');
require('../loading/loading.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isArray');
require('../utils/ripple.js');
require('../button/props.js');
require('../hooks/useDestroyOnClose.js');
require('../_common/js/utils/getScrollbarWidth.js');
require('../dialog/stack.js');
require('../dialog/props.js');
require('../dialog/actions.js');
require('../dialog/plugin.js');
require('../config-provider/config-provider.js');
require('lodash/cloneDeep');

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 = VueCompositionAPI.defineComponent({
  name: "TImageViewer",
  model: {
    prop: "visible",
    event: "change"
  },
  props: _objectSpread({}, imageViewer_props["default"]),
  setup: function setup(props2, _ref) {
    var _props2$defaultIndex;
    var emit = _ref.emit;
    var classPrefix = hooks_useConfig.usePrefixClass();
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("image-viewer");
    var isExpand = VueCompositionAPI.ref(true);
    var showOverlayValue = VueCompositionAPI.computed(function () {
      return imageViewer_utils.getOverlay(props2);
    });
    var _toRefs = VueCompositionAPI.toRefs(props2),
      index = _toRefs.index,
      visible = _toRefs.visible;
    var _useDefaultValue = hooks_useDefaultValue["default"](index, (_props2$defaultIndex = props2.defaultIndex) !== null && _props2$defaultIndex !== void 0 ? _props2$defaultIndex : 0, props2.onIndexChange, "index", "index-change"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      indexValue = _useDefaultValue2[0],
      setIndexValue = _useDefaultValue2[1];
    var _useVModel = hooks_useVModel["default"](visible, props2.defaultVisible, function () {}, "change", "visible"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      visibleValue = _useVModel2[0],
      setVisibleValue = _useVModel2[1];
    var rootClass = VueCompositionAPI.computed(function () {
      return ["".concat(COMPONENT_NAME.value)];
    });
    var wrapClass = VueCompositionAPI.computed(function () {
      return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "-preview-image"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-hide"), !visibleValue.value)];
    });
    var headerClass = VueCompositionAPI.computed(function () {
      return ["".concat(classPrefix.value, "-image-viewer__modal-header"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-show"), isExpand.value)];
    });
    var zIndexValue = VueCompositionAPI.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 imagesList = VueCompositionAPI.computed(function () {
      return imageViewer_utils.formatImages(props2.images);
    });
    var currentImage = VueCompositionAPI.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 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:
          onCloseHandle({
            e: e,
            trigger: "esc"
          });
          break;
      }
    };
    var containerRef = VueCompositionAPI.ref();
    var mountContent = function mountContent() {
      if (containerRef.value) containerRef.value.mountContent();
    };
    var unmountContent = function unmountContent() {
      if (containerRef.value) containerRef.value.unmountContent();
    };
    VueCompositionAPI.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 = VueCompositionAPI.computed(function () {
      return utils_helper.setTransform("translateX(calc(-".concat(indexValue.value, " * (40px / 9 * 16 + 4px)))"));
    });
    var isMultipleImg = VueCompositionAPI.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(imageViewer_base_ImageModalIcon["default"], {
        "attrs": {
          "icon": function icon() {
            return h(iconsVue.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__default["default"]({}, "".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 = utils_renderTnode.renderTNodeJSX(this, "navigationArrow", h(iconsVue.ChevronLeftIcon, {
        "style": utils_helper.setTransform("rotate(".concat(rotateDeg, "deg)")),
        "attrs": {
          "size": "24px"
        }
      }));
      return h(imageViewer_base_ImageModalIcon["default"], {
        "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(imageViewer_base_ImageViewerModal["default"], {
        "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")
      }, [utils_renderTnode.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
        }
      }, [utils_renderTnode.renderTNodeJSX(this, "closeBtn", h(iconsVue.CloseIcon, {
        "attrs": {
          "size": "24px"
        }
      }))]), h(imageViewer_base_ImageViewerUtils["default"], {
        "attrs": {
          "zoomInHandler": this.onZoomIn,
          "zoomOutHandler": this.onZoomOut,
          "mirrorHandler": this.onMirror,
          "resetHandler": this.onRest,
          "rotateHandler": this.onRotate,
          "scale": this.scale,
          "currentImage": this.currentImage
        }
      }), h(imageViewer_base_ImageItem["default"], {
        "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(imageViewer_base_Container["default"], {
      "ref": "containerRef",
      "attrs": {
        "mode": this.mode,
        "renderModal": this.renderModal,
        "renderViewer": this.renderViewer
      }
    }, [utils_renderTnode.renderTNodeJSX(this, "trigger", {
      params: {
        open: this.openHandler
      }
    })]);
  }
});

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