/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
import { defineComponent, ref } from '@vue/composition-api';
import omit from 'lodash/omit';
import { o as observe } from '../../chunks/observe.js';
import { useConfig } from '../config-provider/useConfig.js';
import props from './props.js';
import '../../chunks/_commonjsHelpers.js';
import '../config-provider/context.js';
import 'lodash';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';

var _Image = defineComponent({
  name: "LImage",
  components: {},
  props: props,
  setup: function setup(props2) {
    var lazy = props2.lazy,
        overlayTrigger = props2.overlayTrigger,
        onLoad = props2.onLoad,
        onError = props2.onError;
    var rest = omit(props2, ["className", "src", "style", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"]);

    var _useConfig = useConfig(),
        classPrefix = _useConfig.classPrefix;

    var imageRef = ref(null);
    var shouldLoad = ref(!lazy);

    var handleLoadImage = function handleLoadImage() {
      shouldLoad.value = true;
    };

    var isLoaded = ref(false);

    var handleLoad = function handleLoad() {
      isLoaded.value = true;
      onLoad === null || onLoad === void 0 ? void 0 : onLoad();
    };

    var hasError = ref(false);

    var handleError = function handleError() {
      hasError.value = true;
      onError === null || onError === void 0 ? void 0 : onError();
    };

    var hasMouseEvent = overlayTrigger === "hover";
    var shouldShowOverlay = ref(!hasMouseEvent);

    var handleToggleOverlay = function handleToggleOverlay() {
      if (hasMouseEvent) {
        shouldShowOverlay.value = !shouldShowOverlay.value;
      }
    };

    return {
      imageRef: imageRef,
      handleLoadImage: handleLoadImage,
      classPrefix: classPrefix,
      hasMouseEvent: hasMouseEvent,
      handleToggleOverlay: handleToggleOverlay,
      shouldShowOverlay: shouldShowOverlay,
      hasError: hasError,
      shouldLoad: shouldLoad,
      handleError: handleError,
      handleLoad: handleLoad,
      isLoaded: isLoaded,
      rest: rest
    };
  },
  mounted: function mounted() {
    if (!this.lazy || !this.imageRef) return;
    var io = observe(this.imageRef, null, this.handleLoadImage, 0);
    this.io = io;
  },
  destroyed: function destroyed() {
    this.imageRef && this.io && this.io.unobserve(this.imageRef);
  },
  methods: {
    renderPlaceholder: function renderPlaceholder() {
      var h = this.$createElement;

      if (!this.placeholder) {
        return null;
      }

      return h("div", {
        "class": "".concat(this.classPrefix, "-image__placeholder")
      }, [this.placeholder]);
    },
    renderGalleryShadow: function renderGalleryShadow() {
      var h = this.$createElement;
      if (!this.gallery) return null;
      return h("div", {
        "class": "".concat(this.classPrefix, "-image__gallery-shadow")
      });
    },
    renderOverlay: function renderOverlay() {
      var h = this.$createElement;
      if (!this.overlayContent) return null;
      return h("div", {
        "class": ["".concat(this.classPrefix, "-image__overlay-content"), !this.shouldShowOverlay && "".concat(this.classPrefix, "-image__overlay-content--hidden")]
      }, [this.overlayContent]);
    }
  },
  render: function render() {
    var h = arguments[0];
    return h("div", helper([{
      "ref": "imageRef",
      "class": ["".concat(this.classPrefix, "-image__wrapper"), "".concat(this.classPrefix, "-image__wrapper--shape-").concat(this.shape), this.gallery && "".concat(this.classPrefix, "-image__wrapper--gallery"), this.hasMouseEvent && "".concat(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": "".concat(this.classPrefix, "-image")
    }), !(this.hasError || !this.shouldLoad) && h("img", {
      "attrs": {
        "src": this.src,
        "alt": this.alt
      },
      "on": {
        "error": this.handleError,
        "load": this.handleLoad
      },
      "class": ["".concat(this.classPrefix, "-image"), "".concat(this.classPrefix, "-image--fit-").concat(this.fit), "".concat(this.classPrefix, "-image--position-").concat(this.position)]
    }), !(this.hasError || !this.shouldLoad) && !this.isLoaded && h("div", {
      "class": "".concat(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": "".concat(this.classPrefix, "-image__error")
    }, [this.error || h("div", {
      "attrs": {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }
    }, ["\u56FE\u7247\u65E0\u6CD5\u663E\u793A"])]), this.renderOverlay()]);
  }
});

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