import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { PisellEntityCard } from "../pisellEntityCard/PisellEntityCard.js";
import { ContactFormModal } from "./components/ContactFormModal.js";
import locales_default from "./locales.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { locales } from "@pisell/utils";
import { EditOutlined, PlusCircleOutlined } from "@ant-design/icons";
import "./PisellContactBrief.less";
//#region src/components/PisellContactBrief/PisellContactBrief.tsx
/** 初始化多语言（合并到全局 locales），确保 getText 能取到本组件文案 */
function ensureLocales() {
	try {
		var _getLocale, _getLocale2;
		const current = (_getLocale = (_getLocale2 = locales.getLocale) === null || _getLocale2 === void 0 ? void 0 : _getLocale2.call(locales)) !== null && _getLocale !== void 0 ? _getLocale : "en";
		const locale = current === "zh-TW" ? "zh-HK" : current;
		locales.init(locales_default, locale);
	} catch (_unused) {}
}
/**
* PisellContactBrief 联系人简要卡片
* 基于 PisellEntityCard 展示姓名、电话、邮箱及扩展字段，右侧编辑图标调起编辑弹窗
*/
function PisellContactBrief({ name, phone, email, avatar, extraData = {}, extraFields = [], onSubmit, onDelete, swipeToDelete = false, layout = "horizontal", showAvatar = true, showPhone = true, showEmail = true, primaryLineClamp, secondaryLineClamp, className, style, hoverStyle, activeStyle, disabledStyle, clickable = true, disabled = false, onClick }) {
	const [editModalVisible, setEditModalVisible] = useState(false);
	useEffect(() => {
		ensureLocales();
	}, []);
	const media = useMemo(() => {
		if (!showAvatar) return void 0;
		return {
			type: "avatar",
			src: avatar,
			alt: name,
			size: 32,
			shape: "circle"
		};
	}, [
		showAvatar,
		avatar,
		name
	]);
	const secondaryTexts = useMemo(() => {
		const list = [];
		if (showEmail && email) list.push({ text: email });
		extraFields.forEach((f) => {
			const v = extraData[f.key];
			if (v) list.push({ text: v });
		});
		return list;
	}, [
		showEmail,
		email,
		extraFields,
		extraData
	]);
	const editAction = useMemo(() => ({
		type: "icon",
		icon: /* @__PURE__ */ React.createElement(EditOutlined, { className: "pisell-contact-brief-action-edit" }),
		onClick: () => {
			setEditModalVisible(true);
		}
	}), []);
	const initialEditValues = useMemo(() => _objectSpread2({
		name,
		phone,
		email
	}, extraData), [
		name,
		phone,
		email,
		extraData
	]);
	const handleEditSubmit = useCallback((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,
		style,
		hoverStyle,
		activeStyle,
		disabledStyle,
		clickable,
		disabled,
		onClick,
		media,
		primaryText: name,
		subtitleText: showPhone && phone ? phone : void 0,
		secondaryTexts,
		showMedia: showAvatar,
		showPrimaryText: true,
		showSubtitle: showPhone,
		showSecondaryText: true,
		showTags: false,
		showActions: true,
		actions: [editAction],
		density: "B2",
		layout,
		width: "content",
		swipeToDelete,
		onDelete,
		primaryLineClamp,
		secondaryLineClamp
	})), /* @__PURE__ */ React.createElement(ContactFormModal, {
		visible: editModalVisible,
		onCancel: () => setEditModalVisible(false),
		onSubmit: handleEditSubmit,
		mode: "edit",
		initialValues: initialEditValues,
		extraFields
	}));
}
/**
* 新建联系人入口按钮
* 展示「Add Contact」+ 占位头像 + plus 图标，点击打开新建弹窗
*/
function PisellContactBriefCreateButton({ extraFields = [], onCreate, onClick: onClickProp, className, style }) {
	const [createModalVisible, setCreateModalVisible] = useState(false);
	useEffect(() => {
		ensureLocales();
	}, []);
	const handleClick = useCallback((e) => {
		if (onClickProp) {
			onClickProp(e);
			return;
		}
		setCreateModalVisible(true);
	}, [onClickProp]);
	const handleCreateSubmit = useCallback((values) => {
		onCreate === null || onCreate === void 0 || onCreate(values);
		setCreateModalVisible(false);
	}, [onCreate]);
	const 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 ${className !== null && className !== void 0 ? className : ""}`.trim(),
		style,
		onClick: handleClick,
		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: () => setCreateModalVisible(false),
		onSubmit: handleCreateSubmit,
		mode: "create",
		extraFields
	}));
}
PisellContactBrief.CreateButton = PisellContactBriefCreateButton;
//#endregion
export { PisellContactBrief };
