var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/PisellCards/components/PisellImageCard/PisellImageCard.tsx
var PisellImageCard_exports = {};
__export(PisellImageCard_exports, {
  default: () => PisellImageCard_default
});
module.exports = __toCommonJS(PisellImageCard_exports);
var import_react = __toESM(require("react"));
var import_hooks = require("./hooks");
var import_utils = require("./utils");
var import_PisellImageCard = require("./PisellImageCard.less");
var import_utils2 = require("@pisell/utils");
var PisellImageCard = (props) => {
  const {
    dataSource = {},
    fillMode = "cover",
    lazy = false,
    active,
    disabled,
    loading: loadingState,
    children,
    onClick,
    aliWidth,
    ...restProps
  } = props;
  const { src, alt, errorSrc, fallbackSrc, text } = dataSource;
  const {
    loading: imageLoading,
    error: imageError,
    imgRef
  } = (0, import_hooks.useImageLoad)(src, lazy);
  const { mergedStyle, mergedClassName } = (0, import_hooks.usePisellImageCardStyle)(props);
  const displaySource = (0, import_react.useMemo)(() => {
    if (imageLoading && fallbackSrc) {
      return fallbackSrc;
    }
    if (imageError && errorSrc) {
      return errorSrc;
    }
    if (src) {
      return src;
    }
    return null;
  }, [imageLoading, imageError, src, errorSrc, fallbackSrc]);
  const showTextFallback = (0, import_react.useMemo)(() => {
    return alt;
  }, [displaySource, alt]);
  const truncatedText = (0, import_react.useMemo)(() => {
    if (text) {
      return text;
    }
    return (0, import_utils.getTruncatedText)(alt, 2);
  }, [alt, text]);
  const handleClick = (0, import_react.useCallback)(
    (e) => {
      if ((disabled == null ? void 0 : disabled.value) || (loadingState == null ? void 0 : loadingState.value)) {
        return;
      }
      onClick == null ? void 0 : onClick(e);
    },
    [disabled, loadingState, onClick]
  );
  const renderExtra = (0, import_react.useCallback)(() => {
    if ((loadingState == null ? void 0 : loadingState.value) && (loadingState == null ? void 0 : loadingState.extra)) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-image-card__extra" }, loadingState.extra);
    }
    if ((disabled == null ? void 0 : disabled.value) && (disabled == null ? void 0 : disabled.extra)) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-image-card__extra" }, disabled.extra);
    }
    if ((active == null ? void 0 : active.value) && (active == null ? void 0 : active.extra)) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-image-card__extra" }, active.extra);
    }
    return null;
  }, [active, disabled, loadingState]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: imgRef,
      className: mergedClassName,
      style: mergedStyle,
      onClick: handleClick,
      "data-active": active == null ? void 0 : active.value,
      "data-disabled": disabled == null ? void 0 : disabled.value,
      "data-loading": (loadingState == null ? void 0 : loadingState.value) || imageLoading,
      ...restProps
    },
    displaySource && /* @__PURE__ */ import_react.default.createElement(
      "img",
      {
        src: import_utils2.image.ali(displaySource, aliWidth),
        alt: alt || "",
        className: "pisell-image-card__image",
        style: {
          objectFit: (0, import_utils.getImageObjectFit)(fillMode)
        }
      }
    ),
    showTextFallback && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-image-card__text-fallback" }, truncatedText),
    renderExtra(),
    children
  );
};
var Provider = (props) => /* @__PURE__ */ import_react.default.createElement(PisellImageCard, { ...props == null ? void 0 : props.props, ...props });
var PisellImageCard_default = Provider;
