const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_amount = require("../components/footer/amount.js");
const require_utils = require("../utils.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../components/tabPane/index.js");
const require_index$1 = require("../components/emptyBooking/index.js");
const require_index$2 = require("./DepositItem/index.js");
const require_index$3 = require("./EditDepositModal/index.js");
const require_serve = require("./serve.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
//#region src/components/booking/deposit/index.tsx
const Deposit = (props) => {
	var _context$appHelper, _bookingDetail$bookin;
	const { globalState, setState } = props;
	const [loading, setLoading] = (0, react.useState)(false);
	const [modal, contextHolder] = antd.Modal.useModal();
	const editDepositModalRef = (0, react.useRef)();
	const context = require_useEngineContext.default();
	const { isTerminal, interaction, action, storage, pisellosAllModule, getData } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const { ScheduleModule } = pisellosAllModule;
	const isTeam = (0, react.useMemo)(() => {
		return storage.get("channel") === "android_team_portal" || storage.get("channel") === "ios_team_portal";
	}, []);
	require_utils.request.setRequest(context.appHelper.utils.request);
	const { bookingDetail, bookingId, amountSymbol, generatePaymentLink, locale, callback } = globalState;
	const sendEmail = (id) => {
		action({
			type: "pisell1.sendPayLinkEmail",
			data: {
				id,
				is_deposit: 1,
				deposit_payment_time: bookingDetail.deposit_payment_time,
				customer: bookingDetail.customer,
				contacts: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.contacts
			},
			callback: () => {}
		});
	};
	const create = () => {
		var _editDepositModalRef$, _editDepositModalRef$2;
		(_editDepositModalRef$ = editDepositModalRef.current) === null || _editDepositModalRef$ === void 0 || (_editDepositModalRef$2 = _editDepositModalRef$.handleOpen) === null || _editDepositModalRef$2 === void 0 || _editDepositModalRef$2.call(_editDepositModalRef$);
	};
	const deleteDeposit = () => {
		modal.confirm({
			title: _pisell_utils.locales.getText("pisell2.text.deposit-delete-message-title"),
			content: _pisell_utils.locales.getText("pisell2.text.deposit-delete-message"),
			onOk() {
				require_serve.editDepositAmount(bookingDetail.id, { deposit_amount: 0 }).then((res) => {
					setState({ bookingDetail: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bookingDetail), {}, {
						deposit_amount: 0,
						is_deposit: 0
					}) });
					callback === null || callback === void 0 || callback({
						order_id: bookingId,
						afterCloseUpdate: true
					});
				});
			}
		});
	};
	const getDataFromHost = () => {
		try {
			const is_price_include_tax = getData("is_price_include_tax");
			const tax_rate = getData("tax_rate") || "10";
			const tax_title = getData("tax_title") || "GST";
			const surcharge_list = getData("surcharge_list");
			const schedule_list = getData("schedule_list");
			let scheduleById = {};
			if (Array.isArray(schedule_list)) for (let item of schedule_list) scheduleById[item.id] = item;
			return {
				is_price_include_tax,
				tax_rate,
				tax_title,
				surcharge_list,
				schedule_list,
				scheduleById
			};
		} catch (error) {
			console.error(error);
			return {};
		}
	};
	const total = (0, react.useMemo)(() => {
		return require_amount.getSumTotal(globalState, require_objectSpread2._objectSpread2({
			isEdit: false,
			ScheduleModule
		}, getDataFromHost()));
	}, [globalState]);
	const depositList = (0, react.useMemo)(() => {
		if (!bookingId) return [];
		if ((bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.is_deposit) === 0) return [];
		return [{
			customer: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.customer,
			status: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_payment_status,
			totalAmount: total.expectAmount,
			depositAmount: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_amount,
			depositUnPaidAmount: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_unpaid_amount,
			depositOriginPaidAmount: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_origin_paid_amount,
			depositPaidAmount: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_paid_amount,
			createdTime: (0, dayjs.default)(bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.created_at).format(locale === "en" ? "dddd, D MMMM YYYY HH:mm" : "YYYY-MM-DD HH:mm"),
			orderId: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.id,
			orderNumber: bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.shop_full_order_number,
			amountSymbol: (globalState === null || globalState === void 0 ? void 0 : globalState.amountSymbol) || "$",
			totalAmountStr: (0, _pisell_utils.formatAmount)(total.expectAmount, 2, (globalState === null || globalState === void 0 ? void 0 : globalState.amountSymbol) || "$")
		}];
	}, [
		bookingDetail,
		total === null || total === void 0 ? void 0 : total.expectAmount,
		globalState === null || globalState === void 0 ? void 0 : globalState.amountSymbol,
		bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_amount
	]);
	const isBookingCancelled = (bookingDetail === null || bookingDetail === void 0 || (_bookingDetail$bookin = bookingDetail.bookings) === null || _bookingDetail$bookin === void 0 ? void 0 : _bookingDetail$bookin.appointment_status) === "cancelled";
	const isPaid = (bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.deposit_payment_status) === "paid";
	const footer = (0, react.useMemo)(() => {
		if (!bookingId) return null;
		if (isBookingCancelled) return null;
		if (isPaid) return null;
		return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__booking-deposit-footer" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
			size: "large",
			block: true,
			onClick: depositList.length > 0 ? deleteDeposit : create
		}, depositList.length > 0 ? _pisell_utils.locales.getText("pisell2.text.deposit-delete") : _pisell_utils.locales.getText("pisell2.text.add")));
	}, [
		depositList,
		isBookingCancelled,
		isPaid
	]);
	const handleEdit = (e, item) => {
		var _editDepositModalRef$3, _editDepositModalRef$4;
		(_editDepositModalRef$3 = editDepositModalRef.current) === null || _editDepositModalRef$3 === void 0 || (_editDepositModalRef$4 = _editDepositModalRef$3.handleOpen) === null || _editDepositModalRef$4 === void 0 || _editDepositModalRef$4.call(_editDepositModalRef$3, item.depositAmount);
	};
	const handleChange = (amount) => {
		require_serve.editDepositAmount(bookingDetail.id, { deposit_amount: amount }).then((res) => {
			setState({ bookingDetail: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bookingDetail), {}, {
				deposit_amount: amount,
				is_deposit: 1
			}) });
			callback === null || callback === void 0 || callback({
				order_id: bookingId,
				afterCloseUpdate: true
			});
		});
	};
	const handleGoOrderDetail = () => {
		if (isTerminal === null || isTerminal === void 0 ? void 0 : isTerminal()) {
			var _interaction$utils, _interaction$utils$po;
			return interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 ? void 0 : _interaction$utils$po.call(_interaction$utils, {
				module: "booking",
				key: "edit_order",
				data: { order_id: bookingDetail.id }
			});
		}
		globalState.action({
			type: "pisell1.goOrderDetail",
			data: { "[unique_order_id]": bookingDetail.id }
		});
	};
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		loading,
		header: _pisell_utils.locales.getText("pisell2.text.deposit-list"),
		footer
	}, !bookingId || !depositList.length ? /* @__PURE__ */ react.default.createElement(require_index$1.default, { description: !bookingId ? void 0 : _pisell_utils.locales.getText("pisell2.text.deposit-list-empty") }) : depositList.map((item) => /* @__PURE__ */ react.default.createElement(require_index$2.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ key: item.status }, item), {}, {
		generatePaymentLink,
		sendEmail,
		handleGoOrderDetail,
		onEdit: (e) => handleEdit(e, item),
		isPaid,
		isBookingCancelled,
		hideOrderDetail: isTeam
	}))), /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		totalAmount: total.expectAmount,
		ref: editDepositModalRef,
		amountSymbol,
		onChange: handleChange
	}), contextHolder);
};
//#endregion
exports.default = Deposit;
