import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getCurrentViewModeFromLocalStorage, getTableSettingByCurrentViewMode, setTableSettingToLocalStorage } from "../../../table/Table/utils.js";
import { useEffect, useMemo, useState } from "react";
import { useMemoizedFn } from "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 = 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 = useMemo(() => getCurrentViewModeFromLocalStorage(tableId) || "grid", [tableId]);
	const [queryPageConfig, setQueryPageConfig] = 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 = useMemoizedFn(() => {
		try {
			if (!tableId) return {};
			const setting = getTableSettingByCurrentViewMode(tableId, currentViewMode);
			console.log("setting22", setting);
			return (setting === null || setting === void 0 ? void 0 : setting.filters) || {};
		} catch (e) {
			return {};
		}
	});
	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 = getTableSettingByCurrentViewMode(tableId, currentViewMode);
		const persisted = (setting === null || setting === void 0 ? void 0 : setting.filters) || {};
		if (persisted && Object.keys(persisted).length > 0) setQueryPageConfig((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { filter: _objectSpread2(_objectSpread2({}, prev.filter), persisted) }));
	}, [
		tableId,
		currentViewMode,
		JSON.stringify(localSettings)
	]);
	const handleValuesChange = 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 = _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 _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];
			});
			setTableSettingToLocalStorage({
				tableId,
				tableSettingObj: { filters: filtered },
				currentViewMode,
				currentSettingHash
			});
		}
	});
	const handleFilterChange = useMemoizedFn((changedFields, allFields) => {
		var _localSettings$scope3;
		const nextFilter = _objectSpread2({}, allFields);
		setQueryPageConfig((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
			page: 1,
			filter: _objectSpread2(_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];
			});
			setTableSettingToLocalStorage({
				tableId,
				tableSettingObj: { filters: filtered },
				currentViewMode,
				currentSettingHash
			});
		}
	});
	return {
		queryPageConfig,
		setQueryPageConfig,
		handleValuesChange,
		refreshData: useMemoizedFn(() => {
			setQueryPageConfig((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { page: 1 }));
		}),
		handleFilterChange,
		refreshCurrentPage: useMemoizedFn(() => {
			setQueryPageConfig((prev) => _objectSpread2({}, prev));
		}),
		getInitialValues
	};
};
//#endregion
export { useTableQuery as default };
