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

'use strict';

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

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var vue = require('vue');
var omit = require('lodash/omit');
var isFunction = require('lodash/isFunction');
var iconsVueNext = require('@ldesign/icons-vue-next');
var _common_js_utils_observe = require('../_common/js/utils/observe.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_tnode = require('../hooks/tnode.js');
var space_index = require('../space/index.js');
var image_props = require('./props.js');
require('lodash/cloneDeep');
require('lodash/isString');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7f0559c3.js');
require('dayjs');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../utils/withInstall.js');
require('../space/space.js');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isNumber');
require('../hooks/useConfig.js');
require('../hooks/slot.js');
require('../space/props.js');

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

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

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

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