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; }
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); }
import { Iconfont } from '@pisell/materials';
import classNames from 'classnames';
import React, { useMemo, useState } from 'react';
import { locales } from '@pisell/utils';
import ContactInfoModal from "../../../contactInfoModal";
import "./index.less";
var PREFIX = 'pisell-sale-detail-contact';
function normalizeContactInfo(value) {
  if (!value || Array.isArray(value) || _typeof(value) !== 'object') return null;
  if (!Object.keys(value).length) return null;
  return value;
}
export function Contact(_ref) {
  var _contact$phone, _context$sales;
  var dataSource = _ref.dataSource,
    className = _ref.className,
    context = _ref.context,
    showAction = _ref.showAction,
    showEmpty = _ref.showEmpty;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    modalVisible = _useState2[0],
    setModalVisible = _useState2[1];
  var contact = useMemo(function () {
    return normalizeContactInfo(dataSource);
  }, [dataSource]);
  if (!contact && !showEmpty) return null;
  var displayName = (contact === null || contact === void 0 ? void 0 : contact.display_name) || [contact === null || contact === void 0 ? void 0 : contact.first_name, contact === null || contact === void 0 ? void 0 : contact.last_name].filter(Boolean).join(' ');
  var phone = contact !== null && contact !== void 0 && (_contact$phone = contact.phone) !== null && _contact$phone !== void 0 && _contact$phone.phone ? "".concat(contact.phone.country_calling_code || '', " ").concat(contact.phone.phone).trim() : '';
  var isEmpty = !displayName && !phone && !(contact !== null && contact !== void 0 && contact.email);
  var canEdit = Boolean(showAction && (context === null || context === void 0 || (_context$sales = context.sales) === null || _context$sales === void 0 ? void 0 : _context$sales.setContactsInfo));
  var modalDataSource = contact ? _objectSpread(_objectSpread({}, contact), {}, {
    phone: contact.phone ? {
      phone: contact.phone.phone || '',
      country_calling_code: contact.phone.country_calling_code || '',
      country_code: contact.phone.country_code
    } : undefined
  }) : undefined;
  var handleOpenModal = function handleOpenModal() {
    if (!canEdit) return;
    setModalVisible(true);
  };
  var handleSave = function handleSave(value) {
    var _context$sales2;
    setModalVisible(false);
    context === null || context === void 0 || (_context$sales2 = context.sales) === null || _context$sales2 === void 0 || _context$sales2.setContactsInfo(value);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("section", {
    className: classNames('pisell-sale-detail-contact', className)
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__title")
  }, locales.getText('pisell2.sale-detail.contact-info')), /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "__card"), _defineProperty({}, "".concat(PREFIX, "__card--editable"), canEdit)),
    onClick: handleOpenModal
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__row")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__content-main")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__content")
  }, isEmpty ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__empty-description")
  }, locales.getText('pisell2.sale-detail.add-contact-info')) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__name-line")
  }, displayName ? /*#__PURE__*/React.createElement("span", {
    className: "".concat(PREFIX, "__name")
  }, displayName) : null, phone ? /*#__PURE__*/React.createElement("span", {
    className: "".concat(PREFIX, "__phone")
  }, "(", phone, ")") : null), contact !== null && contact !== void 0 && contact.email ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__text")
  }, contact.email) : null))), showAction ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__action")
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(PREFIX, "__action-icon")
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: isEmpty ? 'pisell2-plus-circle' : 'pisell2-edit-05'
  }))) : null))), /*#__PURE__*/React.createElement(ContactInfoModal, {
    open: modalVisible,
    dataSource: modalDataSource,
    isShowDelete: false,
    onCancel: function onCancel() {
      return setModalVisible(false);
    },
    onSave: handleSave,
    onDelete: function onDelete() {}
  }));
}