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); }
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, useEffect, useMemo, useState } from 'react';
import { EditOutlined, PlusCircleOutlined } from '@ant-design/icons';
import { locales } from '@pisell/utils';
import { PisellEntityCard } from "../pisellEntityCard";
import { ContactFormModal } from "./components";
import contactBriefLocales from "./locales";
import "./PisellContactBrief.less";

/** 初始化多语言（合并到全局 locales），确保 getText 能取到本组件文案 */
function ensureLocales() {
  try {
    var _getLocale, _getLocale2, _ref;
    var current = (_getLocale = (_getLocale2 = (_ref = locales).getLocale) === null || _getLocale2 === void 0 ? void 0 : _getLocale2.call(_ref)) !== null && _getLocale !== void 0 ? _getLocale : 'en';
    var locale = current === 'zh-TW' ? 'zh-HK' : current;
    locales.init(contactBriefLocales, locale);
  } catch (_unused) {
    // 忽略 init 异常
  }
}

/**
 * PisellContactBrief 联系人简要卡片
 * 基于 PisellEntityCard 展示姓名、电话、邮箱及扩展字段，右侧编辑图标调起编辑弹窗
 */
export function PisellContactBrief(_ref2) {
  var name = _ref2.name,
    phone = _ref2.phone,
    email = _ref2.email,
    avatar = _ref2.avatar,
    _ref2$extraData = _ref2.extraData,
    extraData = _ref2$extraData === void 0 ? {} : _ref2$extraData,
    _ref2$extraFields = _ref2.extraFields,
    extraFields = _ref2$extraFields === void 0 ? [] : _ref2$extraFields,
    onSubmit = _ref2.onSubmit,
    onDelete = _ref2.onDelete,
    _ref2$swipeToDelete = _ref2.swipeToDelete,
    swipeToDelete = _ref2$swipeToDelete === void 0 ? false : _ref2$swipeToDelete,
    _ref2$layout = _ref2.layout,
    layout = _ref2$layout === void 0 ? 'horizontal' : _ref2$layout,
    _ref2$showAvatar = _ref2.showAvatar,
    showAvatar = _ref2$showAvatar === void 0 ? true : _ref2$showAvatar,
    _ref2$showPhone = _ref2.showPhone,
    showPhone = _ref2$showPhone === void 0 ? true : _ref2$showPhone,
    _ref2$showEmail = _ref2.showEmail,
    showEmail = _ref2$showEmail === void 0 ? true : _ref2$showEmail,
    primaryLineClamp = _ref2.primaryLineClamp,
    secondaryLineClamp = _ref2.secondaryLineClamp,
    className = _ref2.className,
    style = _ref2.style,
    hoverStyle = _ref2.hoverStyle,
    activeStyle = _ref2.activeStyle,
    disabledStyle = _ref2.disabledStyle,
    _ref2$clickable = _ref2.clickable,
    clickable = _ref2$clickable === void 0 ? true : _ref2$clickable,
    _ref2$disabled = _ref2.disabled,
    disabled = _ref2$disabled === void 0 ? false : _ref2$disabled,
    onClick = _ref2.onClick;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    editModalVisible = _useState2[0],
    setEditModalVisible = _useState2[1];
  useEffect(function () {
    ensureLocales();
  }, []);
  var media = useMemo(function () {
    if (!showAvatar) return undefined;
    return {
      type: 'avatar',
      src: avatar,
      alt: name,
      size: 32,
      shape: 'circle'
    };
  }, [showAvatar, avatar, name]);
  var secondaryTexts = useMemo(function () {
    var list = [];
    if (showEmail && email) list.push({
      text: email
    });
    extraFields.forEach(function (f) {
      var v = extraData[f.key];
      if (v) list.push({
        text: v
      });
    });
    return list;
  }, [showEmail, email, extraFields, extraData]);
  var editAction = useMemo(function () {
    return {
      type: 'icon',
      icon: /*#__PURE__*/React.createElement(EditOutlined, {
        className: "pisell-contact-brief-action-edit"
      }),
      onClick: function onClick() {
        setEditModalVisible(true);
      }
    };
  }, []);
  var initialEditValues = useMemo(function () {
    return _objectSpread({
      name: name,
      phone: phone,
      email: email
    }, extraData);
  }, [name, phone, email, extraData]);
  var handleEditSubmit = useCallback(function (values) {
    onSubmit === null || onSubmit === void 0 || onSubmit(values);
    setEditModalVisible(false);
  }, [onSubmit]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-contact-brief-wrap"
  }, /*#__PURE__*/React.createElement(PisellEntityCard, {
    className: className,
    style: style,
    hoverStyle: hoverStyle,
    activeStyle: activeStyle,
    disabledStyle: disabledStyle,
    clickable: clickable,
    disabled: disabled,
    onClick: onClick,
    media: media,
    primaryText: name,
    subtitleText: showPhone && phone ? phone : undefined,
    secondaryTexts: secondaryTexts,
    showMedia: showAvatar,
    showPrimaryText: true,
    showSubtitle: showPhone,
    showSecondaryText: true,
    showTags: false,
    showActions: true,
    actions: [editAction],
    density: "B2",
    layout: layout,
    width: "content",
    swipeToDelete: swipeToDelete,
    onDelete: onDelete,
    primaryLineClamp: primaryLineClamp,
    secondaryLineClamp: secondaryLineClamp
  })), /*#__PURE__*/React.createElement(ContactFormModal, {
    visible: editModalVisible,
    onCancel: function onCancel() {
      return setEditModalVisible(false);
    },
    onSubmit: handleEditSubmit,
    mode: "edit",
    initialValues: initialEditValues,
    extraFields: extraFields
  }));
}

