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); }
import React, { useMemo } from 'react';
import { Tag, Dropdown, Button } from 'antd';
import { PisellAvatar, Icon } from '@pisell/materials';
import { PlusCircle, User01 } from '@pisell/icon';
import classNames from 'classnames';
import LineCard from "./line";
import localeTexts from "./locales";
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import ClientName from "../clientName";
import { getAvatarText, getFieldValue, isWalkIn as isWalkInData } from "./utils";
import "./index.less";
export var processClientData = function processClientData(dataSource) {
  var fieldNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  if (!dataSource) return null;
  var fields = ['id', 'name', 'display_name', 'nickname', 'cover', 'phone', 'email', 'birthday', 'address', 'tags'];
  var _data = {};
  fields.forEach(function (key) {
    _data[key] = getFieldValue(dataSource, key, fieldNames);
  });
  return _objectSpread(_objectSpread({}, dataSource), _data);
};

// 类名前缀
export var PREFIX = 'pisell-client-card';

// 渲染头像组件
var renderAvatar = function renderAvatar(processedData) {
  var avatarConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var isShowAvatar = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var isShowStatus = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  var statusConfig = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  if (!isShowAvatar || !processedData) return null;
  var icon = avatarConfig.icon,
    _avatarConfig$useText = avatarConfig.useTextAvatar,
    useTextAvatar = _avatarConfig$useText === void 0 ? false : _avatarConfig$useText,
    children = avatarConfig.children;

  // 状态指示器
  var statusIndicator = isShowStatus && /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__status-indicator"),
    style: {
      backgroundColor: statusConfig.color || '#17B26A'
    }
  });
  if (useTextAvatar) {
    var text = children || getAvatarText(processedData.name || processedData.display_name || processedData.nickname || '');
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__avatar-wrapper")
    }, /*#__PURE__*/React.createElement(PisellAvatar, _extends({
      className: "".concat(PREFIX, "__avatar")
    }, avatarConfig), text), statusIndicator);
  }
  if (icon) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__avatar-wrapper")
    }, /*#__PURE__*/React.createElement(PisellAvatar, _extends({
      className: "".concat(PREFIX, "__avatar")
    }, avatarConfig, {
      icon: /*#__PURE__*/React.createElement(Iconfont, {
        type: icon
      })
    })), statusIndicator);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__avatar-wrapper")
  }, /*#__PURE__*/React.createElement(PisellAvatar, _extends({
    className: "".concat(PREFIX, "__avatar")
  }, avatarConfig, {
    src: processedData.cover
  })), statusIndicator);
};

// 渲染标签
var renderTags = function renderTags(processedData) {
  var isShowTag = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var tags = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var tagConfig = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  if (!isShowTag) return null;
  var tagsToShow = tags.length > 0 ? tags : (processedData === null || processedData === void 0 ? void 0 : processedData.tags) || [];
  if (!tagsToShow.length) return null;
  var _tagConfig$gap = tagConfig.gap,
    gap = _tagConfig$gap === void 0 ? 4 : _tagConfig$gap,
    _tagConfig$flexWrap = tagConfig.flexWrap,
    flexWrap = _tagConfig$flexWrap === void 0 ? 'nowrap' : _tagConfig$flexWrap;
  var isNoWrap = flexWrap === 'nowrap';
  return /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "__tags"), isNoWrap && "".concat(PREFIX, "__tags--nowrap")),
    style: {
      gap: gap,
      flexWrap: flexWrap
    }
  }, tagsToShow.map(function (tag, index) {
    var content = typeof tag === 'string' ? tag : tag.children || tag.label;
    var tagProps = {
      key: index,
      color: _typeof(tag) === 'object' ? tag.color : undefined,
      className: classNames("".concat(PREFIX, "__tag"), isNoWrap && "".concat(PREFIX, "__tag--nowrap"), !isNoWrap && "".concat(PREFIX, "__tag--wrap"))
    };
    return /*#__PURE__*/React.createElement(Tag, tagProps, content);
  }));
};

