const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_booking = require("../config/booking.js");
const require_serve = require("../../pisellSalesGrid/serve.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
let ahooks = require("ahooks");
//#region src/plus/pisellSalesManagement/hooks/useSalesGridData.ts
function getDefaultSearchParams(perspective) {
	if (perspective === "monitor") return { filter: { values: {
		order_payment_status: require_booking.defaultPaymentStatusKeys,
		booking_time_start_between: [(0, dayjs.default)().startOf("day"), (0, dayjs.default)().endOf("day")]
	} } };
	return {
		filter: { values: {
			payment_status: require_booking.defaultPaymentStatusKeys,
			orderDate: [(0, dayjs.default)(), (0, dayjs.default)()]
		} },
		sort: "created_at:desc"
	};
}
/**
* Sales Management 内使用的列表数据 hook：统一发起订单/预订列表请求，
* 管理分页、筛选、多选状态，并返回传给 PisellSalesGrid 的受控 props，
* 使 Sales Grid 成为通用展示组件。
*/
function useSalesGridData(params) {
	var _getBookingList$data$, _getBookingList$data, _getOrderList$data$li, _getOrderList$data, _getBookingList$data$2, _getBookingList$data2, _getOrderList$data$co, _getOrderList$data2;
	const { perspective, extParams } = params;
	const [pagination, setPagination] = (0, react.useState)({
		pageNumber: 1,
		pageSize: 50
	});
	const [searchParams, setSearchParams] = (0, react.useState)(() => getDefaultSearchParams(perspective));
	const [selectedKeys, setSelectedKeys] = (0, react.useState)([]);
	const [selectedRows, setSelectedRows] = (0, react.useState)([]);
	const requestParams = (0, react.useMemo)(() => {
		const { keyword, filter, sort } = searchParams;
		const filterVals = (filter === null || filter === void 0 ? void 0 : filter.values) || {};
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
			keyword,
			sort
		}, filterVals), {}, {
			skip: pagination.pageNumber,
			num: pagination.pageSize
		});
	}, [
		searchParams,
		pagination.pageNumber,
		pagination.pageSize
	]);
	const _getOrderList = (0, ahooks.useRequest)(require_serve.getOrderList, { manual: true });
	const _getBookingList = (0, ahooks.useRequest)(require_serve.getBookingList, { manual: true });
	const runListParams = (0, react.useMemo)(() => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, requestParams), (extParams === null || extParams === void 0 ? void 0 : extParams.list) || {}), [requestParams, extParams === null || extParams === void 0 ? void 0 : extParams.list]);
	(0, react.useEffect)(() => {
		if (perspective === "monitor") _getBookingList.run(runListParams);
		else _getOrderList.run(runListParams);
	}, [perspective, runListParams]);
	(0, react.useEffect)(() => {
		setSearchParams(getDefaultSearchParams(perspective));
		setPagination((p) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, p), {}, { pageNumber: 1 }));
		setSelectedKeys([]);
		setSelectedRows([]);
	}, [perspective]);
	const onPageChange = (0, react.useCallback)((pageNumber, pageSize) => {
		if (pageNumber < 1 || pageSize < 1) return;
		setPagination({
			pageNumber,
			pageSize
		});
	}, []);
	const onSearch = (0, react.useCallback)((params) => {
		setSearchParams(params || {});
		setPagination((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { pageNumber: 1 }));
	}, []);
	const onReset = (0, react.useCallback)(() => {
		setSearchParams(getDefaultSearchParams(perspective));
		setPagination((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { pageNumber: 1 }));
	}, [perspective]);
	const onSelectionChange = (0, react.useCallback)((keys, rows) => {
		setSelectedKeys(keys);
		setSelectedRows(rows);
	}, []);
	const onRefresh = (0, react.useCallback)(() => {
		if (perspective === "monitor") _getBookingList.run(runListParams);
		else _getOrderList.run(runListParams);
	}, [
		perspective,
		runListParams,
		_getBookingList,
		_getOrderList
	]);
	const refreshAsync = (0, react.useCallback)(() => {
		if (perspective === "monitor") return _getBookingList.runAsync(runListParams);
		return _getOrderList.runAsync(runListParams);
	}, [
		perspective,
		runListParams,
		_getBookingList,
		_getOrderList
	]);
	return {
		data: perspective === "monitor" ? (_getBookingList$data$ = (_getBookingList$data = _getBookingList.data) === null || _getBookingList$data === void 0 ? void 0 : _getBookingList$data.list) !== null && _getBookingList$data$ !== void 0 ? _getBookingList$data$ : [] : (_getOrderList$data$li = (_getOrderList$data = _getOrderList.data) === null || _getOrderList$data === void 0 ? void 0 : _getOrderList$data.list) !== null && _getOrderList$data$li !== void 0 ? _getOrderList$data$li : [],
		loading: perspective === "monitor" ? _getBookingList.loading : _getOrderList.loading,
		total: perspective === "monitor" ? (_getBookingList$data$2 = (_getBookingList$data2 = _getBookingList.data) === null || _getBookingList$data2 === void 0 ? void 0 : _getBookingList$data2.count) !== null && _getBookingList$data$2 !== void 0 ? _getBookingList$data$2 : 0 : (_getOrderList$data$co = (_getOrderList$data2 = _getOrderList.data) === null || _getOrderList$data2 === void 0 ? void 0 : _getOrderList$data2.count) !== null && _getOrderList$data$co !== void 0 ? _getOrderList$data$co : 0,
		pagination,
		onPageChange,
		searchParams,
		onSearch,
		onReset,
		selectedKeys,
		selectedRows,
		onSelectionChange,
		rowKey: "id",
		onRefresh,
		refreshAsync
	};
}
//#endregion
exports.useSalesGridData = useSalesGridData;