/**
 * 新建联系人入口按钮
 * 展示「Add Contact」+ 占位头像 + plus 图标，点击打开新建弹窗
 */
export function PisellContactBriefCreateButton(_ref3) {
  var _ref3$extraFields = _ref3.extraFields,
    extraFields = _ref3$extraFields === void 0 ? [] : _ref3$extraFields,
    onCreate = _ref3.onCreate,
    onClickProp = _ref3.onClick,
    className = _ref3.className,
    style = _ref3.style;
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    createModalVisible = _useState4[0],
    setCreateModalVisible = _useState4[1];
  useEffect(function () {
    ensureLocales();
  }, []);
  var handleClick = useCallback(function (e) {
    if (onClickProp) {
      onClickProp(e);
      return;
    }
    setCreateModalVisible(true);
  }, [onClickProp]);
  var handleCreateSubmit = useCallback(function (values) {
    onCreate === null || onCreate === void 0 || onCreate(values);
    setCreateModalVisible(false);
  }, [onCreate]);
  var addContactText = locales.getText('pisell.contact.brief.addContact');
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    role: "button",
    tabIndex: 0,
    className: "pisell-contact-brief-create-btn ".concat(className !== null && className !== void 0 ? className : '').trim(),
    style: style,
    onClick: handleClick,
    onKeyDown: function onKeyDown(e) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        handleClick(e);
      }
    }
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-contact-brief-create-btn-avatar"
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-contact-brief-create-btn-avatar-inner"
  })), /*#__PURE__*/React.createElement("span", {
    className: "pisell-contact-brief-create-btn-text"
  }, addContactText), /*#__PURE__*/React.createElement(PlusCircleOutlined, {
    className: "pisell-contact-brief-create-btn-icon"
  })), /*#__PURE__*/React.createElement(ContactFormModal, {
    visible: createModalVisible,
    onCancel: function onCancel() {
      return setCreateModalVisible(false);
    },
    onSubmit: handleCreateSubmit,
    mode: "create",
    extraFields: extraFields
  }));
}
PisellContactBrief.CreateButton = PisellContactBriefCreateButton;