/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-c89aafbb.js';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { defineComponent, ref, computed, watch } from '@vue/composition-api';
import omit from 'lodash/omit';
import isFunction from 'lodash/isFunction';
import { ImageIcon, ImageErrorIcon } from '@ldesign/icons-vue';
import observe from '../_common/js/utils/observe.js';
import { useConfig } from '../config-provider/useConfig.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { Space } from '../space/index.js';
import props from './props.js';
import '../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../_chunks/dep-67ff4010.js';
import '../config-provider/type.js';
import '@babel/runtime/helpers/defineProperty';
import 'vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../space/space.js';
import '../hooks/useConfig.js';
import '../space/props.js';
import '../space/style';
import '../space/type.js';

var _Image = defineComponent({
  name: "TImage",
  components: {
    Space: Space
  },
  props: props,
  setup: function setup(props2, _ref) {
    var emit = _ref.emit;
    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("image"),
      classPrefix = _useConfig.classPrefix,
      globalConfig = _useConfig.globalConfig;
    var imageRef = ref(null);
    var imageSrc = computed(function () {
      return isFunction(globalConfig.value.replaceImageSrc) ? globalConfig.value.replaceImageSrc(props2) : props2.src;
    });
    watch(function () {
      return props2.src;
    }, function () {
      hasError.value = false;
      isLoaded.value = false;
    });
    var shouldLoad = ref(!lazy);
    var handleLoadImage = function handleLoadImage() {
      shouldLoad.value = true;
    };
    var isLoaded = ref(false);
    var handleLoad = function handleLoad(e) {
      isLoaded.value = true;
      emit("load", {
        e: e
      });
      onLoad === null || onLoad === void 0 ? void 0 : onLoad({
        e: e
      });
    };
    var hasError = ref(false);
    var handleError = function handleError(e) {
      hasError.value = true;
      emit("error", {
        e: e
      });
      onError === null || onError === void 0 ? void 0 : onError({
        e: e
      });
    };
    var hasMouseEvent = overlayTrigger === "hover";
    var shouldShowOverlay = ref(!hasMouseEvent);
    var handleToggleOverlay = function handleToggleOverlay() {
      if (hasMouseEvent) shouldShowOverlay.value = !shouldShowOverlay.value;
    };
    var imageClasses = computed(function () {
      return ["".concat(classPrefix.value, "-image"), "".concat(classPrefix.value, "-image--fit-").concat(props2.fit), "".concat(classPrefix.value, "-image--position-").concat(props2.position)];
    });
    return {
      imageRef: imageRef,
      imageClasses: imageClasses,
      handleLoadImage: handleLoadImage,
      classPrefix: classPrefix,
      globalConfig: globalConfig,
      hasMouseEvent: hasMouseEvent,
      handleToggleOverlay: handleToggleOverlay,
      shouldShowOverlay: shouldShowOverlay,
      imageSrc: imageSrc,
      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;
  },
  unmounted: function unmounted() {
    this.imageRef && this.io && this.io.unobserve(this.imageRef);
  },
  methods: {
    renderPlaceholder: function renderPlaceholder() {
      var h = this.$createElement;
      var placeholder = renderTNodeJSX(this, "placeholder");
      if (!placeholder) return;
      return h("div", {
        "class": "".concat(this.classPrefix, "-image__placeholder")
      }, [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;
      var overlay = renderTNodeJSX(this, "overlayContent");
      if (!overlay) return null;
      return h("div", {
        "class": ["".concat(this.classPrefix, "-image__overlay-content"), !this.shouldShowOverlay && "".concat(this.classPrefix, "-image__overlay-content--hidden")]
      }, [overlay]);
    },
    renderImageSrcset: function renderImageSrcset() {
      var h = this.$createElement;
      return h("picture", [Object.entries(this.srcset).map(function (_ref2) {
        var _ref3 = _slicedToArray(_ref2, 2),
          type = _ref3[0],
          url = _ref3[1];
        return h("source", {
          "attrs": {
            "type": type,
            "srcset": url
          }
        });
      }), this.src && this.renderImage(this.src)]);
    },
    renderImage: function renderImage(url) {
      var h = this.$createElement;
      return h("img", {
        "attrs": {
          "src": url,
          "alt": this.alt
        },
        "on": {
          "error": this.handleError,
          "load": this.handleLoad
        },
        "class": this.imageClasses
      });
    }
  },
  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) && (this.srcset && Object.keys(this.srcset).length ? this.renderImageSrcset() : this.renderImage(this.imageSrc)), !(this.hasError || !this.shouldLoad) && !this.isLoaded && h("div", {
      "class": "".concat(this.classPrefix, "-image__loading")
    }, [renderTNodeJSX(this, "loading") || h(Space, {
      "attrs": {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }
    }, [h(ImageIcon, {
      "attrs": {
        "size": "24px"
      }
    }), this.globalConfig.loadingText])]), this.hasError && h("div", {
      "class": "".concat(this.classPrefix, "-image__error")
    }, [renderTNodeJSX(this, "error") || h(Space, {
      "attrs": {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }
    }, [h(ImageErrorIcon, {
      "attrs": {
        "size": "24px"
      }
    }), this.globalConfig.errorText])]), this.renderOverlay()]);
  }
});

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