import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import CompensatedPisellContainer from "../../../../pro/compensatedPisellContainer/index.js";
import withResponsive from "../../../../responsive/withResponsive.js";
import { useGetGoalShopInfo, useRequest } from "../../hooks.js";
import React, { useState } from "react";
import { Form } from "antd";
import { locales } from "@pisell/utils";
import { Button as Button$1, FormItemInput, FormItemSelect, PisellToast } from "@pisell/materials";
import classNames from "classnames";
import "./index.less";
//#region src/components/ecocup/components/AddCustomerModal/index.tsx
const AddCustomerModal = (props) => {
	const { visible, onCancel, onOk, variant } = props;
	const currentVariant = variant || "desktop";
	const isPhoneVariant = currentVariant === "phone";
	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);
		};
	}();
	const renderFooter = () => /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-add-customer-modal-footer" }, /* @__PURE__ */ React.createElement(Button$1, {
		size: "large",
		onClick: _onCancel,
		disabled: loading
	}, locales.getText("pisell2.text.ecocup.button.cancel")), /* @__PURE__ */ React.createElement(Button$1, {
		size: "large",
		type: "primary",
		onClick: _onOk,
		loading
	}, locales.getText("pisell2.text.ecocup.button.confirm")));
	const title = locales.getText("pisell2.text.ecocup.add-customer-modal.title");
	const containerClassName = classNames("pisell2-ecocup-add-customer-modal", `pisell2-ecocup-add-customer-modal--${currentVariant}`);
	return /* @__PURE__ */ React.createElement(CompensatedPisellContainer, {
		renderType: isPhoneVariant ? "drawer" : "modal",
		showHandlerBar: isPhoneVariant,
		onHandlerBarClick: _onCancel,
		props: isPhoneVariant ? {
			open: visible,
			placement: "bottom",
			width: "100%",
			height: "auto",
			style: { maxHeight: "95dvh" },
			title,
			onClose: _onCancel,
			destroyOnClose: true,
			footer: renderFooter(),
			rootClassName: containerClassName,
			className: containerClassName,
			bodyStyle: { padding: "0 24px" }
		} : {
			open: visible,
			title,
			onCancel: _onCancel,
			width: 680,
			centered: true,
			destroyOnClose: true,
			footer: renderFooter(),
			className: containerClassName,
			wrapClassName: containerClassName,
			bodyStyle: {
				padding: "0 24px",
				maxHeight: "min(640px, calc(100dvh - 220px))",
				overflowY: "auto"
			}
		}
	}, /* @__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",
		innerRef: null,
		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",
		innerRef: null,
		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",
		innerRef: null,
		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",
		labelField: "label",
		valueField: "value",
		options: [
			{
				label: "Regular",
				value: "regular"
			},
			{
				label: "Business",
				value: "business"
			},
			{
				label: "Admin",
				value: "admin"
			}
		]
	}))));
};
AddCustomerModal.displayName = "AddCustomerModal";
var AddCustomerModal_default = withResponsive(AddCustomerModal);
//#endregion
export { AddCustomerModal_default as default };
