const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_locales = require("./locales.js");
const require_ContactInfoContent = require("./ContactInfoContent.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/plus/contactInfoModal/index.tsx
const ContactInfoModalContent = (props) => {
	var _context$appHelper$ut;
	const { open, title, onCancel, onSave, onDelete, isShowDelete, dataSource, config } = props;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, ((_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");
	const [form] = antd.Form.useForm();
	(0, react.useEffect)(() => {
		form.resetFields();
		if (dataSource) {
			const { first_name, last_name, email } = dataSource || {};
			const { country_calling_code, phone, country_code } = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.phone) || {};
			let countryCallingCode = "";
			if (country_calling_code) countryCallingCode = (country_calling_code === null || country_calling_code === void 0 ? void 0 : country_calling_code.startsWith("+")) ? country_calling_code : `+${country_calling_code}`;
			form.setFieldsValue({
				first_name,
				last_name,
				phone: {
					phone,
					countryCallingCode,
					countryCode: country_code
				},
				email
			});
		} else form.resetFields();
	}, [dataSource, form]);
	const isEmpty = (0, react.useMemo)(() => {
		var _dataSource$phone;
		return !(dataSource === null || dataSource === void 0 ? void 0 : dataSource.first_name) && !(dataSource === null || dataSource === void 0 ? void 0 : dataSource.last_name) && !(dataSource === null || dataSource === void 0 || (_dataSource$phone = dataSource.phone) === null || _dataSource$phone === void 0 ? void 0 : _dataSource$phone.phone) && !(dataSource === null || dataSource === void 0 ? void 0 : dataSource.email);
	}, [dataSource]);
	const handleCancel = (e) => {
		var _e$preventDefault, _e$stopPropagation;
		e === null || e === void 0 || (_e$preventDefault = e.preventDefault) === null || _e$preventDefault === void 0 || _e$preventDefault.call(e);
		e === null || e === void 0 || (_e$stopPropagation = e.stopPropagation) === null || _e$stopPropagation === void 0 || _e$stopPropagation.call(e);
		onCancel();
	};
	const handleDelete = (e) => {
		var _e$preventDefault2, _e$stopPropagation2;
		e === null || e === void 0 || (_e$preventDefault2 = e.preventDefault) === null || _e$preventDefault2 === void 0 || _e$preventDefault2.call(e);
		e === null || e === void 0 || (_e$stopPropagation2 = e.stopPropagation) === null || _e$stopPropagation2 === void 0 || _e$stopPropagation2.call(e);
		onDelete();
	};
	const handleSave = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (e) {
			var _e$preventDefault3, _e$stopPropagation3;
			e === null || e === void 0 || (_e$preventDefault3 = e.preventDefault) === null || _e$preventDefault3 === void 0 || _e$preventDefault3.call(e);
			e === null || e === void 0 || (_e$stopPropagation3 = e.stopPropagation) === null || _e$stopPropagation3 === void 0 || _e$stopPropagation3.call(e);
			try {
				var _values$phone, _values$phone2, _values$phone3;
				const values = yield form.validateFields();
				console.log("values", values);
				onSave({
					first_name: (values === null || values === void 0 ? void 0 : values.first_name) || "",
					last_name: (values === null || values === void 0 ? void 0 : values.last_name) || "",
					display_name: (values === null || values === void 0 ? void 0 : values.first_name) || (values === null || values === void 0 ? void 0 : values.last_name) ? `${(values === null || values === void 0 ? void 0 : values.first_name) || ""} ${(values === null || values === void 0 ? void 0 : values.last_name) || ""}` : "",
					phone: {
						phone: (values === null || values === void 0 || (_values$phone = values.phone) === null || _values$phone === void 0 ? void 0 : _values$phone.phone) || "",
						country_calling_code: (values === null || values === void 0 || (_values$phone2 = values.phone) === null || _values$phone2 === void 0 ? void 0 : _values$phone2.countryCallingCode) || "",
						country_code: (values === null || values === void 0 || (_values$phone3 = values.phone) === null || _values$phone3 === void 0 ? void 0 : _values$phone3.countryCode) || ""
					},
					email: (values === null || values === void 0 ? void 0 : values.email) || ""
				});
			} catch (error) {
				console.error("handleSave failed:", error);
			}
		});
		return function handleSave(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open,
		onCancel: handleCancel,
		title: title || _pisell_utils.locales.getText("pisell2.text.contact-info.modal.title"),
		footer: /* @__PURE__ */ react.default.createElement("div", { className: "pisell-contact-info-modal-footer" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "default",
			block: true,
			onClick: handleCancel
		}, _pisell_utils.locales.getText("pisell2.text.button.cancel")), isShowDelete && !isEmpty ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "default",
			block: true,
			danger: true,
			onClick: handleDelete
		}, _pisell_utils.locales.getText("pisell2.text.button.delete")) : null, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			block: true,
			onClick: handleSave
		}, _pisell_utils.locales.getText("pisell2.text.button.save"))),
		destroyOnClose: true,
		mobileModalHeight: "auto"
	}, /* @__PURE__ */ react.default.createElement(require_ContactInfoContent.default, {
		form,
		config
	}));
};
const ContactInfoModal = (props) => {
	const { open } = props;
	return open ? /* @__PURE__ */ react.default.createElement(ContactInfoModalContent, props) : null;
};
//#endregion
exports.default = ContactInfoModal;
