const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_PisellSingleLineText = require("../../pisellSingleLineText/PisellSingleLineText.js");
const require_PisellPhone = require("../../pisellPhone/PisellPhone.js");
const require_PisellEmail = require("../../pisellEmail/PisellEmail.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
//#region src/components/PisellContactBrief/components/ContactFormModal.tsx
const FIRSTNAME_FIELD = "firstname";
const LASTNAME_FIELD = "lastname";
const PHONE_FIELD = "phone";
const EMAIL_FIELD = "email";
/** 将 name 拆分为 firstname + lastname（按首个空格） */
function splitName(name) {
	if (!name || !name.trim()) return ["", ""];
	const idx = name.trim().indexOf(" ");
	if (idx <= 0) return [name.trim(), ""];
	return [name.trim().slice(0, idx), name.trim().slice(idx + 1)];
}
/** 将 firstname + lastname 合并为 name */
function joinName(first, last) {
	return [first, last].map((s) => s === null || s === void 0 ? void 0 : s.trim()).filter(Boolean).join(" ");
}
/**
* 联系人新建/编辑弹窗
* 表单含 Firstname、Lastname、Phone、Email + extraFields，与 Figma 新建/编辑弹窗一致
*/
function ContactFormModal({ visible, onCancel, onSubmit, mode, initialValues, extraFields = [] }) {
	const [form] = antd.Form.useForm();
	(0, react.useEffect)(() => {
		if (visible) {
			var _initialValues$name, _initialValues$phone, _initialValues$email;
			const [firstname, lastname] = splitName(initialValues === null || initialValues === void 0 ? void 0 : initialValues.name);
			const values = {
				firstname,
				lastname,
				name: (_initialValues$name = initialValues === null || initialValues === void 0 ? void 0 : initialValues.name) !== null && _initialValues$name !== void 0 ? _initialValues$name : "",
				phone: (_initialValues$phone = initialValues === null || initialValues === void 0 ? void 0 : initialValues.phone) !== null && _initialValues$phone !== void 0 ? _initialValues$phone : "",
				email: (_initialValues$email = initialValues === null || initialValues === void 0 ? void 0 : initialValues.email) !== null && _initialValues$email !== void 0 ? _initialValues$email : ""
			};
			if (initialValues && extraFields.length > 0) extraFields.forEach((f) => {
				var _initialValues$f$key;
				values[f.key] = (_initialValues$f$key = initialValues[f.key]) !== null && _initialValues$f$key !== void 0 ? _initialValues$f$key : "";
			});
			form.setFieldsValue(values);
		}
	}, [
		visible,
		initialValues,
		extraFields,
		form
	]);
	const handleOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				var _raw$firstname, _raw$lastname;
				const raw = yield form.validateFields();
				const values = {
					name: joinName((_raw$firstname = raw.firstname) !== null && _raw$firstname !== void 0 ? _raw$firstname : "", (_raw$lastname = raw.lastname) !== null && _raw$lastname !== void 0 ? _raw$lastname : ""),
					phone: raw.phone,
					email: raw.email
				};
				extraFields.forEach((f) => {
					values[f.key] = raw[f.key];
				});
				onSubmit(values);
				form.resetFields();
				onCancel();
			} catch (_unused) {}
		});
		return function handleOk() {
			return _ref.apply(this, arguments);
		};
	}();
	const handleCancel = () => {
		form.resetFields();
		onCancel();
	};
	const title = mode === "create" ? _pisell_utils.locales.getText("pisell.contact.brief.modal.title.create") : _pisell_utils.locales.getText("pisell.contact.brief.modal.title.edit");
	const okText = _pisell_utils.locales.getText("pisell.contact.brief.modal.save");
	const cancelText = _pisell_utils.locales.getText("pisell.contact.brief.modal.cancel");
	return /* @__PURE__ */ react.default.createElement(antd.Modal, {
		title,
		open: visible,
		onOk: handleOk,
		onCancel: handleCancel,
		okText,
		cancelText,
		destroyOnClose: true,
		width: 480,
		className: "pisell-contact-brief-modal"
	}, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		preserve: false,
		className: "pisell-contact-brief-modal-form"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-contact-brief-modal-form-row" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: FIRSTNAME_FIELD,
		label: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.firstname"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.required")
		}],
		className: "pisell-contact-brief-modal-form-item-half"
	}, /* @__PURE__ */ react.default.createElement(require_PisellSingleLineText.default, {
		placeholder: "",
		mode: "edit"
	})), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: LASTNAME_FIELD,
		label: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.lastname"),
		className: "pisell-contact-brief-modal-form-item-half"
	}, /* @__PURE__ */ react.default.createElement(require_PisellSingleLineText.default, {
		placeholder: "",
		mode: "edit"
	}))), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: PHONE_FIELD,
		label: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.phone")
	}, /* @__PURE__ */ react.default.createElement(require_PisellPhone.default, {
		placeholder: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.phone.placeholder"),
		mode: "edit"
	})), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: EMAIL_FIELD,
		label: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.email")
	}, /* @__PURE__ */ react.default.createElement(require_PisellEmail.default, {
		placeholder: _pisell_utils.locales.getText("pisell.contact.brief.modal.field.email.placeholder"),
		mode: "edit"
	})), extraFields.map((f) => /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		key: f.key,
		name: f.key,
		label: f.label
	}, /* @__PURE__ */ react.default.createElement(require_PisellSingleLineText.default, {
		placeholder: "",
		mode: "edit"
	})))));
}
//#endregion
exports.ContactFormModal = ContactFormModal;
