const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_hooks = require("../../hooks.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/components/ecocup/components/AddCustomerModal/index.tsx
const AddCustomerModal = (props) => {
	const { visible, onCancel, onOk } = props;
	const [form] = antd.Form.useForm();
	const { post } = require_hooks.useRequest();
	const [loading, setLoading] = (0, react.useState)(false);
	const { goalShopId } = require_hooks.useGetGoalShopInfo();
	const _onCancel = () => {
		if (loading) return;
		onCancel === null || onCancel === void 0 || onCancel();
		form.resetFields();
	};
	const _onOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (loading) return;
			try {
				const values = yield form.validateFields();
				if (!(yield addCustomer(values))) return;
				onOk === null || onOk === void 0 || onOk(values);
				form.resetFields();
			} catch (error) {
				console.error(error);
			}
		});
		return function _onOk() {
			return _ref.apply(this, arguments);
		};
	}();
	const addCustomer = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (values) {
			const { first_name, last_name, phone, email, membership } = values;
			try {
				setLoading(true);
				const res = yield post("/shop/wallet/eco/customer", {
					goal_shop_id: goalShopId,
					customer: {
						first_name,
						last_name,
						phone: phone === null || phone === void 0 ? void 0 : phone.phone,
						country_calling_code: phone === null || phone === void 0 ? void 0 : phone.countryCallingCode,
						email,
						register_channel: "shop"
					},
					tags: [membership]
				});
				if ((res === null || res === void 0 ? void 0 : res.code) == 200) {
					(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.success") });
					return true;
				}
				return false;
			} catch (error) {
				console.error(error);
				return false;
			} finally {
				setLoading(false);
			}
		});
		return function addCustomer(_x) {
			return _ref2.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: visible,
		headerDivider: true,
		title: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.title"),
		onCancel: _onCancel,
		onOk: _onOk,
		okText: _pisell_utils.locales.getText("pisell2.text.ecocup.button.confirm"),
		cancelText: _pisell_utils.locales.getText("pisell2.text.ecocup.button.cancel"),
		width: 680,
		centered: true,
		destroyOnClose: true,
		wrapClassName: "pisell2-ecocup-add-customer-modal",
		okButtonProps: { loading }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-add-customer-modal-content" }, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		size: "large"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-add-customer-modal-content-name-wrap" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemInput, {
		renderMode: "edit",
		label: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.first-name"),
		placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.first-name-placeholder"),
		name: "first_name",
		requiredobj: {
			required: true,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.first-name-required")
		}
	}), /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemInput, {
		renderMode: "edit",
		label: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.last-name"),
		placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.last-name-placeholder"),
		name: "last_name",
		requiredobj: {
			required: true,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.last-name-required")
		}
	})), /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemInput.Mobile, {
		renderMode: "edit",
		name: "phone",
		label: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.phone"),
		placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.phone-placeholder"),
		typeobj: {
			type: "phone",
			enabled: true,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.phone-error")
		},
		requiredobj: {
			required: true,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.phone-required")
		},
		returnFormat: "object"
	}), /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemInput, {
		renderMode: "edit",
		name: "email",
		label: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.email"),
		placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.email-placeholder"),
		requiredobj: {
			required: true,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.email-required")
		}
	}), /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemSelect, {
		renderMode: "edit",
		name: "membership",
		label: _pisell_utils.locales.getText("pisell2.text.ecocup.add-customer-modal.membership"),
		defaultValue: "regular",
		options: [
			{
				label: "Regular",
				value: "regular"
			},
			{
				label: "Business",
				value: "business"
			},
			{
				label: "Admin",
				value: "admin"
			}
		]
	}))));
};
//#endregion
exports.default = AddCustomerModal;
