/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../_chunks/dep-29915b8e.js';
import { defineComponent, ref } from '@vue/composition-api';
import omit from 'lodash/omit';
import { o as observe } from '../../_chunks/dep-356f585c.js';
import { useConfig } from '../config-provider/useConfig.js';
import { useLNodeJSX } from '../../hooks/lnode.js';
import props from './props.js';
import '../config-provider/context.js';
import 'lodash';
import '../../_chunks/dep-def4c2ce.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../hooks/render-lnode.js';

var _Image = defineComponent({
  name: "LImage",
  components: {},
  props,
  setup(props2) {
    const {
      lazy,
      overlayTrigger,
      onLoad,
      onError
    } = props2;
    const renderLNodeJSX = useLNodeJSX();
    const rest = omit(props2, ["className", "src", "style", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"]);
    const {
      classPrefix
    } = useConfig();
    const imageRef = ref(null);
    const shouldLoad = ref(!lazy);
    const handleLoadImage = () => {
      shouldLoad.value = true;
    };
    const isLoaded = ref(false);
    const handleLoad = () => {
      isLoaded.value = true;
      onLoad === null || onLoad === void 0 ? void 0 : onLoad();
    };
    const hasError = ref(false);
    const handleError = () => {
      hasError.value = true;
      onError === null || onError === void 0 ? void 0 : onError();
    };
    const hasMouseEvent = overlayTrigger === "hover";
    const shouldShowOverlay = ref(!hasMouseEvent);
    const handleToggleOverlay = () => {
      if (hasMouseEvent) shouldShowOverlay.value = !shouldShowOverlay.value;
    };
    return {
      imageRef,
      handleLoadImage,
      classPrefix,
      hasMouseEvent,
      handleToggleOverlay,
      shouldShowOverlay,
      hasError,
      shouldLoad,
      handleError,
      handleLoad,
      isLoaded,
      rest,
      renderLNodeJSX
    };
  },
  mounted() {
    if (!this.lazy || !this.imageRef) return;
    const io = observe(this.imageRef, null, this.handleLoadImage, 0);
    this.io = io;
  },
  unmounted() {
    this.imageRef && this.io && this.io.unobserve(this.imageRef);
  },
  methods: {
    renderPlaceholder() {
      const h = this.$createElement;
      if (!this.placeholder) return null;
      return h("div", {
        "class": `${this.classPrefix}-image__placeholder`
      }, [this.placeholder]);
    },
    renderGalleryShadow() {
      const h = this.$createElement;
      if (!this.gallery) return null;
      return h("div", {
        "class": `${this.classPrefix}-image__gallery-shadow`
      });
    },
    renderOverlay() {
      const h = this.$createElement;
      const content = this.renderLNodeJSX("overlayContent");
      if (!content) return null;
      return h("div", {
        "class": [`${this.classPrefix}-image__overlay-content`, !this.shouldShowOverlay && `${this.classPrefix}-image__overlay-content--hidden`]
      }, [content]);
    },
    renderError() {
      const h = this.$createElement;
      const content = this.renderLNodeJSX("error");
      if (!content) {
        return h("div", {
          "attrs": {
            "direction": "vertical",
            "size": 8,
            "align": "center"
          }
        }, ["\u56FE\u7247\u65E0\u6CD5\u663E\u793A"]);
      }
      return content;
    }
  },
  render() {
    const h = arguments[0];
    return h("div", helper([{
      "ref": "imageRef",
      "class": [`${this.classPrefix}-image__wrapper`, `${this.classPrefix}-image__wrapper--shape-${this.shape}`, this.gallery && `${this.classPrefix}-image__wrapper--gallery`, this.hasMouseEvent && `${this.classPrefix}-image__wrapper--need-hover`, this.className],
      "on": {
        "mouseenter": this.handleToggleOverlay,
        "mouseleave": this.handleToggleOverlay
      }
    }, this.rest]), [this.renderPlaceholder(), this.renderGalleryShadow(), (this.hasError || !this.shouldLoad) && h("div", {
      "class": `${this.classPrefix}-image`
    }), !(this.hasError || !this.shouldLoad) && h("img", {
      "attrs": {
        "src": this.src,
        "alt": this.alt
      },
      "on": {
        "error": this.handleError,
        "load": this.handleLoad
      },
      "class": [`${this.classPrefix}-image`, `${this.classPrefix}-image--fit-${this.fit}`, `${this.classPrefix}-image--position-${this.position}`]
    }), !(this.hasError || !this.shouldLoad) && !this.isLoaded && h("div", {
      "class": `${this.classPrefix}-image__loading`
    }, [this.loading || h("div", {
      "attrs": {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }
    }, ["\u56FE\u7247\u52A0\u8F7D\u4E2D"])]), this.hasError && h("div", {
      "class": `${this.classPrefix}-image__error`
    }, [this.renderError()]), this.renderOverlay()]);
  }
});

export { _Image as default };
//# sourceMappingURL=image.js.map
