require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../table/Table/utils.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/components/dataSourceComponents/dataSourceTable/hooks/useTableQuery.ts
const useTableQuery = (props, currentSettingHash) => {
	var _tabs$items, _tabs$items2;
	const { filterBy, sortBy, tabs, localSettings } = props;
	const tableId = (0, react.useMemo)(() => {
		if (props.__designMode === "design") return "designMode-mock-table-id";
		return (props === null || props === void 0 ? void 0 : props.currentComponentId) || props.__id;
	}, [
		props.__id,
		props.__designMode,
		props === null || props === void 0 ? void 0 : props.currentComponentId
	]);
	const currentViewMode = (0, react.useMemo)(() => require_utils.getCurrentViewModeFromLocalStorage(tableId) || "grid", [tableId]);
	const [queryPageConfig, setQueryPageConfig] = (0, react.useState)({
		page: 1,
		pageSize: 20,
		filter: (tabs === null || tabs === void 0 || (_tabs$items = tabs.items) === null || _tabs$items === void 0 || (_tabs$items = _tabs$items[0]) === null || _tabs$items === void 0 ? void 0 : _tabs$items.filterBy) || filterBy || {},
		sort: (tabs === null || tabs === void 0 || (_tabs$items2 = tabs.items) === null || _tabs$items2 === void 0 || (_tabs$items2 = _tabs$items2[0]) === null || _tabs$items2 === void 0 ? void 0 : _tabs$items2.sortBy) || sortBy
	});
	const getInitialValues = (0, ahooks.useMemoizedFn)(() => {
		try {
			if (!tableId) return {};
			const setting = require_utils.getTableSettingByCurrentViewMode(tableId, currentViewMode);
			console.log("setting22", setting);
			return (setting === null || setting === void 0 ? void 0 : setting.filters) || {};
		} catch (e) {
			return {};
		}
	});
	(0, react.useEffect)(() => {
		var _localSettings$scope;
		if (!tableId) return;
		if (!!!((localSettings === null || localSettings === void 0 ? void 0 : localSettings.enable) && ((_localSettings$scope = localSettings.scope) === null || _localSettings$scope === void 0 ? void 0 : _localSettings$scope.includes("filters")))) return;
		const setting = require_utils.getTableSettingByCurrentViewMode(tableId, currentViewMode);
		const persisted = (setting === null || setting === void 0 ? void 0 : setting.filters) || {};
		if (persisted && Object.keys(persisted).length > 0) setQueryPageConfig((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { filter: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev.filter), persisted) }));
	}, [
		tableId,
		currentViewMode,
		JSON.stringify(localSettings)
	]);
	const handleValuesChange = (0, ahooks.useMemoizedFn)((params, allParams) => {
		var _localSettings$scope2;
		const { page, size } = params.pagination || {};
		let nextFilterForSave = null;
		setQueryPageConfig((prev) => {
			let sort = prev.sort || sortBy;
			let filter = prev.filter;
			if ("tabs" in params) {
				var _params$tabs, _params$tabs2;
				sort = ((_params$tabs = params.tabs) === null || _params$tabs === void 0 ? void 0 : _params$tabs.sortBy) || sortBy || "";
				const tabsFilter = ((_params$tabs2 = params.tabs) === null || _params$tabs2 === void 0 ? void 0 : _params$tabs2.filterBy) || {};
				filter = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev.filter), tabsFilter);
				nextFilterForSave = filter;
			}
			if ("order_by" in params) {
				var _allParams$tabs;
				sort = params.order_by || ((_allParams$tabs = allParams.tabs) === null || _allParams$tabs === void 0 ? void 0 : _allParams$tabs.sortBy) || sortBy;
			}
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, {
				page: size !== prev.pageSize ? 1 : page !== null && page !== void 0 ? page : prev.page,
				pageSize: size !== null && size !== void 0 ? size : prev.pageSize,
				sort,
				filter
			});
		});
		if (!!((localSettings === null || localSettings === void 0 ? void 0 : localSettings.enable) && ((_localSettings$scope2 = localSettings.scope) === null || _localSettings$scope2 === void 0 ? void 0 : _localSettings$scope2.includes("filters"))) && tableId && nextFilterForSave) {
			const excluded = new Set((localSettings === null || localSettings === void 0 ? void 0 : localSettings.excludedFilterFields) || []);
			const filtered = {};
			Object.keys(nextFilterForSave).forEach((k) => {
				if (!excluded.has(k)) filtered[k] = nextFilterForSave[k];
			});
			require_utils.setTableSettingToLocalStorage({
				tableId,
				tableSettingObj: { filters: filtered },
				currentViewMode,
				currentSettingHash
			});
		}
	});
	const handleFilterChange = (0, ahooks.useMemoizedFn)((changedFields, allFields) => {
		var _localSettings$scope3;
		const nextFilter = require_objectSpread2._objectSpread2({}, allFields);
		setQueryPageConfig((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, {
			page: 1,
			filter: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev.filter), nextFilter), filterBy || {})
		}));
		if (!!((localSettings === null || localSettings === void 0 ? void 0 : localSettings.enable) && ((_localSettings$scope3 = localSettings.scope) === null || _localSettings$scope3 === void 0 ? void 0 : _localSettings$scope3.includes("filters"))) && tableId) {
			const excluded = new Set((localSettings === null || localSettings === void 0 ? void 0 : localSettings.excludedFilterFields) || []);
			const filtered = {};
			Object.keys(nextFilter || {}).forEach((k) => {
				if (!excluded.has(k)) filtered[k] = nextFilter[k];
			});
			require_utils.setTableSettingToLocalStorage({
				tableId,
				tableSettingObj: { filters: filtered },
				currentViewMode,
				currentSettingHash
			});
		}
	});
	return {
		queryPageConfig,
		setQueryPageConfig,
		handleValuesChange,
		refreshData: (0, ahooks.useMemoizedFn)(() => {
			setQueryPageConfig((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { page: 1 }));
		}),
		handleFilterChange,
		refreshCurrentPage: (0, ahooks.useMemoizedFn)(() => {
			setQueryPageConfig((prev) => require_objectSpread2._objectSpread2({}, prev));
		}),
		getInitialValues
	};
};
//#endregion
exports.default = useTableQuery;
