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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var omit = require('lodash/omit');
var observe = require('../../chunks/observe.js');
var components_configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_lnode = require('../../hooks/lnode.js');
var components_image_props = require('./props.js');
require('../config-provider/context.js');
require('lodash');
require('../../chunks/zh_CN.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../hooks/render-lnode.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);

var _Image = VueCompositionAPI.defineComponent({
  name: "LImage",
  components: {},
  props: components_image_props["default"],
  setup(props2) {
    const {
      lazy,
      overlayTrigger,
      onLoad,
      onError
    } = props2;
    const renderLNodeJSX = hooks_lnode.useLNodeJSX();
    const rest = omit__default["default"](props2, ["className", "src", "style", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"]);
    const {
      classPrefix
    } = components_configProvider_useConfig.useConfig();
    const imageRef = VueCompositionAPI.ref(null);
    const shouldLoad = VueCompositionAPI.ref(!lazy);
    const handleLoadImage = () => {
      shouldLoad.value = true;
    };
    const isLoaded = VueCompositionAPI.ref(false);
    const handleLoad = () => {
      isLoaded.value = true;
      onLoad === null || onLoad === void 0 ? void 0 : onLoad();
    };
    const hasError = VueCompositionAPI.ref(false);
    const handleError = () => {
      hasError.value = true;
      onError === null || onError === void 0 ? void 0 : onError();
    };
    const hasMouseEvent = overlayTrigger === "hover";
    const shouldShowOverlay = VueCompositionAPI.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.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.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()]);
  }
});

exports["default"] = _Image;
//# sourceMappingURL=image.js.map