// 渲染联系信息
var renderContactInfo = function renderContactInfo(processedData) {
  var isShowPhone = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var isShowEmail = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var isShowBirthday = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  var isShowAddress = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
  var gap = arguments.length > 5 ? arguments[5] : undefined;
  var cardType = arguments.length > 6 ? arguments[6] : undefined;
  if (!processedData) return null;
  var fields = [];

  // B2 模式下，联系方式只显示一个，电话优先于邮箱
  if (cardType === 'b2') {
    if (isShowPhone && processedData.phone) {
      fields.push({
        value: processedData.phone,
        type: 'phone'
      });
    } else if (isShowEmail && processedData.email) {
      fields.push({
        value: processedData.email,
        type: 'email'
      });
    }
  } else {
    // 其他模式下，显示所有可用的联系方式
    if (isShowPhone && processedData.phone) {
      fields.push({
        value: processedData.phone,
        type: 'phone'
      });
    }
    if (isShowEmail && processedData.email) {
      fields.push({
        value: processedData.email,
        type: 'email'
      });
    }
  }
  if (isShowBirthday && processedData.birthday) {
    fields.push({
      value: processedData.birthday,
      type: 'birthday'
    });
  }
  if (isShowAddress && processedData.address) {
    fields.push({
      value: processedData.address,
      type: 'address'
    });
  }
  if (!fields.length) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__fields"),
    style: {
      gap: gap
    }
  }, fields.map(function (field, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: classNames("".concat(PREFIX, "__contact"), field.type === 'address' && "".concat(PREFIX, "__contact--address"))
    }, field.value);
  }));
};
// 渲染额外字段
var renderExtraFields = function renderExtraFields() {
  var extraFields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var gap = arguments.length > 1 ? arguments[1] : undefined;
  if (!extraFields.length) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__extra-fields"),
    style: {
      gap: gap
    }
  }, extraFields.map(function (field, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: "".concat(PREFIX, "__extra-field")
    }, field.icon && /*#__PURE__*/React.createElement(Iconfont, {
      type: field.icon,
      className: "".concat(PREFIX, "__extra-field-icon"),
      style: {
        fontSize: field.iconSize,
        color: field.iconColor
      }
    }), field.isShowLabel !== false && field.label && /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__extra-field-label")
    }, field.label, ":"), field.isShowValue !== false && field.value && /*#__PURE__*/React.createElement("span", {
      className: classNames("".concat(PREFIX, "__extra-field-value"))
    }, field.value));
  }));
};
var ClientCard = function ClientCard(_ref) {
  var _context$appHelper;
  var _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'b2' : _ref$type,
    _ref$direction = _ref.direction,
    direction = _ref$direction === void 0 ? 'horizontal' : _ref$direction,
    dataSource = _ref.dataSource,
    _ref$fieldNames = _ref.fieldNames,
    fieldNames = _ref$fieldNames === void 0 ? {} : _ref$fieldNames,
    _ref$style = _ref.style,
    customStyle = _ref$style === void 0 ? {} : _ref$style,
    horizontalGap = _ref.horizontalGap,
    verticalGap = _ref.verticalGap,
    _ref$isShowAvatar = _ref.isShowAvatar,
    isShowAvatar = _ref$isShowAvatar === void 0 ? true : _ref$isShowAvatar,
    _ref$isShowName = _ref.isShowName,
    isShowName = _ref$isShowName === void 0 ? true : _ref$isShowName,
    _ref$isShowPhone = _ref.isShowPhone,
    isShowPhone = _ref$isShowPhone === void 0 ? true : _ref$isShowPhone,
    _ref$isShowEmail = _ref.isShowEmail,
    isShowEmail = _ref$isShowEmail === void 0 ? true : _ref$isShowEmail,
    _ref$isShowBirthday = _ref.isShowBirthday,
    isShowBirthday = _ref$isShowBirthday === void 0 ? true : _ref$isShowBirthday,
    _ref$isShowAddress = _ref.isShowAddress,
    isShowAddress = _ref$isShowAddress === void 0 ? true : _ref$isShowAddress,
    _ref$isShowTag = _ref.isShowTag,
    isShowTag = _ref$isShowTag === void 0 ? true : _ref$isShowTag,
    _ref$isShowStatus = _ref.isShowStatus,
    isShowStatus = _ref$isShowStatus === void 0 ? false : _ref$isShowStatus,
    _ref$avatar = _ref.avatar,
    avatar = _ref$avatar === void 0 ? {} : _ref$avatar,
    _ref$status = _ref.status,
    status = _ref$status === void 0 ? {} : _ref$status,
    dropdown = _ref.dropdown,
    defaultName = _ref.defaultName,
    _ref$nameStyle = _ref.nameStyle,
    nameStyle = _ref$nameStyle === void 0 ? {} : _ref$nameStyle,
    _ref$tags = _ref.tags,
    tags = _ref$tags === void 0 ? [] : _ref$tags,
    _ref$tag = _ref.tag,
    tag = _ref$tag === void 0 ? {} : _ref$tag,
    _ref$extraFields = _ref.extraFields,
    extraFields = _ref$extraFields === void 0 ? [] : _ref$extraFields,
    _ref$slots = _ref.slots,
    slots = _ref$slots === void 0 ? {} : _ref$slots,
    onViewClient = _ref.onViewClient,
    onAddClient = _ref.onAddClient,
    onChangeClient = _ref.onChangeClient,
    onRemoveClient = _ref.onRemoveClient;
  var context = useEngineContext();
  locales.init(localeTexts, ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.storage) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.get('umi_locale')) || 'en');
  var processed = useMemo(function () {
    if (dataSource) {
      return processClientData(dataSource, fieldNames);
    }
    return null;
  }, [dataSource, fieldNames]);
  var translatedExtraFields = useMemo(function () {
    var _fields = extraFields.filter(function (item) {
      return !!item;
    });
    return _fields;
  }, [extraFields]);
  var _dropdownProps = useMemo(function () {
    var _props = {
      menu: {
        items: [{
          key: 'change',
          label: locales.getText('pisell2.text.change-client'),
          onClick: function onClick() {
            onChangeClient === null || onChangeClient === void 0 || onChangeClient(dataSource);
          }
        }, {
          key: 'remove',
          label: locales.getText('pisell2.text.remove-client'),
          onClick: function onClick() {
            onRemoveClient === null || onRemoveClient === void 0 || onRemoveClient(dataSource);
          }
        }]
      }
    };
    if (dropdown && 'open' in dropdown) {
      _props.open = dropdown.open;
    }
    return _props;
  }, [dropdown]);
  var isWalkIn = isWalkInData(processed === null || processed === void 0 ? void 0 : processed.id);
  if (isWalkIn) {
    var _cardStyle = _objectSpread(_objectSpread({}, customStyle), {}, {
      gap: horizontalGap
    });
    return /*#__PURE__*/React.createElement("div", {
      className: classNames(PREFIX, "".concat(PREFIX, "--empty")),
      onClick: function onClick() {
        return onAddClient === null || onAddClient === void 0 ? void 0 : onAddClient();
      },
      style: _objectSpread(_objectSpread({}, _cardStyle), {}, {
        cursor: 'pointer'
      })
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__content")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__info")
    }, isShowAvatar && /*#__PURE__*/React.createElement(PisellAvatar, {
      icon: avatar.icon ? /*#__PURE__*/React.createElement(Iconfont, {
        type: avatar.icon
      }) : /*#__PURE__*/React.createElement(User01, {
        style: {
          color: '#475467'
        }
      }),
      size: avatar.size
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__details"),
      style: {
        gap: verticalGap
      }
    }, isShowName && /*#__PURE__*/React.createElement(ClientName, {
      dataSource: defaultName || locales.getText('pisell2.text.add-client'),
      style: nameStyle
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__empty-description")
    }, locales.getText('pisell2.text.leave-empty-for-walk-in')))), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__actions")
    }, /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__plugin-icon")
    }, /*#__PURE__*/React.createElement(PlusCircle, {
      style: {
        fontSize: 28,
        color: '#475467'
      }
    })))));
  }

  // 根据类型渲染不同的卡片
  if (type === 'b1') {
    return /*#__PURE__*/React.createElement(LineCard, {
      dataSource: processed,
      isShowAvatar: isShowAvatar,
      isShowPhone: isShowPhone,
      isShowEmail: isShowEmail,
      isShowStatus: isShowStatus
      // 样式配置
      ,
      style: customStyle,
      horizontalGap: horizontalGap
      // 头像配置
      ,
      avatar: avatar,
      status: status
      // 姓名样式
      ,
      nameStyle: nameStyle
      // 标签配置
      ,
      tags: tags,
      tag: tag
      // 插槽
      ,
      slots: slots
      // 事件回调
      ,
      onViewClient: onViewClient
    });
  }

  // 通用卡片渲染逻辑 (b2, b5, b9)
  var cardStyle = _objectSpread(_objectSpread({}, customStyle), {}, {
    gap: horizontalGap
  });
  var isVertical = direction === 'vertical';
  var isInlineTag = tag.placement === 'inline';

  // 根据卡片类型确定显示的字段
  var getFieldVisibility = function getFieldVisibility() {
    switch (type) {
      case 'b2':
        return {
          showBirthday: false,
          showAddress: false,
          showExtraFields: false
        };
      case 'b5':
        return {
          showBirthday: isShowBirthday,
          showAddress: isShowAddress,
          showExtraFields: false
        };
      case 'b9':
        return {
          showBirthday: isShowBirthday,
          showAddress: isShowAddress,
          showExtraFields: true
        };
      default:
        return {
          showBirthday: isShowBirthday,
          showAddress: isShowAddress,
          showExtraFields: true
        };
    }
  };
  var _getFieldVisibility = getFieldVisibility(),
    showBirthday = _getFieldVisibility.showBirthday,
    showAddress = _getFieldVisibility.showAddress,
    showExtraFields = _getFieldVisibility.showExtraFields;
  return /*#__PURE__*/React.createElement("div", {
    className: classNames(PREFIX, "".concat(PREFIX, "--").concat(type), _defineProperty(_defineProperty({}, "".concat(PREFIX, "--vertical"), isVertical), "".concat(PREFIX, "--horizontal"), !isVertical)),
    style: _objectSpread(_objectSpread({}, cardStyle), {}, {
      cursor: 'pointer'
    }),
    onClick: function onClick() {
      return dataSource && (onViewClient === null || onViewClient === void 0 ? void 0 : onViewClient(dataSource));
    }
  }, slots.top, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__content")
  }, slots.left && /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__slot-left")
  }, slots.left), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__info")
  }, renderAvatar(processed, avatar, isShowAvatar, isShowStatus, status), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__details"),
    style: {
      gap: verticalGap
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "__name-tags"), isInlineTag && "".concat(PREFIX, "__name-tags--inline"))
  }, isShowName && /*#__PURE__*/React.createElement(ClientName, {
    dataSource: processed,
    style: nameStyle,
    className: "pisell-client-card__name "
  }), isInlineTag && renderTags(processed, isShowTag, tags, tag)), !isInlineTag && renderTags(processed, isShowTag, tags, tag), renderContactInfo(processed, isShowPhone, isShowEmail, showBirthday, showAddress, typeof verticalGap === 'number' ? verticalGap : undefined, type), showExtraFields && renderExtraFields(translatedExtraFields, typeof verticalGap === 'number' ? verticalGap : undefined))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__actions"),
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement(Dropdown, _extends({
    trigger: ['click'],
    placement: "bottomRight"
  }, _dropdownProps), /*#__PURE__*/React.createElement(Button, _extends({
    type: "text",
    className: "".concat(PREFIX, "__menu-button"),
    size: "large",
    icon: /*#__PURE__*/React.createElement(Icon, {
      type: 'pisell2-dots-vertical',
      className: "".concat(PREFIX, "__menu-button-icon")
    })
  }, (dropdown === null || dropdown === void 0 ? void 0 : dropdown.buttonProps) || {}, {
    onClick: function onClick(e) {
      var _dropdown$buttonProps, _dropdown$buttonProps2;
      e.stopPropagation(); // 阻止事件冒泡，避免触发卡片点击
      dropdown === null || dropdown === void 0 || (_dropdown$buttonProps = dropdown.buttonProps) === null || _dropdown$buttonProps === void 0 || (_dropdown$buttonProps2 = _dropdown$buttonProps.onClick) === null || _dropdown$buttonProps2 === void 0 || _dropdown$buttonProps2.call(_dropdown$buttonProps, e);
    }
  })))), slots.right && /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__slot-right")
  }, slots.right)), slots.bottom);
};
export default ClientCard;