var _excluded = ["dataSource", "fillMode", "lazy", "active", "disabled", "loading", "children", "onClick", "aliWidth"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useMemo, useCallback } from 'react';
import { useImageLoad, usePisellImageCardStyle } from "./hooks";
import { getImageObjectFit, getTruncatedText } from "./utils";
import "./PisellImageCard.less";
import { image as imageAli } from '@pisell/utils';

/**
 * PisellImageCard 图片卡片组件
 * 用于展示图片，支持占位图、失败兜底、文字降级、懒加载等功能
 *
 * @component
 * @example
 * ```tsx
 * // 基础用法
 * <PisellImageCard
 *   dataSource={{
 *     src: 'https://example.com/image.png',
 *     alt: '商品图片'
 *   }}
 * />
 *
 * // 带占位图和失败兜底
 * <PisellImageCard
 *   dataSource={{
 *     src: 'https://example.com/image.png',
 *     alt: '商品图片',
 *     fallbackSrc: 'https://example.com/placeholder.png',
 *     errorSrc: 'https://example.com/error.png'
 *   }}
 * />
 *
 * // 自定义尺寸和填充模式
 * <PisellImageCard
 *   dataSource={{ src: 'https://example.com/image.png' }}
 *   width={200}
 *   height={200}
 *   fillMode="contain"
 * />
 *
 * // 启用懒加载
 * <PisellImageCard
 *   dataSource={{ src: 'https://example.com/image.png' }}
 *   lazy
 * />
 *
 * // 激活状态
 * <PisellImageCard
 *   dataSource={{ src: 'https://example.com/image.png' }}
 *   active={{ value: true }}
 * />
 * ```
 */
var PisellImageCard = function PisellImageCard(props) {
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? {} : _props$dataSource,
    _props$fillMode = props.fillMode,
    fillMode = _props$fillMode === void 0 ? 'cover' : _props$fillMode,
    _props$lazy = props.lazy,
    lazy = _props$lazy === void 0 ? false : _props$lazy,
    active = props.active,
    disabled = props.disabled,
    loadingState = props.loading,
    children = props.children,
    onClick = props.onClick,
    aliWidth = props.aliWidth,
    restProps = _objectWithoutProperties(props, _excluded);
  var src = dataSource.src,
    alt = dataSource.alt,
    errorSrc = dataSource.errorSrc,
    fallbackSrc = dataSource.fallbackSrc,
    text = dataSource.text;

  // 使用图片加载 Hook
  var _useImageLoad = useImageLoad(src, lazy),
    imageLoading = _useImageLoad.loading,
    imageError = _useImageLoad.error,
    imgRef = _useImageLoad.imgRef;

  // 使用样式合并 Hook
  var _usePisellImageCardSt = usePisellImageCardStyle(props),
    mergedStyle = _usePisellImageCardSt.mergedStyle,
    mergedClassName = _usePisellImageCardSt.mergedClassName;

  /**
   * 获取当前应该显示的图片地址
   * 优先级：loading(fallbackSrc) > error(errorSrc) > src
   */
  var displaySource = useMemo(function () {
    if (imageLoading && fallbackSrc) {
      return fallbackSrc;
    }
    if (imageError && errorSrc) {
      return errorSrc;
    }
    if (src) {
      return src;
    }
    return null;
  }, [imageLoading, imageError, src, errorSrc, fallbackSrc]);

  /**
   * 是否显示文字降级
   * 当没有任何图片可显示时，显示 alt 的前 2 个字符
   */
  var showTextFallback = useMemo(function () {
    return alt;
  }, [displaySource, alt]);
  /**
   * 截取后的文字
   */
  var truncatedText = useMemo(function () {
    if (text) {
      return text;
    }
    return getTruncatedText(alt, 2);
  }, [alt, text]);

  /**
   * 处理点击事件
   */
  var handleClick = useCallback(function (e) {
    // 如果禁用或加载中，不触发点击事件
    if (disabled !== null && disabled !== void 0 && disabled.value || loadingState !== null && loadingState !== void 0 && loadingState.value) {
      return;
    }
    onClick === null || onClick === void 0 || onClick(e);
  }, [disabled, loadingState, onClick]);

  /**
   * 渲染状态的 extra 内容
   * 优先级：loading > disabled > active
   */
  var renderExtra = useCallback(function () {
    if (loadingState !== null && loadingState !== void 0 && loadingState.value && loadingState !== null && loadingState !== void 0 && loadingState.extra) {
      return /*#__PURE__*/React.createElement("div", {
        className: "pisell-image-card__extra"
      }, loadingState.extra);
    }
    if (disabled !== null && disabled !== void 0 && disabled.value && disabled !== null && disabled !== void 0 && disabled.extra) {
      return /*#__PURE__*/React.createElement("div", {
        className: "pisell-image-card__extra"
      }, disabled.extra);
    }
    if (active !== null && active !== void 0 && active.value && active !== null && active !== void 0 && active.extra) {
      return /*#__PURE__*/React.createElement("div", {
        className: "pisell-image-card__extra"
      }, active.extra);
    }
    return null;
  }, [active, disabled, loadingState]);
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: imgRef,
    className: mergedClassName,
    style: mergedStyle,
    onClick: handleClick,
    "data-active": active === null || active === void 0 ? void 0 : active.value,
    "data-disabled": disabled === null || disabled === void 0 ? void 0 : disabled.value,
    "data-loading": (loadingState === null || loadingState === void 0 ? void 0 : loadingState.value) || imageLoading
  }, restProps), displaySource && /*#__PURE__*/React.createElement("img", {
    src: imageAli.ali(displaySource, aliWidth),
    alt: alt || '',
    className: "pisell-image-card__image",
    style: {
      objectFit: getImageObjectFit(fillMode)
    }
  }), showTextFallback && /*#__PURE__*/React.createElement("div", {
    className: "pisell-image-card__text-fallback"
  }, truncatedText), renderExtra(), children);
};

/**
 * Provider 包装器
 * 兼容 PisellCards 系统的 props 传递方式
 */
var Provider = function Provider(props) {
  return /*#__PURE__*/React.createElement(PisellImageCard, _extends({}, props === null || props === void 0 ? void 0 : props.props, props));
};
export default Provider;