import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { useGetGoalShopInfo, useRequest } from "../../hooks.js";
import React, { useState } from "react";
import { Form } from "antd";
import { locales } from "@pisell/utils";
import { FormItemInput, FormItemSelect, PisellModal, PisellToast } from "@pisell/materials";
import "./index.less";
//#region src/components/ecocup/components/AddCustomerModal/index.tsx
const AddCustomerModal = (props) => {
	const { visible, onCancel, onOk } = props;
	const [form] = Form.useForm();
	const { post } = useRequest();
	const [loading, setLoading] = useState(false);
	const { goalShopId } = useGetGoalShopInfo();
	const _onCancel = () => {
		if (loading) return;
		onCancel === null || onCancel === void 0 || onCancel();
		form.resetFields();
	};
	const _onOk = function() {
		var _ref = _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 = _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) {
					PisellToast({ content: 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.createElement(PisellModal, {
		open: visible,
		headerDivider: true,
		title: locales.getText("pisell2.text.ecocup.add-customer-modal.title"),
		onCancel: _onCancel,
		onOk: _onOk,
		okText: locales.getText("pisell2.text.ecocup.button.confirm"),
		cancelText: locales.getText("pisell2.text.ecocup.button.cancel"),
		width: 680,
		centered: true,
		destroyOnClose: true,
		wrapClassName: "pisell2-ecocup-add-customer-modal",
		okButtonProps: { loading }
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-add-customer-modal-content" }, /* @__PURE__ */ React.createElement(Form, {
		form,
		size: "large"
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-add-customer-modal-content-name-wrap" }, /* @__PURE__ */ React.createElement(FormItemInput, {
		renderMode: "edit",
		label: locales.getText("pisell2.text.ecocup.add-customer-modal.first-name"),
		placeholder: locales.getText("pisell2.text.ecocup.add-customer-modal.first-name-placeholder"),
		name: "first_name",
		requiredobj: {
			required: true,
			message: locales.getText("pisell2.text.ecocup.add-customer-modal.first-name-required")
		}
	}), /* @__PURE__ */ React.createElement(FormItemInput, {
		renderMode: "edit",
		label: locales.getText("pisell2.text.ecocup.add-customer-modal.last-name"),
		placeholder: locales.getText("pisell2.text.ecocup.add-customer-modal.last-name-placeholder"),
		name: "last_name",
		requiredobj: {
			required: true,
			message: locales.getText("pisell2.text.ecocup.add-customer-modal.last-name-required")
		}
	})), /* @__PURE__ */ React.createElement(FormItemInput.Mobile, {
		renderMode: "edit",
		name: "phone",
		label: locales.getText("pisell2.text.ecocup.add-customer-modal.phone"),
		placeholder: locales.getText("pisell2.text.ecocup.add-customer-modal.phone-placeholder"),
		typeobj: {
			type: "phone",
			enabled: true,
			message: locales.getText("pisell2.text.ecocup.add-customer-modal.phone-error")
		},
		requiredobj: {
			required: true,
			message: locales.getText("pisell2.text.ecocup.add-customer-modal.phone-required")
		},
		returnFormat: "object"
	}), /* @__PURE__ */ React.createElement(FormItemInput, {
		renderMode: "edit",
		name: "email",
		label: locales.getText("pisell2.text.ecocup.add-customer-modal.email"),
		placeholder: locales.getText("pisell2.text.ecocup.add-customer-modal.email-placeholder"),
		requiredobj: {
			required: true,
			message: locales.getText("pisell2.text.ecocup.add-customer-modal.email-required")
		}
	}), /* @__PURE__ */ React.createElement(FormItemSelect, {
		renderMode: "edit",
		name: "membership",
		label: 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
export { AddCustomerModal as default };
