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 _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 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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useCallback, useMemo, useState } from 'react';
import CardWrapper from "../CardWrapper";
import Cover from "../Cover";
import { useDynamicComponents } from "../../hooks/useDynamicComponents";
import { PREFIX } from "../../status";
import { locales } from '@pisell/utils';
import localeTexts from "../../locales";
import useEngineContext from "../../../../../../hooks/useEngineContext";
import Action from "../Action";
import CardMoreText from "../CardMoreText";
import WarningLists from "../WarningLists";
import "./index.less";
var CardCom = function CardCom(_ref) {
  var _context$appHelper$ut, _dataSource$sortKeys3;
  var props = _ref.props,
    className = _ref.className;
  var context = useEngineContext();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isFold = _useState2[0],
    setIsFold = _useState2[1];
  locales.init(localeTexts, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get('umi_locale')) || 'en');
  var skuCardConfig = props.skuCardConfig,
    skuImageConfig = props.skuImageConfig,
    dataSource = props.dataSource,
    skuActionConfig = props.skuActionConfig,
    skuTextConfig = props.skuTextConfig;
  var _useDynamicComponents = useDynamicComponents(),
    renderComponents = _useDynamicComponents.renderComponents;
  var _ref2 = skuCardConfig || {},
    layoutDirection = _ref2.layoutDirection,
    displayMode = _ref2.displayMode,
    isFoldable = _ref2.isFoldable;
  var _ref3 = skuImageConfig || {},
    isCover = _ref3.isCover,
    height = _ref3.height;
  var _ref4 = skuActionConfig || {},
    method = _ref4.method;
  var _ref5 = (skuTextConfig === null || skuTextConfig === void 0 ? void 0 : skuTextConfig.infoConfig) || {},
    paddingY = _ref5.paddingY,
    justifyContent = _ref5.justifyContent,
    alignItems = _ref5.alignItems,
    slot = _ref5.slot;

  /**
   * @title: 文本区域样式
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-09 13:38
   */
  var contentStyle = useMemo(function () {
    var isMinHeight = isCover && layoutDirection === 'row' && displayMode !== 'display_a1';
    return {
      minHeight: isMinHeight ? "".concat(height) : 'auto',
      padding: "".concat(paddingY, "px 0"),
      alignItems: layoutDirection === 'column' ? alignItems : 'initial',
      justifyContent: layoutDirection === 'row' ? justifyContent : 'initial'
    };
  }, [paddingY, justifyContent, alignItems, isCover, height, layoutDirection, displayMode]);

  /**
   * @title: 操作区域
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-11 18:21
   */
  var actionCom = useCallback(function (key) {
    // a1模式不显示操作区域
    if (displayMode === 'display_a1') {
      return false;
    }

    // 纵向布局
    if (key === 'column') {
      if (layoutDirection === 'column' && method === 'button') {
        return /*#__PURE__*/React.createElement(Action, {
          props: props,
          className: "".concat(PREFIX, "__card-action-custom")
        });
      }
      return false;
    }

    // 横向布局
    if (key === 'row') {
      return (
        /*#__PURE__*/
        // 为了占位
        React.createElement("div", {
          style: {
            opacity: layoutDirection === 'column' ? 0 : 1
          }
        }, /*#__PURE__*/React.createElement(Action, {
          props: props
        }))
      );
    }
    return false;
  }, [props]);

  /**
   * @title: 图片区域
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-11 18:11
   */
  var coverCom = useMemo(function () {
    // 开启图片且不是a1模式才显示图片
    if (isCover && displayMode !== 'display_a1') {
      return /*#__PURE__*/React.createElement(Cover, {
        props: props
      });
    }
    return null;
  }, [isCover, displayMode, props]);

  /**
   * @title: 展开收起
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-12 13:38
   */
  var isShowMoreBtn = useMemo(function () {
    var _dataSource$sortKeys, _dataSource$sortKeys2;
    if (dataSource !== null && dataSource !== void 0 && (_dataSource$sortKeys = dataSource.sortKeys) !== null && _dataSource$sortKeys !== void 0 && _dataSource$sortKeys.length && (dataSource === null || dataSource === void 0 || (_dataSource$sortKeys2 = dataSource.sortKeys) === null || _dataSource$sortKeys2 === void 0 ? void 0 : _dataSource$sortKeys2.length) < 4) return false;
    if (displayMode !== 'display_a9' || !isFoldable) return false;
    return true;
  }, [dataSource === null || dataSource === void 0 || (_dataSource$sortKeys3 = dataSource.sortKeys) === null || _dataSource$sortKeys3 === void 0 ? void 0 : _dataSource$sortKeys3.length, displayMode, isFoldable]);

  /**
   * @title: 格式化排序key
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-12 16:06
   */
  var formatSortKeys = useMemo(function () {
    var sortKeys = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.sortKeys) || [];

    // A1模式，只显示title和price
    if (displayMode === 'display_a1') {
      var a1SortKeys = ['title', 'price'];
      return sortKeys.filter(function (key) {
        return a1SortKeys.includes(key);
      });
    }

    // A2模式，显示前3个
    if (displayMode === 'display_a2') {
      return sortKeys.slice(0, 3);
    }

    // A5模式，显示前5个
    if (displayMode === 'display_a5') {
      return sortKeys.slice(0, 5);
    }

    // A9模式，开启展开收起
    if (isShowMoreBtn) {
      return isFold ? sortKeys.slice(0, 4) : sortKeys;
    }
    return sortKeys;
  }, [dataSource === null || dataSource === void 0 ? void 0 : dataSource.sortKeys, isShowMoreBtn, displayMode, isFold]);

  /**
   * @title: 内容区域
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-11 18:11
   */
  var contentCom = useMemo(function () {
    // 内容插槽
    if (slot) {
      return slot;
    }
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__card-right-content ").concat(className, "-content "),
      style: contentStyle
    }, renderComponents(_objectSpread(_objectSpread({}, props), {}, {
      sortKeys: formatSortKeys
    })), /*#__PURE__*/React.createElement(WarningLists, {
      lists: dataSource === null || dataSource === void 0 ? void 0 : dataSource.warning_lists
    }), isShowMoreBtn ? /*#__PURE__*/React.createElement(CardMoreText, _extends({}, props, {
      onChange: setIsFold,
      isFold: isFold
    })) : null);
  }, [slot, contentStyle, className, props, formatSortKeys, isShowMoreBtn, isFold]);

  /**
   * @title: 卡片插槽
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-09-12 16:49
   */
  var cardSlot = useMemo(function () {
    if (skuCardConfig !== null && skuCardConfig !== void 0 && skuCardConfig.slot) {
      return skuCardConfig === null || skuCardConfig === void 0 ? void 0 : skuCardConfig.slot;
    }
    if (displayMode === 'display_custom') {
      return {
        type: 'JSSlot',
        value: []
      };
    }
    return null;
  }, [skuCardConfig === null || skuCardConfig === void 0 ? void 0 : skuCardConfig.slot, displayMode]);
  return /*#__PURE__*/React.createElement(CardWrapper, props, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__card ").concat(PREFIX, "__card-").concat(layoutDirection, " ").concat(className)
  }, cardSlot ? cardSlot : /*#__PURE__*/React.createElement(React.Fragment, null, coverCom, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__card-right")
  }, contentCom, actionCom('row')), actionCom('column'))));
};
export default CardCom;