"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index$5 = require("../../../../node_modules/@vueuse/core/index.js");
const types = require("../../../utils/types.js");
const scroll = require("../../../utils/dom/scroll.js");
const index$2 = require("../../../hooks/use-attrs/index.js");
const index = require("../../../hooks/use-locale/index.js");
const index$1 = require("../../../hooks/use-namespace/index.js");
const image = require("./image.js");
const index$4 = require("../../image-viewer/index.js");
const Vue = require("vue");
const fromPairs = require("../../../../../lodash-es/fromPairs.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const index$3 = require("../../../../node_modules/@vueuse/shared/index.js");
const _hoisted_1 = [
  "src",
  "loading",
  "crossorigin"
];
const _hoisted_2 = { key: 0 };
var image_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ Vue.defineComponent({
  name: "ElImage",
  inheritAttrs: false,
  __name: "image",
  props: image.imageProps,
  emits: image.imageEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { t } = index.useLocale();
    const ns = index$1.useNamespace("image");
    const rawAttrs = Vue.useAttrs();
    const containerAttrs = Vue.computed(() => {
      return fromPairs.default(Object.entries(rawAttrs).filter(([key]) => /^(data-|on[A-Z])/i.test(key) || ["id", "style"].includes(key)));
    });
    const imgAttrs = index$2.useAttrs({
      excludeListeners: true,
      excludeKeys: Vue.computed(() => {
        return Object.keys(containerAttrs.value);
      })
    });
    const imageSrc = Vue.ref();
    const hasLoadError = Vue.ref(false);
    const isLoading = Vue.ref(true);
    const showViewer = Vue.ref(false);
    const container = Vue.ref();
    const _scrollContainer = Vue.ref();
    const supportLoading = index$3.isClient && "loading" in HTMLImageElement.prototype;
    let stopScrollListener;
    const imageKls = Vue.computed(() => [
      ns.e("inner"),
      preview.value && ns.e("preview"),
      isLoading.value && ns.is("loading")
    ]);
    const imageStyle = Vue.computed(() => {
      const { fit } = props;
      if (index$3.isClient && fit) return { objectFit: fit };
      return {};
    });
    const preview = Vue.computed(() => {
      const { previewSrcList } = props;
      return shared_esmBundler.isArray(previewSrcList) && previewSrcList.length > 0;
    });
    const imageIndex = Vue.computed(() => {
      const { previewSrcList, initialIndex } = props;
      let previewIndex = initialIndex;
      if (initialIndex > previewSrcList.length - 1) previewIndex = 0;
      return previewIndex;
    });
    const isManual = Vue.computed(() => {
      if (props.loading === "eager") return false;
      return !supportLoading && props.loading === "lazy" || props.lazy;
    });
    const loadImage = () => {
      if (!index$3.isClient) return;
      isLoading.value = true;
      hasLoadError.value = false;
      imageSrc.value = props.src;
    };
    function handleLoad(event) {
      isLoading.value = false;
      hasLoadError.value = false;
      emit("load", event);
    }
    function handleError(event) {
      isLoading.value = false;
      hasLoadError.value = true;
      emit("error", event);
    }
    function handleLazyLoad(isIntersecting) {
      if (isIntersecting) {
        loadImage();
        removeLazyLoadListener();
      }
    }
    const lazyLoadHandler = index$3.useThrottleFn(handleLazyLoad, 200, true);
    async function addLazyLoadListener() {
      if (!index$3.isClient) return;
      await Vue.nextTick();
      const { scrollContainer } = props;
      if (types.isElement(scrollContainer)) _scrollContainer.value = scrollContainer;
      else if (shared_esmBundler.isString(scrollContainer) && scrollContainer !== "") _scrollContainer.value = document.querySelector(scrollContainer) ?? void 0;
      else if (container.value) {
        const scrollContainer2 = scroll.getScrollContainer(container.value);
        _scrollContainer.value = types.isWindow(scrollContainer2) ? void 0 : scrollContainer2;
      }
      const { stop } = index$5.useIntersectionObserver(container, ([entry]) => {
        lazyLoadHandler(entry.isIntersecting);
      }, { root: _scrollContainer });
      stopScrollListener = stop;
    }
    function removeLazyLoadListener() {
      if (!index$3.isClient || !lazyLoadHandler) return;
      stopScrollListener == null ? void 0 : stopScrollListener();
      _scrollContainer.value = void 0;
      stopScrollListener = void 0;
    }
    function clickHandler() {
      if (!preview.value) return;
      showViewer.value = true;
      emit("show");
    }
    function closeViewer() {
      showViewer.value = false;
      emit("close");
    }
    function switchViewer(val) {
      emit("switch", val);
    }
    Vue.watch(() => props.src, () => {
      if (isManual.value) {
        isLoading.value = true;
        hasLoadError.value = false;
        removeLazyLoadListener();
        addLazyLoadListener();
      } else loadImage();
    });
    Vue.onMounted(() => {
      if (isManual.value) addLazyLoadListener();
      else loadImage();
    });
    __expose({ showPreview: clickHandler });
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createElementBlock("div", Vue.mergeProps({
        ref_key: "container",
        ref: container
      }, containerAttrs.value, { class: [Vue.unref(ns).b(), _ctx.$attrs.class] }), [hasLoadError.value ? Vue.renderSlot(_ctx.$slots, "error", { key: 0 }, () => [Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(ns).e("error")) }, Vue.toDisplayString(Vue.unref(t)("el.image.error")), 3)]) : (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 1 }, [imageSrc.value !== void 0 ? (Vue.openBlock(), Vue.createElementBlock("img", Vue.mergeProps({ key: 0 }, Vue.unref(imgAttrs), {
        src: imageSrc.value,
        loading: __props.loading,
        style: imageStyle.value,
        class: imageKls.value,
        crossorigin: __props.crossorigin,
        onClick: clickHandler,
        onLoad: handleLoad,
        onError: handleError
      }), null, 16, _hoisted_1)) : Vue.createCommentVNode("v-if", true), isLoading.value ? (Vue.openBlock(), Vue.createElementBlock("div", {
        key: 1,
        class: Vue.normalizeClass(Vue.unref(ns).e("wrapper"))
      }, [Vue.renderSlot(_ctx.$slots, "placeholder", {}, () => [Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(ns).e("placeholder")) }, null, 2)])], 2)) : Vue.createCommentVNode("v-if", true)], 64)), preview.value ? (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 2 }, [showViewer.value ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$4.ElImageViewer), {
        key: 0,
        "z-index": __props.zIndex,
        "initial-index": imageIndex.value,
        infinite: __props.infinite,
        "zoom-rate": __props.zoomRate,
        "min-scale": __props.minScale,
        "max-scale": __props.maxScale,
        "show-progress": __props.showProgress,
        "url-list": __props.previewSrcList,
        scale: __props.scale,
        crossorigin: __props.crossorigin,
        "hide-on-click-modal": __props.hideOnClickModal,
        teleported: __props.previewTeleported,
        "close-on-press-escape": __props.closeOnPressEscape,
        onClose: closeViewer,
        onSwitch: switchViewer
      }, Vue.createSlots({
        toolbar: Vue.withCtx((toolbar) => [Vue.renderSlot(_ctx.$slots, "toolbar", Vue.normalizeProps(Vue.guardReactiveProps(toolbar)))]),
        default: Vue.withCtx(() => [_ctx.$slots.viewer ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_2, [Vue.renderSlot(_ctx.$slots, "viewer")])) : Vue.createCommentVNode("v-if", true)]),
        _: 2
      }, [_ctx.$slots.progress ? {
        name: "progress",
        fn: Vue.withCtx((progress) => [Vue.renderSlot(_ctx.$slots, "progress", Vue.normalizeProps(Vue.guardReactiveProps(progress)))]),
        key: "0"
      } : void 0, _ctx.$slots["viewer-error"] ? {
        name: "viewer-error",
        fn: Vue.withCtx((viewerError) => [Vue.renderSlot(_ctx.$slots, "viewer-error", Vue.normalizeProps(Vue.guardReactiveProps(viewerError)))]),
        key: "1"
      } : void 0]), 1032, [
        "z-index",
        "initial-index",
        "infinite",
        "zoom-rate",
        "min-scale",
        "max-scale",
        "show-progress",
        "url-list",
        "scale",
        "crossorigin",
        "hide-on-click-modal",
        "teleported",
        "close-on-press-escape"
      ])) : Vue.createCommentVNode("v-if", true)], 64)) : Vue.createCommentVNode("v-if", true)], 16);
    };
  }
});
exports.default = image_vue_vue_type_script_setup_true_lang_default;
