"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _comp = require("../../ui/src/comp");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _ui = require("../../ui");
var _dom = require("../../ui/src/dom");
var _util = require("./util");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeImage',
  mixins: [_ui.globalMixins.sizeMixin],
  props: {
    src: [String, Array],
    alt: [String, Number],
    loading: String,
    title: [String, Number],
    width: [String, Number],
    height: [String, Number],
    circle: Boolean,
    zIndex: Number,
    maskClosable: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().image.maskClosable;
      }
    },
    toolbarConfig: Object,
    showPreview: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().image.showPreview;
      }
    },
    showPrintButton: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().image.showPrintButton;
      }
    },
    showDownloadButton: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().image.showDownloadButton;
      }
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().image.size || (0, _ui.getConfig)().size;
      }
    },
    getThumbnailUrlMethod: Function
  },
  data: function data() {
    var reactData = {};
    return {
      xID: _xeUtils.default.uniqueId(),
      reactData: reactData
    };
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeImgStyle: function computeImgStyle() {
      var $xeImage = this;
      var props = $xeImage;
      var width = props.width,
        height = props.height;
      var style = {};
      if (width && height) {
        style.maxWidth = (0, _dom.toCssUnit)(width);
        style.maxHeight = (0, _dom.toCssUnit)(height);
      } else {
        if (width) {
          style.width = (0, _dom.toCssUnit)(width);
        }
        if (height) {
          style.height = (0, _dom.toCssUnit)(height);
        }
      }
      return style;
    },
    computeImgList: function computeImgList() {
      var $xeImage = this;
      var props = $xeImage;
      var src = props.src;
      if (src) {
        return (_xeUtils.default.isArray(src) ? src : [src]).map(function (item) {
          if (_xeUtils.default.isString(item)) {
            return {
              url: item,
              alt: ''
            };
          }
          return {
            url: item.url,
            alt: item.alt
          };
        });
      }
      return [];
    },
    computeImgItem: function computeImgItem() {
      var $xeImage = this;
      var imgList = $xeImage.computeImgList;
      return imgList[0];
    },
    computeImgUrl: function computeImgUrl() {
      var $xeImage = this;
      var imgItem = $xeImage.computeImgItem;
      return imgItem ? "".concat(imgItem.url || '') : '';
    },
    computeImgThumbnailUrl: function computeImgThumbnailUrl() {
      var $xeImage = this;
      var props = $xeImage;
      var getThumbnailUrlFn = props.getThumbnailUrlMethod || (0, _ui.getConfig)().image.getThumbnailUrlMethod;
      var imgUrl = $xeImage.computeImgUrl;
      return getThumbnailUrlFn ? getThumbnailUrlFn({
        url: imgUrl,
        $image: $xeImage
      }) : '';
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeImage = this;
      $xeImage.$emit(type, (0, _ui.createEvent)(evnt, {
        $image: $xeImage
      }, params));
    },
    clickEvent: function clickEvent(evnt) {
      var $xeImage = this;
      var props = $xeImage;
      var $xeImageGroup = $xeImage.$xeImageGroup;
      var showPreview = props.showPreview,
        toolbarConfig = props.toolbarConfig,
        showPrintButton = props.showPrintButton,
        showDownloadButton = props.showDownloadButton,
        maskClosable = props.maskClosable,
        zIndex = props.zIndex;
      var imgList = $xeImage.computeImgList;
      var imgUrl = $xeImage.computeImgUrl;
      if ($xeImageGroup) {
        $xeImageGroup.handleClickImgEvent(evnt, {
          url: imgUrl
        });
      } else {
        if (showPreview && imgUrl) {
          (0, _util.openPreviewImage)({
            urlList: imgList,
            toolbarConfig: toolbarConfig,
            showPrintButton: showPrintButton,
            showDownloadButton: showDownloadButton,
            maskClosable: maskClosable,
            zIndex: zIndex,
            events: {
              change: function change(eventParams) {
                $xeImage.dispatchEvent('change', eventParams, eventParams.$event);
              },
              rotate: function rotate(eventParams) {
                $xeImage.dispatchEvent('rotate', eventParams, eventParams.$event);
              }
            }
          });
        }
        $xeImage.dispatchEvent('click', {
          url: imgUrl
        }, evnt);
      }
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeImage = this;
      var props = $xeImage;
      var alt = props.alt,
        loading = props.loading,
        circle = props.circle;
      var imgStyle = $xeImage.computeImgStyle;
      var imgUrl = $xeImage.computeImgUrl;
      var imgThumbnailUrl = $xeImage.computeImgThumbnailUrl;
      var vSize = $xeImage.computeSize;
      return h('img', {
        ref: 'refElem',
        class: ['vxe-image', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--circle', circle)],
        style: imgStyle,
        attrs: {
          src: imgThumbnailUrl || imgUrl,
          alt: alt,
          loading: loading
        },
        on: {
          click: $xeImage.clickEvent
        }
      });
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */