import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { defaultPaymentStatusKeys } from "../config/booking.js";
import { getBookingList, getOrderList } from "../../pisellSalesGrid/serve.js";
import dayjs from "dayjs";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useRequest } from "ahooks";
//#region src/plus/pisellSalesManagement/hooks/useSalesGridData.ts
function getDefaultSearchParams(perspective) {
	if (perspective === "monitor") return { filter: { values: {
		order_payment_status: defaultPaymentStatusKeys,
		booking_time_start_between: [dayjs().startOf("day"), dayjs().endOf("day")]
	} } };
	return {
		filter: { values: {
			payment_status: defaultPaymentStatusKeys,
			orderDate: [dayjs(), dayjs()]
		} },
		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] = useState({
		pageNumber: 1,
		pageSize: 50
	});
	const [searchParams, setSearchParams] = useState(() => getDefaultSearchParams(perspective));
	const [selectedKeys, setSelectedKeys] = useState([]);
	const [selectedRows, setSelectedRows] = useState([]);
	const requestParams = useMemo(() => {
		const { keyword, filter, sort } = searchParams;
		const filterVals = (filter === null || filter === void 0 ? void 0 : filter.values) || {};
		return _objectSpread2(_objectSpread2({
			keyword,
			sort
		}, filterVals), {}, {
			skip: pagination.pageNumber,
			num: pagination.pageSize
		});
	}, [
		searchParams,
		pagination.pageNumber,
		pagination.pageSize
	]);
	const _getOrderList = useRequest(getOrderList, { manual: true });
	const _getBookingList = useRequest(getBookingList, { manual: true });
	const runListParams = useMemo(() => _objectSpread2(_objectSpread2({}, requestParams), (extParams === null || extParams === void 0 ? void 0 : extParams.list) || {}), [requestParams, extParams === null || extParams === void 0 ? void 0 : extParams.list]);
	useEffect(() => {
		if (perspective === "monitor") _getBookingList.run(runListParams);
		else _getOrderList.run(runListParams);
	}, [perspective, runListParams]);
	useEffect(() => {
		setSearchParams(getDefaultSearchParams(perspective));
		setPagination((p) => _objectSpread2(_objectSpread2({}, p), {}, { pageNumber: 1 }));
		setSelectedKeys([]);
		setSelectedRows([]);
	}, [perspective]);
	const onPageChange = useCallback((pageNumber, pageSize) => {
		if (pageNumber < 1 || pageSize < 1) return;
		setPagination({
			pageNumber,
			pageSize
		});
	}, []);
	const onSearch = useCallback((params) => {
		setSearchParams(params || {});
		setPagination((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { pageNumber: 1 }));
	}, []);
	const onReset = useCallback(() => {
		setSearchParams(getDefaultSearchParams(perspective));
		setPagination((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { pageNumber: 1 }));
	}, [perspective]);
	const onSelectionChange = useCallback((keys, rows) => {
		setSelectedKeys(keys);
		setSelectedRows(rows);
	}, []);
	const onRefresh = useCallback(() => {
		if (perspective === "monitor") _getBookingList.run(runListParams);
		else _getOrderList.run(runListParams);
	}, [
		perspective,
		runListParams,
		_getBookingList,
		_getOrderList
	]);
	const refreshAsync = 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
export { useSalesGridData };
