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, { useMemo, useState } from 'react';
import { Iconfont } from '@pisell/materials';
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import Avatar from "./components/Avatar";
import { SwipeAction } from "./components/SwipeAction";
import ContactInfoModal from "../contactInfoModal";
import localeTexts from "./locales";
import "./index.less";
export var PREFIX = 'pisell-plus-contact-info';
var ContactInfo = function ContactInfo(props) {
  var _context$appHelper$ut, _props$style2;
  console.log('props>>>>>>>>>', props);
  var dataSource = props.dataSource,
    defaultName = props.defaultName,
    placeholder = props.placeholder,
    _props$direction = props.direction,
    direction = _props$direction === void 0 ? 'horizontal' : _props$direction,
    isShowDelete = props.isShowDelete,
    isShowAvatar = props.isShowAvatar,
    isShowPhone = props.isShowPhone,
    isShowEmail = props.isShowEmail,
    horizontalGap = props.horizontalGap,
    verticalGap = props.verticalGap,
    _onSave = props.onSave,
    _onDelete = props.onDelete,
    _onCancel = props.onCancel;
  var context = useEngineContext();
  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 _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    modalVisible = _useState2[0],
    setModalVisible = _useState2[1];
  var isHorizontal = useMemo(function () {
    return direction === 'horizontal';
  }, [direction]);
  var displayName = useMemo(function () {
    var _ref = dataSource || {},
      first_name = _ref.first_name,
      last_name = _ref.last_name;
    if (!first_name && !last_name) {
      return defaultName || '';
    }
    return "".concat(first_name || '', " ").concat(last_name || '');
  }, [dataSource, defaultName]);
  var displayPhone = useMemo(function () {
    var _ref2 = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.phone) || {},
      country_calling_code = _ref2.country_calling_code,
      phone = _ref2.phone;
    if (!country_calling_code || !phone) {
      return '';
    }
    // 需要对country_calling_code做兼容处理，部分数据可能没有+号前缀
    var countryCallingCode = country_calling_code !== null && country_calling_code !== void 0 && country_calling_code.startsWith('+') ? country_calling_code : "+".concat(country_calling_code);
    return "".concat(countryCallingCode || '', " ").concat(phone || '');
  }, [dataSource === null || dataSource === void 0 ? void 0 : dataSource.phone]);
  var isEmpty = useMemo(function () {
    var _dataSource$phone;
    return !(dataSource !== null && dataSource !== void 0 && dataSource.first_name) && !(dataSource !== null && dataSource !== void 0 && dataSource.last_name) && !(dataSource !== null && dataSource !== void 0 && (_dataSource$phone = dataSource.phone) !== null && _dataSource$phone !== void 0 && _dataSource$phone.phone) && !(dataSource !== null && dataSource !== void 0 && dataSource.email);
  }, [dataSource]);
  var handleClick = function handleClick() {
    console.log('handleClick');
    setModalVisible(true);
  };
  var _rightActions = useMemo(function () {
    var deleteAction = {
      key: 'delete',
      text: /*#__PURE__*/React.createElement("div", {
        style: {
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center',
          color: 'white',
          cursor: 'pointer',
          padding: '10px 0px',
          borderRadius: '8px'
        }
      }, /*#__PURE__*/React.createElement(Iconfont, {
        type: "pisell2-trash-01",
        style: {
          fontSize: 20,
          marginRight: 4
        }
      }), /*#__PURE__*/React.createElement("span", {
        style: {
          fontSize: 12,
          fontWeight: 500
        }
      }, locales.getText('pisell2.text.contact-info.actions.delete'))),
      color: 'danger',
      onClick: function onClick(e) {
        e.preventDefault();
        e.stopPropagation();
        _onDelete === null || _onDelete === void 0 || _onDelete();
      }
    };
    return [deleteAction];
  }, [dataSource, _onDelete]);
  var renderContent = function renderContent() {
    var _props$style;
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__").concat(direction),
      style: {
        gap: verticalGap,
        padding: (props === null || props === void 0 || (_props$style = props.style) === null || _props$style === void 0 ? void 0 : _props$style.padding) || '8px'
      }
    }, isHorizontal ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__horizontal-left"),
      style: {
        gap: horizontalGap
      }
    }, isShowAvatar && /*#__PURE__*/React.createElement(Avatar, {
      config: props.avatar,
      name: displayName,
      cover: ''
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__horizontal-left-content"),
      style: {
        gap: verticalGap
      }
    }, isEmpty ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__empty-description")
    }, placeholder || locales.getText('pisell2.text.contact-info.placeholder')) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__name-horizontal-container")
    }, /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__name-horizontal"),
      style: props.nameStyle
    }, displayName), isShowPhone && displayPhone && /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__phone-horizontal"),
      style: props.textStyle
    }, "(", displayPhone, ")")), isShowEmail && (dataSource === null || dataSource === void 0 ? void 0 : dataSource.email) && /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__text"),
      style: props.textStyle
    }, dataSource === null || dataSource === void 0 ? void 0 : dataSource.email)))), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__horizontal-right")
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: isEmpty ? 'pisell2-plus-circle' : 'pisell2-edit-05',
      className: "".concat(PREFIX, "__horizontal-right-icon")
    }))) : /*#__PURE__*/React.createElement(React.Fragment, null, isShowAvatar && /*#__PURE__*/React.createElement(Avatar, {
      config: props.avatar,
      name: 'hhhhh',
      cover: ''
    }), isEmpty ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__empty-description")
    }, placeholder || locales.getText('pisell2.text.contact-info.placeholder')) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__name"),
      style: props.nameStyle
    }, displayName), isShowPhone && displayPhone && /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__text"),
      style: props.textStyle
    }, displayPhone), isShowEmail && (dataSource === null || dataSource === void 0 ? void 0 : dataSource.email) && /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__text"),
      style: props.textStyle
    }, dataSource === null || dataSource === void 0 ? void 0 : dataSource.email))));
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX),
    style: _objectSpread(_objectSpread({}, props.style || {}), {}, {
      padding: 0
    }),
    onClick: handleClick
  }, isShowDelete && !isEmpty ? /*#__PURE__*/React.createElement(SwipeAction, {
    closeOnAction: false,
    rightActions: _rightActions,
    style: {
      borderRadius: (props === null || props === void 0 || (_props$style2 = props.style) === null || _props$style2 === void 0 ? void 0 : _props$style2.borderRadius) || 0
    }
  }, renderContent()) : renderContent()), /*#__PURE__*/React.createElement(ContactInfoModal, {
    open: modalVisible,
    dataSource: dataSource,
    isShowDelete: isShowDelete,
    onCancel: function onCancel() {
      setModalVisible(false);
      _onCancel === null || _onCancel === void 0 || _onCancel();
    },
    onSave: function onSave(value) {
      setModalVisible(false);
      _onSave === null || _onSave === void 0 || _onSave(value);
    },
    onDelete: function onDelete() {
      setModalVisible(false);
      _onDelete === null || _onDelete === void 0 || _onDelete();
    }
  }));
};
export default ContactInfo;