import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { usePisellOS } from "../../../../hooks/useEngineContext.js";
import { useBookingTicket } from "./bookingTicket.js";
import { useActiveExecute } from "./useActiveExecute.js";
import { useEffect, useState } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/components/ticketBooking/hooks/pisellos/useCustomer.ts
const useCustomer = (props) => {
	const bookingTicket = useBookingTicket();
	usePisellOS();
	const { executeIfActive } = useActiveExecute();
	const [customers, setCustomers] = useState([]);
	const [selectedCustomer, setSelectedCustomer] = useState(null);
	const [loading, setLoading] = useState(false);
	const [error, setError] = useState(null);
	const [pagination, setPagination] = useState({
		page: 1,
		pageSize: 20,
		total: 0
	});
	const [hasMore, setHasMore] = useState(false);
	const [loadingMore, setLoadingMore] = useState(false);
	useEffect(() => {
		var _bookingTicket$store;
		if (!bookingTicket) return;
		const customerModule = bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$store = bookingTicket.store) === null || _bookingTicket$store === void 0 ? void 0 : _bookingTicket$store.customer;
		const wrappedOnCustomerListUpdate = executeIfActive((state) => {
			setCustomers(state.customerList);
			setHasMore(state.hasMore);
			setPagination((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
				total: state.total,
				page: state.currentPage,
				pageSize: state.pageSize
			}));
		});
		const wrappedOnCustomerSelected = executeIfActive((customer) => {
			var _props$changeCustomer;
			setSelectedCustomer(customer);
			props === null || props === void 0 || (_props$changeCustomer = props.changeCustomerToGlobalState) === null || _props$changeCustomer === void 0 || _props$changeCustomer.call(props, customer);
		});
		const wrappedOnPaginationChange = executeIfActive((paginationInfo) => {
			setPagination(paginationInfo);
		});
		const unlisten1 = customerModule.effectsOn("onCustomerListUpdate", wrappedOnCustomerListUpdate);
		const unlisten2 = customerModule.effectsOn("onCustomerSelected", wrappedOnCustomerSelected);
		const unlisten3 = customerModule.effectsOn("onPaginationChange", wrappedOnPaginationChange);
		return () => {
			unlisten1();
			unlisten2();
			unlisten3();
		};
	}, []);
	const loadCustomers = useMemoizedFn(function() {
		var _ref = _asyncToGenerator(function* (params) {
			try {
				setLoading(true);
				yield bookingTicket.getCustomerList(params);
			} catch (err) {
				setError(err === null || err === void 0 ? void 0 : err.message);
			}
			setLoading(false);
		});
		return function(_x) {
			return _ref.apply(this, arguments);
		};
	}());
	const initLoad = useMemoizedFn(_asyncToGenerator(function* (keyword = "", formId) {
		try {
			setLoading(true);
			yield bookingTicket.resetAndLoadCustomers({
				search: keyword || void 0,
				relation_form_id: formId || void 0
			});
		} catch (error) {
			console.error("初始化加载失败:", error);
		}
		setLoading(false);
	}));
	const loadMore = useMemoizedFn(_asyncToGenerator(function* () {
		if (!hasMore || loadingMore) return;
		setLoadingMore(true);
		setLoading(true);
		try {
			yield bookingTicket.loadMoreCustomers();
		} catch (error) {
			console.error("加载更多失败:", error);
		}
		setLoading(false);
		setLoadingMore(false);
	}));
	return {
		customers,
		selectedCustomer,
		loading,
		error,
		pagination,
		loadCustomers,
		changePage: useMemoizedFn(function() {
			var _ref2 = _asyncToGenerator(function* (page, pageSize) {
				try {
					setLoading(true);
					yield bookingTicket.changeCustomerPage(page, pageSize);
				} catch (err) {
					setError(err.message);
				}
				setLoading(false);
			});
			return function(_x2, _x3) {
				return _ref2.apply(this, arguments);
			};
		}()),
		selectCustomer: useMemoizedFn((customer) => {
			var _bookingTicket$setAct, _props$changeCustomer2;
			bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$setAct = bookingTicket.setActiveCustomer) === null || _bookingTicket$setAct === void 0 || _bookingTicket$setAct.call(bookingTicket, customer);
			props === null || props === void 0 || (_props$changeCustomer2 = props.changeCustomerToGlobalState) === null || _props$changeCustomer2 === void 0 || _props$changeCustomer2.call(props, customer);
		}),
		addCustomerToFirst: useMemoizedFn((customer) => {
			var _bookingTicket$addCus;
			bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$addCus = bookingTicket.addCustomerToFirst) === null || _bookingTicket$addCus === void 0 || _bookingTicket$addCus.call(bookingTicket, customer);
		}),
		initLoad,
		hasMore,
		loadMore
	};
};
//#endregion
export { useCustomer };
