"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index = require("../icon/index.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const format = require("../utils/format.js");
const basic = require("../utils/basic.js");
const [name, bem] = create.createNamespace("image");
const imageProps = {
  src: String,
  alt: String,
  fit: String,
  position: String,
  round: Boolean,
  block: Boolean,
  width: props.numericProp,
  height: props.numericProp,
  radius: props.numericProp,
  lazyLoad: Boolean,
  iconSize: props.numericProp,
  showError: props.truthProp,
  errorIcon: props.makeStringProp("photo-fail"),
  iconPrefix: String,
  showLoading: props.truthProp,
  loadingIcon: props.makeStringProp("photo"),
  crossorigin: String,
  referrerpolicy: String,
  decoding: String
};
var stdin_default = Vue.defineComponent({
  name,
  props: imageProps,
  emits: ["load", "error"],
  setup(props2, {
    emit,
    slots
  }) {
    const error = Vue.ref(false);
    const loading = Vue.ref(true);
    const imageRef = Vue.ref();
    const {
      $Lazyload
    } = Vue.getCurrentInstance().proxy;
    const style = Vue.computed(() => {
      const style2 = {
        width: format.addUnit(props2.width),
        height: format.addUnit(props2.height)
      };
      if (basic.isDef(props2.radius)) {
        style2.overflow = "hidden";
        style2.borderRadius = format.addUnit(props2.radius);
      }
      return style2;
    });
    Vue.watch(() => props2.src, () => {
      error.value = false;
      loading.value = true;
    });
    const onLoad = (event) => {
      if (loading.value) {
        loading.value = false;
        emit("load", event);
      }
    };
    const triggerLoad = () => {
      const loadEvent = new Event("load");
      Object.defineProperty(loadEvent, "target", {
        value: imageRef.value,
        enumerable: true
      });
      onLoad(loadEvent);
    };
    const onError = (event) => {
      error.value = true;
      loading.value = false;
      emit("error", event);
    };
    const renderIcon = (name2, className, slot) => {
      if (slot) {
        return slot();
      }
      return Vue.createVNode(index.Icon, {
        "name": name2,
        "size": props2.iconSize,
        "class": className,
        "classPrefix": props2.iconPrefix
      }, null);
    };
    const renderPlaceholder = () => {
      if (loading.value && props2.showLoading) {
        return Vue.createVNode("div", {
          "class": bem("loading")
        }, [renderIcon(props2.loadingIcon, bem("loading-icon"), slots.loading)]);
      }
      if (error.value && props2.showError) {
        return Vue.createVNode("div", {
          "class": bem("error")
        }, [renderIcon(props2.errorIcon, bem("error-icon"), slots.error)]);
      }
    };
    const renderImage = () => {
      if (error.value || !props2.src) {
        return;
      }
      const attrs = {
        alt: props2.alt,
        class: bem("img"),
        decoding: props2.decoding,
        style: {
          objectFit: props2.fit,
          objectPosition: props2.position
        },
        crossorigin: props2.crossorigin,
        referrerpolicy: props2.referrerpolicy
      };
      if (props2.lazyLoad) {
        return Vue.withDirectives(Vue.createVNode("img", Vue.mergeProps({
          "ref": imageRef
        }, attrs), null), [[Vue.resolveDirective("lazy"), props2.src]]);
      }
      return Vue.createVNode("img", Vue.mergeProps({
        "ref": imageRef,
        "src": props2.src,
        "onLoad": onLoad,
        "onError": onError
      }, attrs), null);
    };
    const onLazyLoaded = ({
      el
    }) => {
      const check = () => {
        if (el === imageRef.value && loading.value) {
          triggerLoad();
        }
      };
      if (imageRef.value) {
        check();
      } else {
        Vue.nextTick(check);
      }
    };
    const onLazyLoadError = ({
      el
    }) => {
      if (el === imageRef.value && !error.value) {
        onError();
      }
    };
    if ($Lazyload && basic.inBrowser) {
      $Lazyload.$on("loaded", onLazyLoaded);
      $Lazyload.$on("error", onLazyLoadError);
      Vue.onBeforeUnmount(() => {
        $Lazyload.$off("loaded", onLazyLoaded);
        $Lazyload.$off("error", onLazyLoadError);
      });
    }
    Vue.onMounted(() => {
      Vue.nextTick(() => {
        var _a;
        if (((_a = imageRef.value) == null ? void 0 : _a.complete) && !props2.lazyLoad) {
          triggerLoad();
        }
      });
    });
    return () => {
      var _a;
      return Vue.createVNode("div", {
        "class": bem({
          round: props2.round,
          block: props2.block
        }),
        "style": style.value
      }, [renderImage(), renderPlaceholder(), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
    };
  }
});
exports.default = stdin_default;
exports.imageProps = imageProps;
