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

'use strict';

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

var helper = require('../_chunks/dep-c3e1ffac.js');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var omit = require('lodash/omit');
var isFunction = require('lodash/isFunction');
var iconsVue = require('@ldesign/icons-vue');
var _common_js_utils_observe = require('../_common/js/utils/observe.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var space_index = require('../space/index.js');
var image_props = require('./props.js');
require('../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
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-7c884501.js');
require('dayjs');
require('@babel/runtime/helpers/defineProperty');
require('vue');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../space/space.js');
require('../hooks/useConfig.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 = VueCompositionAPI.defineComponent({
  name: "TImage",
  components: {
    Space: space_index.Space
  },
  props: image_props["default"],
  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__default["default"](props2, ["className", "src", "style", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"]);
    var _useConfig = configProvider_useConfig.useConfig("image"),
      classPrefix = _useConfig.classPrefix,
      globalConfig = _useConfig.globalConfig;
    var imageRef = VueCompositionAPI.ref(null);
    var imageSrc = VueCompositionAPI.computed(function () {
      return isFunction__default["default"](globalConfig.value.replaceImageSrc) ? globalConfig.value.replaceImageSrc(props2) : props2.src;
    });
    VueCompositionAPI.watch(function () {
      return props2.src;
    }, function () {
      hasError.value = false;
      isLoaded.value = false;
    });
    var shouldLoad = VueCompositionAPI.ref(!lazy);
    var handleLoadImage = function handleLoadImage() {
      shouldLoad.value = true;
    };
    var isLoaded = VueCompositionAPI.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 = VueCompositionAPI.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 = VueCompositionAPI.ref(!hasMouseEvent);
    var handleToggleOverlay = function handleToggleOverlay() {
      if (hasMouseEvent) shouldShowOverlay.value = !shouldShowOverlay.value;
    };
    var imageClasses = VueCompositionAPI.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 = _common_js_utils_observe["default"](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 = utils_renderTnode.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 = utils_renderTnode.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__default["default"](_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.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")
    }, [utils_renderTnode.renderTNodeJSX(this, "loading") || h(space_index.Space, {
      "attrs": {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }
    }, [h(iconsVue.ImageIcon, {
      "attrs": {
        "size": "24px"
      }
    }), this.globalConfig.loadingText])]), this.hasError && h("div", {
      "class": "".concat(this.classPrefix, "-image__error")
    }, [utils_renderTnode.renderTNodeJSX(this, "error") || h(space_index.Space, {
      "attrs": {
        "direction": "vertical",
        "size": 8,
        "align": "center"
      }
    }, [h(iconsVue.ImageErrorIcon, {
      "attrs": {
        "size": "24px"
      }
    }), this.globalConfig.errorText])]), this.renderOverlay()]);
  }
});

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