function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useMemo } from 'react';
import classNames from 'classnames';
import "./index.less";
var clamp = function clamp(value, min, max) {
  return Math.max(min, Math.min(value, max));
};
var stopPropagation = function stopPropagation(e) {
  e.stopPropagation();
};
var Media = function Media(_ref) {
  var cover = _ref.cover,
    label = _ref.label,
    extra = _ref.extra,
    indicator = _ref.indicator,
    _ref$showIndicator = _ref.showIndicator,
    showIndicator = _ref$showIndicator === void 0 ? true : _ref$showIndicator,
    _ref$coverType = _ref.coverType,
    coverType = _ref$coverType === void 0 ? 'auto' : _ref$coverType,
    shape = _ref.shape,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'large' : _ref$size,
    _ref$selected = _ref.selected,
    selected = _ref$selected === void 0 ? false : _ref$selected,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    onClick = _ref.onClick,
    styles = _ref.styles,
    className = _ref.className,
    style = _ref.style;
  // 样式属性对象，用于传递给样式函数
  var styleProps = {
    selected: selected,
    disabled: disabled,
    shape: shape,
    size: size
  };

  // 计算字体大小（仅当size为数字时）
  var fontSize = useMemo(function () {
    if (typeof size === 'number') {
      return clamp(size * 0.2, 12, 48);
    }
    return undefined;
  }, [size]);

  // 计算封面样式
  var coverStyle = useMemo(function () {
    var baseStyle = {};

    // 当使用数字size时，将宽高转化为行内样式
    if (typeof size === 'number') {
      baseStyle.width = size;
      baseStyle.height = size;
      baseStyle.fontSize = fontSize;
    }
    if (!(styles !== null && styles !== void 0 && styles.cover)) return baseStyle;
    var customStyle = typeof styles.cover === 'function' ? styles.cover(styleProps) : styles.cover;
    return _objectSpread(_objectSpread({}, baseStyle), customStyle);
  }, [size, fontSize, styles === null || styles === void 0 ? void 0 : styles.cover, styleProps]);

  // 计算容器样式
  var containerStyle = useMemo(function () {
    if (!(styles !== null && styles !== void 0 && styles.container)) return style;
    var customStyle = typeof styles.container === 'function' ? styles.container(styleProps) : styles.container;
    return _objectSpread(_objectSpread({}, customStyle), style);
  }, [styles === null || styles === void 0 ? void 0 : styles.container, style, styleProps]);

  // 计算标签样式
  var labelStyle = useMemo(function () {
    var baseStyle = {};
    if (fontSize) {
      baseStyle.fontSize = fontSize;
    }
    if (!(styles !== null && styles !== void 0 && styles.label)) return baseStyle;
    var customStyle = typeof styles.label === 'function' ? styles.label(styleProps) : styles.label;
    return _objectSpread(_objectSpread({}, baseStyle), customStyle);
  }, [fontSize, styles === null || styles === void 0 ? void 0 : styles.label, styleProps]);

  // 计算extra样式
  var extraStyle = useMemo(function () {
    if (!(styles !== null && styles !== void 0 && styles.extra)) return undefined;
    return typeof styles.extra === 'function' ? styles.extra(styleProps) : styles.extra;
  }, [styles === null || styles === void 0 ? void 0 : styles.extra, styleProps]);

  // 计算指示器大小
  var indicatorSize = useMemo(function () {
    if (typeof size === 'number') {
      return Math.round(size * 0.3);
    }
    return size === 'large' ? 'middle' : 'small';
  }, [size]);

  // 计算指示器样式
  var indicatorStyle = useMemo(function () {
    if (!(styles !== null && styles !== void 0 && styles.indicator)) return undefined;
    return typeof styles.indicator === 'function' ? styles.indicator(styleProps) : styles.indicator;
  }, [styles === null || styles === void 0 ? void 0 : styles.indicator, styleProps]);

  // 获取文字内容（用于文字 cover）
  var getTextContent = function getTextContent() {
    if (typeof label === 'string' && label) {
      return label[0].toUpperCase();
    }
    return '';
  };

  // 渲染占位 logo
  var renderPlaceholderLogo = function renderPlaceholderLogo() {
    return /*#__PURE__*/React.createElement("svg", {
      className: "pisell-selector_media-placeholder",
      viewBox: "0 0 1024 1024",
      width: "60%",
      height: "60%"
    }, /*#__PURE__*/React.createElement("path", {
      d: "M512 85.333333c235.648 0 426.666667 191.018667 426.666667 426.666667s-191.018667 426.666667-426.666667 426.666667S85.333333 747.648 85.333333 512 276.352 85.333333 512 85.333333z m0 85.333334C323.477333 170.666667 170.666667 323.477333 170.666667 512s152.810667 341.333333 341.333333 341.333333 341.333333-152.810667 341.333333-341.333333S700.522667 170.666667 512 170.666667z m0 128a85.333333 85.333333 0 1 1 0 170.666666 85.333333 85.333333 0 0 1 0-170.666666z m0 256c94.293333 0 170.666667 76.373333 170.666667 170.666666a42.666667 42.666667 0 0 1-85.333334 0 85.333333 85.333333 0 0 0-170.666666 0 42.666667 42.666667 0 0 1-85.333334 0c0-94.293333 76.373333-170.666667 170.666667-170.666666z",
      fill: "currentColor"
    }));
  };

  // 渲染封面内容
  var renderCover = function renderCover() {
    // 根据 coverType 决定渲染逻辑
    if (coverType === 'image') {
      // 强制使用图片模式
      if (typeof cover === 'string' && cover) {
        return /*#__PURE__*/React.createElement("img", {
          src: cover,
          alt: typeof label === 'string' ? label : ''
        });
      }
      if ( /*#__PURE__*/React.isValidElement(cover)) {
        return cover;
      }
      // 没有图片，显示占位 logo
      return renderPlaceholderLogo();
    }
    if (coverType === 'text') {
      // 强制使用文字模式
      var _text = getTextContent();
      if (_text) {
        return /*#__PURE__*/React.createElement("span", {
          className: "pisell-selector_media-text"
        }, _text);
      }
      // 没有文字，显示占位 logo
      return renderPlaceholderLogo();
    }

    // 默认为 'auto'：自动判断
    // 优先级：图片 > 文字 > 占位 logo
    if (typeof cover === 'string' && cover) {
      return /*#__PURE__*/React.createElement("img", {
        src: cover,
        alt: typeof label === 'string' ? label : ''
      });
    }
    if ( /*#__PURE__*/React.isValidElement(cover)) {
      return cover;
    }
    var text = getTextContent();
    if (text) {
      return /*#__PURE__*/React.createElement("span", {
        className: "pisell-selector_media-text"
      }, text);
    }

    // 都没有，显示占位 logo
    return renderPlaceholderLogo();
  };

  // 渲染指示器
  var renderIndicator = function renderIndicator() {
    // 如果 showIndicator 为 false，或者没有指示器，或者未选中，则不显示
    if (!showIndicator || !indicator || !selected) return null;

    // 如果指示器是React元素，尝试克隆并注入属性
    if ( /*#__PURE__*/React.isValidElement(indicator)) {
      var clonedIndicator = /*#__PURE__*/React.cloneElement(indicator, {
        size: indicatorSize,
        variant: 'filled'
      });
      return /*#__PURE__*/React.createElement("div", {
        className: "pisell-selector_media-indicator-wrapper",
        style: indicatorStyle,
        onClick: stopPropagation
      }, clonedIndicator);
    }
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-selector_media-indicator-wrapper",
      style: indicatorStyle,
      onClick: stopPropagation
    }, indicator);
  };

  // 处理点击
  var handleClick = function handleClick(e) {
    if (disabled) return;
    onClick === null || onClick === void 0 || onClick(e);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-selector_media-item', _defineProperty(_defineProperty(_defineProperty({}, "pisell-selector_media-item-".concat(size), ['small', 'middle', 'large'].includes(size)), 'pisell-selector_media-item-disabled', disabled), 'pisell-selector_media-item-selected', selected), className),
    style: containerStyle,
    onClick: handleClick
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-selector_media-cover', "pisell-selector_media-cover-".concat(shape)),
    style: coverStyle
  }, renderCover(), renderIndicator()), label && /*#__PURE__*/React.createElement("div", {
    className: "pisell-selector_media-label",
    style: labelStyle
  }, label), extra && /*#__PURE__*/React.createElement("div", {
    className: "pisell-selector_media-extra",
    style: extraStyle,
    onClick: stopPropagation
  }, extra));
};
export default Media;