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

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { defineComponent, ref, onMounted, onUnmounted, computed, watch, createVNode, mergeProps } from 'vue';
import { o as omit_1 } from '../_chunks/dep-350764bc.mjs';
import { i as isFunction_1 } from '../_chunks/dep-5717e263.mjs';
import { ImageIcon, ImageErrorIcon } from '@ldesign/icons-vue-next';
import observe from '../_common/js/utils/observe.mjs';
import { useConfig } from '../config-provider/useConfig.mjs';
import { useTNodeJSX, useTNodeDefault } from '../hooks/tnode.mjs';
import { Space } from '../space/index.mjs';
import props from './props.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-0d3dd4b5.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-6884627f.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../utils/withInstall.mjs';
import '../space/space.mjs';
import '../_chunks/dep-2db7b3ec.mjs';
import '../_chunks/dep-3f21269a.mjs';
import '../hooks/useConfig.mjs';
import '../hooks/slot.mjs';
import '../space/props.mjs';
import '../space/style/css.mjs';
import '../space/type.mjs';

var _Image = defineComponent({
  name: "TImage",
  props: props,
  setup: function setup(props2) {
    var imageRef = ref(null);
    var io = null;
    var renderTNodeJSX = useTNodeJSX();
    onMounted(function () {
      if (!props2.lazy || !imageRef.value) return;
      var ioObserver = observe(imageRef.value, null, handleLoadImage, 0);
      io = ioObserver;
    });
    onUnmounted(function () {
      imageRef.value && io && io.unobserve(imageRef.value);
    });
    var _useConfig = useConfig("image"),
      classPrefix = _useConfig.classPrefix,
      globalConfig = _useConfig.globalConfig;
    var imageSrc = computed(function () {
      return isFunction_1(globalConfig.value.replaceImageSrc) ? globalConfig.value.replaceImageSrc(props2) : props2.src;
    });
    watch(function () {
      return props2.src;
    }, function () {
      hasError.value = false;
      isLoaded.value = false;
    });
    var shouldLoad = ref(!props2.lazy);
    var handleLoadImage = function handleLoadImage() {
      shouldLoad.value = true;
    };
    var isLoaded = ref(false);
    var handleLoad = function handleLoad(e) {
      var _props2$onLoad;
      isLoaded.value = true;
      (_props2$onLoad = props2.onLoad) === null || _props2$onLoad === void 0 ? void 0 : _props2$onLoad.call(props2, {
        e: e
      });
    };
    var hasError = ref(false);
    var handleError = function handleError(e) {
      var _props2$onError;
      hasError.value = true;
      (_props2$onError = props2.onError) === null || _props2$onError === void 0 ? void 0 : _props2$onError.call(props2, {
        e: e
      });
    };
    var hasMouseEvent = computed(function () {
      return props2.overlayTrigger === "hover";
    });
    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)];
    });
    var shouldShowOverlay = ref(!hasMouseEvent.value);
    var handleToggleOverlay = function handleToggleOverlay() {
      if (hasMouseEvent.value) shouldShowOverlay.value = !shouldShowOverlay.value;
    };
    var renderPlaceholder = function renderPlaceholder() {
      var placeholder = renderTNodeJSX("placeholder");
      if (!placeholder) return null;
      return createVNode("div", {
        "class": "".concat(classPrefix.value, "-image__placeholder")
      }, [placeholder]);
    };
    var renderGalleryShadow = function renderGalleryShadow() {
      if (!props2.gallery) return null;
      return createVNode("div", {
        "class": "".concat(classPrefix.value, "-image__gallery-shadow")
      }, null);
    };
    var renderOverlay = function renderOverlay() {
      var overlayContent = renderTNodDefault("overlayContent");
      if (!overlayContent) return null;
      return createVNode("div", {
        "class": ["".concat(classPrefix.value, "-image__overlay-content"), !shouldShowOverlay.value && "".concat(classPrefix.value, "-image__overlay-content--hidden")]
      }, [overlayContent]);
    };
    function renderImageSrcset() {
      return createVNode("picture", null, [Object.entries(props2.srcset).map(function (_ref) {
        var _ref2 = _slicedToArray(_ref, 2),
          type = _ref2[0],
          url = _ref2[1];
        return createVNode("source", {
          "type": type,
          "srcset": url
        }, null);
      }), props2.src && renderImage(props2.src)]);
    }
    function renderImage(url) {
      return createVNode("img", {
        "src": url,
        "onError": handleError,
        "onLoad": handleLoad,
        "class": imageClasses.value,
        "alt": props2.alt
      }, null);
    }
    var renderTNodDefault = useTNodeDefault();
    return function () {
      return createVNode("div", mergeProps({
        "ref": imageRef,
        "class": ["".concat(classPrefix.value, "-image__wrapper"), "".concat(classPrefix.value, "-image__wrapper--shape-").concat(props2.shape), props2.gallery && "".concat(classPrefix.value, "-image__wrapper--gallery"), hasMouseEvent.value && "".concat(classPrefix.value, "-image__wrapper--need-hover")],
        "onMouseenter": handleToggleOverlay,
        "onMouseleave": handleToggleOverlay
      }, omit_1(props2, ["src", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"])), [renderPlaceholder(), renderGalleryShadow(), (hasError.value || !shouldLoad.value) && createVNode("div", {
        "class": "".concat(classPrefix.value, "-image")
      }, null), !(hasError.value || !shouldLoad.value) && (props2.srcset && Object.keys(props2.srcset).length ? renderImageSrcset() : renderImage(imageSrc.value)), !(hasError.value || !shouldLoad.value) && !isLoaded.value && createVNode("div", {
        "class": "".concat(classPrefix.value, "-image__loading")
      }, [renderTNodeJSX("loading") || createVNode(Space, {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }, {
        "default": function _default() {
          return [createVNode(ImageIcon, {
            "size": "24px"
          }, null), globalConfig.value.loadingText];
        }
      })]), hasError.value && createVNode("div", {
        "class": "".concat(classPrefix.value, "-image__error")
      }, [renderTNodDefault("error", {
        defaultNode: createVNode(Space, {
          "direction": "vertical",
          "size": 8,
          "align": "center"
        }, {
          "default": function _default() {
            return [createVNode(ImageErrorIcon, {
              "size": "24px"
            }, null), globalConfig.value.errorText];
          }
        })
      })]), renderOverlay()]);
    };
  }
});

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