const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../../contactInfoModal/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/plus/saleDetail/components/Contact/index.tsx
const 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;
}
function Contact({ dataSource, className, showAction, showEmpty, sales }) {
	var _contact$phone;
	const [modalVisible, setModalVisible] = (0, react.useState)(false);
	const contact = (0, react.useMemo)(() => normalizeContactInfo(dataSource), [dataSource]);
	if (!contact && !showEmpty) return null;
	const 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(" ");
	const phone = (contact === null || contact === void 0 || (_contact$phone = contact.phone) === null || _contact$phone === void 0 ? void 0 : _contact$phone.phone) ? `${contact.phone.country_calling_code || ""} ${contact.phone.phone}`.trim() : "";
	const isEmpty = !displayName && !phone && !(contact === null || contact === void 0 ? void 0 : contact.email);
	const canEdit = Boolean(showAction && (sales === null || sales === void 0 ? void 0 : sales.setContactsInfo));
	const modalDataSource = contact ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, contact), {}, { phone: contact.phone ? {
		phone: contact.phone.phone || "",
		country_calling_code: contact.phone.country_calling_code || "",
		country_code: contact.phone.country_code
	} : void 0 }) : void 0;
	const handleOpenModal = () => {
		if (!canEdit) return;
		setModalVisible(true);
	};
	const handleSave = (value) => {
		setModalVisible(false);
		sales === null || sales === void 0 || sales.setContactsInfo(value);
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("section", { className: (0, classnames.default)("pisell-sale-detail-contact", className) }, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__title` }, _pisell_utils.locales.getText("pisell2.sale-detail.contact-info")), /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${PREFIX}__card`, { [`${PREFIX}__card--editable`]: canEdit }),
		onClick: handleOpenModal
	}, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__row` }, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__content-main` }, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__content` }, isEmpty ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__empty-description` }, _pisell_utils.locales.getText("pisell2.sale-detail.add-contact-info")) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__name-line` }, displayName ? /* @__PURE__ */ react.default.createElement("span", { className: `${PREFIX}__name` }, displayName) : null, phone ? /* @__PURE__ */ react.default.createElement("span", { className: `${PREFIX}__phone` }, "(", phone, ")") : null), (contact === null || contact === void 0 ? void 0 : contact.email) ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__text` }, contact.email) : null))), showAction ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__action` }, /* @__PURE__ */ react.default.createElement("span", { className: `${PREFIX}__action-icon` }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: isEmpty ? "pisell2-plus-circle" : "pisell2-edit-05" }))) : null))), /* @__PURE__ */ react.default.createElement(require_index.default, {
		open: modalVisible,
		dataSource: modalDataSource,
		isShowDelete: false,
		onCancel: () => setModalVisible(false),
		onSave: handleSave,
		onDelete: () => {}
	}));
}
//#endregion
exports.Contact = Contact;
