"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
require("../../ui/index.js");
const XEUtils = require("xe-utils");
const dom = require("../../ui/src/dom.js");
const util = require("./util.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
function createInternalData() {
  return {
    // dgTime: 0,
    // mdTime: 0
  };
}
function createReactData() {
  return {};
}
const VxeImageComponent = comp.defineVxeComponent({
  name: "VxeImage",
  props: {
    src: [String, Array],
    alt: [String, Number],
    loading: String,
    title: [String, Number],
    width: [String, Number],
    height: [String, Number],
    circle: Boolean,
    draggable: {
      type: Boolean,
      default: () => config.getConfig().image.draggable
    },
    zIndex: Number,
    maskClosable: {
      type: Boolean,
      default: () => config.getConfig().image.maskClosable
    },
    toolbarConfig: Object,
    showPreview: {
      type: Boolean,
      default: () => config.getConfig().image.showPreview
    },
    showPrintButton: {
      type: Boolean,
      default: () => config.getConfig().image.showPrintButton
    },
    showDownloadButton: {
      type: Boolean,
      default: () => config.getConfig().image.showDownloadButton
    },
    size: {
      type: String,
      default: () => config.getConfig().image.size || config.getConfig().size
    },
    getThumbnailUrlMethod: Function
  },
  emits: [
    "click",
    "change",
    "rotate",
    "dragstart",
    "drag",
    "dragend"
  ],
  setup(props, context) {
    const { emit } = context;
    const xID = XEUtils.uniqueId();
    const $xeImageGroup = vue.inject("$xeImageGroup", null);
    const refElem = vue.ref();
    const { computeSize } = useFns.useSize(props);
    const internalData = createInternalData();
    const reactData = vue.reactive(createReactData());
    const refMaps = {
      refElem
    };
    const computeWrapperStyle = vue.computed(() => {
      const { width, height, circle } = props;
      const stys = {};
      if (width) {
        stys.width = dom.toCssUnit(width);
      } else {
        if (circle) {
          if (height) {
            stys.width = dom.toCssUnit(height);
          }
        }
      }
      if (height) {
        stys.height = dom.toCssUnit(height);
      } else {
        if (circle) {
          if (width) {
            stys.height = dom.toCssUnit(width);
          }
        }
      }
      return stys;
    });
    const computeImgStyle = vue.computed(() => {
      const { width, height } = props;
      const stys = {};
      if (width && height) {
        stys.maxWidth = dom.toCssUnit(width);
        stys.maxHeight = dom.toCssUnit(height);
      } else {
        if (width) {
          stys.width = dom.toCssUnit(width);
        }
        if (height) {
          stys.height = dom.toCssUnit(height);
        }
      }
      return stys;
    });
    const computeImgList = vue.computed(() => {
      const { src } = props;
      if (src) {
        return (XEUtils.isArray(src) ? src : [src]).map((item) => {
          if (XEUtils.isString(item)) {
            return {
              url: item,
              alt: ""
            };
          }
          return {
            url: item.url,
            alt: item.alt
          };
        });
      }
      return [];
    });
    const computeImgItem = vue.computed(() => {
      const imgList = computeImgList.value;
      return imgList[0];
    });
    const computeImgUrl = vue.computed(() => {
      const imgItem = computeImgItem.value;
      return imgItem ? `${imgItem.url || ""}` : "";
    });
    const computeImgThumbnailUrl = vue.computed(() => {
      const getThumbnailUrlFn = props.getThumbnailUrlMethod || config.getConfig().image.getThumbnailUrlMethod;
      const imgUrl = computeImgUrl.value;
      return getThumbnailUrlFn ? getThumbnailUrlFn({ url: imgUrl, $image: $xeImage }) : "";
    });
    const computeMaps = {
      computeSize
    };
    const $xeImage = {
      xID,
      props,
      context,
      reactData,
      internalData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $image: $xeImage }, params));
    };
    const imageMethods = {
      dispatchEvent
    };
    const clickEvent = (evnt) => {
      const { showPreview, toolbarConfig, showPrintButton, showDownloadButton, maskClosable, zIndex } = props;
      const { mdTime, dgTime } = internalData;
      if (mdTime && dgTime && dgTime > mdTime) {
        return;
      }
      const imgList = computeImgList.value;
      const imgUrl = computeImgUrl.value;
      if ($xeImageGroup) {
        $xeImageGroup.handleClickImgEvent(evnt, { url: imgUrl });
      } else {
        if (showPreview && imgUrl) {
          util.openPreviewImage({
            urlList: imgList,
            toolbarConfig,
            showPrintButton,
            showDownloadButton,
            maskClosable,
            zIndex,
            events: {
              change(eventParams) {
                dispatchEvent("change", eventParams, eventParams.$event);
              },
              rotate(eventParams) {
                dispatchEvent("rotate", eventParams, eventParams.$event);
              }
            }
          });
        }
        dispatchEvent("click", { url: imgUrl }, evnt);
      }
    };
    const mousedownEvent = () => {
      internalData.mdTime = Date.now();
    };
    const handleDragstartEvent = (evnt) => {
      internalData.dgTime = Date.now();
      dispatchEvent("dragstart", {}, evnt);
    };
    const handleDragEvent = (evnt) => {
      dispatchEvent(evnt.type, {}, evnt);
    };
    const imagePrivateMethods = {};
    Object.assign($xeImage, imageMethods, imagePrivateMethods);
    const renderVN = () => {
      const { alt, loading, circle, draggable } = props;
      const wrapperStyle = computeWrapperStyle.value;
      const imgStyle = computeImgStyle.value;
      const imgUrl = computeImgUrl.value;
      const imgThumbnailUrl = computeImgThumbnailUrl.value;
      const vSize = computeSize.value;
      let imgDrag;
      if (!XEUtils.eqNull(draggable)) {
        imgDrag = !!(draggable === true || draggable === "true");
      }
      return vue.h("div", {
        class: ["vxe-image", {
          [`size--${vSize}`]: vSize,
          "is--circle": circle
        }],
        style: wrapperStyle
      }, [
        vue.h("img", {
          ref: refElem,
          class: "vxe-image-img",
          src: imgThumbnailUrl || imgUrl,
          alt,
          loading,
          draggable: imgDrag,
          style: imgStyle,
          onMousedown: mousedownEvent,
          onClick: clickEvent,
          onDragstart: handleDragstartEvent,
          onDrag: handleDragEvent,
          onDragend: handleDragEvent
        })
      ]);
    };
    vue.onBeforeUnmount(() => {
      XEUtils.assign(reactData, createReactData());
      XEUtils.assign(internalData, createInternalData());
    });
    $xeImage.renderVN = renderVN;
    return $xeImage;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxeImageComponent;
