import { ensureArrayQueryFilter } from "../ensureArrayQueryFilter.mjs";
import { getFiltersAndPaginationFromBody } from "./getFiltersAndPaginationFromBody.mjs";

//#region src/utils/filtersAndPagination/getTagFiltersAndPagination.ts
/**
* Extracts filters and pagination information from the request body.
* @param req - Express request object.
* @returns Object containing filters, page, pageSize, and getNumberOfPages functions.
*/
const getTagFiltersAndPagination = (req, res) => {
	const { filters: filtersRequest, ...pagination } = getFiltersAndPaginationFromBody(req);
	const { roles, organization } = res.locals;
	let filters = {};
	const sortOptions = { updatedAt: -1 };
	const { name, search, ids, keys, organizationId, fetchAll } = filtersRequest ?? {};
	if (ids) filters = {
		...filters,
		_id: { $in: ensureArrayQueryFilter(ids) }
	};
	if (keys) filters = {
		...filters,
		key: { $in: ensureArrayQueryFilter(keys) }
	};
	if (name) filters = {
		...filters,
		name: new RegExp(name, "i")
	};
	if (search) {
		const searchRegex = new RegExp(search, "i");
		filters = {
			...filters,
			$or: [
				{ name: searchRegex },
				{ key: searchRegex },
				{ description: searchRegex },
				{ instructions: searchRegex }
			]
		};
	}
	if (organizationId) filters = {
		...filters,
		organizationId
	};
	if (!(roles.includes("admin") && fetchAll === "true")) filters = {
		...filters,
		organizationId: organization?.id
	};
	return {
		filters,
		sortOptions,
		...pagination
	};
};

//#endregion
export { getTagFiltersAndPagination };
//# sourceMappingURL=getTagFiltersAndPagination.mjs.map