/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 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 components_image_props = require('./props.js');
require('../config-provider/context.js');
require('lodash');
require('../../chunks/zh_CN.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: function setup(props2) {
    var lazy = props2.lazy,
        overlayTrigger = props2.overlayTrigger,
        onLoad = props2.onLoad,
        onError = props2.onError;
    var rest = omit__default["default"](props2, ["className", "src", "style", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"]);

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

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

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

    var isLoaded = VueCompositionAPI.ref(false);

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

    var hasError = VueCompositionAPI.ref(false);

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

    var hasMouseEvent = overlayTrigger === "hover";
    var shouldShowOverlay = VueCompositionAPI.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.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.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()]);
  }
});

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