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

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { defineComponent, ref, onMounted, onUnmounted, computed, watch, createVNode, mergeProps } from 'vue';
import omit from 'lodash/omit';
import isFunction from 'lodash/isFunction';
import { ImageIcon, ImageErrorIcon } from '@ldesign/icons-vue-next';
import observe from '../_common/js/utils/observe.js';
import { useConfig } from '../config-provider/useConfig.js';
import { useTNodeJSX, useTNodeDefault } from '../hooks/tnode.js';
import { Space } from '../space/index.js';
import props from './props.js';
import 'lodash/cloneDeep';
import 'lodash/isString';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
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-e041f5b0.js';
import 'dayjs';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isObject';
import '../utils/withInstall.js';
import '../space/space.js';
import '@babel/runtime/helpers/defineProperty';
import 'lodash/isNumber';
import '../hooks/useConfig.js';
import '../hooks/slot.js';
import '../space/props.js';

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(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(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.js.map
