const require_utils_ensureArrayQueryFilter = require('../ensureArrayQueryFilter.cjs');
const require_utils_filtersAndPagination_getFiltersAndPaginationFromBody = require('./getFiltersAndPaginationFromBody.cjs');

//#region src/utils/filtersAndPagination/getProjectFiltersAndPagination.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 getProjectFiltersAndPagination = (req, res) => {
	const { filters: filtersRequest,...pagination } = require_utils_filtersAndPagination_getFiltersAndPaginationFromBody.getFiltersAndPaginationFromBody(req);
	const { roles, organization } = res.locals;
	let filters = {};
	let sortOptions = { updatedAt: -1 };
	const { name, search, ids, organizationId, membersIds, sortBy, sortOrder, fetchAll } = filtersRequest ?? {};
	if (ids) filters = {
		...filters,
		_id: { $in: require_utils_ensureArrayQueryFilter.ensureArrayQueryFilter(ids) }
	};
	if (name) filters = {
		...filters,
		name: new RegExp(name, "i")
	};
	if (search) {
		const searchRegex = new RegExp(search, "i");
		filters = {
			...filters,
			$or: [{ name: searchRegex }]
		};
	}
	if (organizationId) filters = {
		...filters,
		organizationId
	};
	if (membersIds) filters = {
		...filters,
		membersIds: { $in: require_utils_ensureArrayQueryFilter.ensureArrayQueryFilter(membersIds) }
	};
	if (sortBy && sortOrder && (sortOrder === "asc" || sortOrder === "desc")) sortOptions = { [sortBy]: sortOrder === "asc" ? 1 : -1 };
	if (!(roles.includes("admin") && fetchAll === "true")) filters = {
		...filters,
		organizationId: organization?.id
	};
	return {
		filters,
		sortOptions,
		...pagination
	};
};

//#endregion
exports.getProjectFiltersAndPagination = getProjectFiltersAndPagination;
//# sourceMappingURL=getProjectFiltersAndPagination.cjs.map