require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_bookingTicket = require("./bookingTicket.js");
const require_useActiveExecute = require("./useActiveExecute.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/components/ticketBooking/hooks/pisellos/useCustomer.ts
const useCustomer = (props) => {
	const bookingTicket = require_bookingTicket.useBookingTicket();
	require_useEngineContext.usePisellOS();
	const { executeIfActive } = require_useActiveExecute.useActiveExecute();
	const [customers, setCustomers] = (0, react.useState)([]);
	const [selectedCustomer, setSelectedCustomer] = (0, react.useState)(null);
	const [loading, setLoading] = (0, react.useState)(false);
	const [error, setError] = (0, react.useState)(null);
	const [pagination, setPagination] = (0, react.useState)({
		page: 1,
		pageSize: 20,
		total: 0
	});
	const [hasMore, setHasMore] = (0, react.useState)(false);
	const [loadingMore, setLoadingMore] = (0, react.useState)(false);
	(0, react.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) => require_objectSpread2._objectSpread2(require_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 = (0, ahooks.useMemoizedFn)(function() {
		var _ref = require_asyncToGenerator._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 = (0, ahooks.useMemoizedFn)(require_asyncToGenerator._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 = (0, ahooks.useMemoizedFn)(require_asyncToGenerator._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: (0, ahooks.useMemoizedFn)(function() {
			var _ref2 = require_asyncToGenerator._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: (0, ahooks.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: (0, ahooks.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
exports.useCustomer = useCustomer;
