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

//#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 } = require_utils_filtersAndPagination_getFiltersAndPaginationFromBody.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: require_utils_ensureArrayQueryFilter.ensureArrayQueryFilter(ids) }
	};
	if (keys) filters = {
		...filters,
		key: { $in: require_utils_ensureArrayQueryFilter.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
exports.getTagFiltersAndPagination = getTagFiltersAndPagination;
//# sourceMappingURL=getTagFiltersAndPagination.cjs.map