const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_utils_responseData = require('../utils/responseData.cjs');
const require_utils_errors_ErrorHandler = require('../utils/errors/ErrorHandler.cjs');
const require_utils_permissions = require('../utils/permissions.cjs');
const require_services_email_service = require('../services/email.service.cjs');
const require_services_projectAccessKey_service = require('../services/projectAccessKey.service.cjs');
let express_intlayer = require("express-intlayer");
express_intlayer = require_rolldown_runtime.__toESM(express_intlayer);

//#region src/controllers/projectAccessKey.controller.ts
/**
* Adds a new access key to a project.
*/
const addNewAccessKey = async (req, res, _next) => {
	const { user, project, roles, permissions } = res.locals;
	const { grants, name, expiresAt } = req.body;
	if (!project) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
		return;
	}
	if (!user) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
		return;
	}
	if (!require_utils_permissions.hasPermission(roles, "project:write")({
		...res.locals,
		targetProjectIds: [project.id]
	})) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
		return;
	}
	const filteredPermisions = require_utils_permissions.intersectPermissions(permissions, grants);
	try {
		const newAccessKey = await require_services_projectAccessKey_service.addNewAccessKey({
			name,
			expiresAt,
			grants: filteredPermisions
		}, project.id, user);
		const responseData = require_utils_responseData.formatResponse({
			message: (0, express_intlayer.t)({
				en: "Access key created successfully",
				es: "Clave de acceso creada con éxito",
				fr: "Clé d'accès créée avec succès"
			}),
			description: (0, express_intlayer.t)({
				en: "The access key has been created successfully",
				es: "La clave de acceso ha sido creada con éxito",
				fr: "La clé d'accès a été créée avec succès"
			}),
			data: newAccessKey
		});
		res.json(responseData);
		require_services_email_service.sendEmail({
			type: "oAuthTokenCreated",
			to: user.email,
			username: user.name,
			applicationName: newAccessKey.name ?? newAccessKey.clientId,
			scopes: newAccessKey.grants,
			tokenDetailsUrl: `${process.env.CLIENT_URL}/oauth2/token`,
			securityLogUrl: `${process.env.CLIENT_URL}/security-log`,
			supportUrl: `${process.env.CLIENT_URL}/support`
		});
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};
/**
* Deletes an access key from a project.
*/
const deleteAccessKey = async (req, res, _next) => {
	const { user, project, roles } = res.locals;
	const { clientId } = req.body;
	if (!project) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
		return;
	}
	if (!user) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
		return;
	}
	if (!clientId) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "CLIENT_ID_NOT_FOUND");
		return;
	}
	if (!require_utils_permissions.hasPermission(roles, "project:write")({
		...res.locals,
		targetProjectIds: [project.id]
	})) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
		return;
	}
	try {
		if (!await require_services_projectAccessKey_service.deleteAccessKey(clientId, project, user.id)) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "ACCESS_KEY_NOT_FOUND", { clientId });
			return;
		}
		const responseData = require_utils_responseData.formatResponse({
			message: (0, express_intlayer.t)({
				en: "Access key deleted successfully",
				es: "Clave de acceso eliminada con éxito",
				fr: "Clé d'accès supprimée avec succès"
			}),
			description: (0, express_intlayer.t)({
				en: "The access key has been deleted successfully",
				es: "La clave de acceso ha sido eliminada con éxito",
				fr: "La clé d'accès a été supprimée avec succès"
			}),
			data: null
		});
		res.json(responseData);
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};
/**
* Refreshes an access key from a project.
*/
const refreshAccessKey = async (req, res, _next) => {
	const { user, project, roles } = res.locals;
	const { clientId } = req.body;
	if (!project) require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
	if (!user) require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
	if (!clientId) require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "CLIENT_ID_NOT_FOUND");
	if (!require_utils_permissions.hasPermission(roles, "project:write")({
		...res.locals,
		targetProjectIds: [project?.id]
	})) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
		return;
	}
	try {
		const newAccessKey = await require_services_projectAccessKey_service.refreshAccessKey(clientId, project?.id, user?.id);
		const responseData = require_utils_responseData.formatResponse({
			message: (0, express_intlayer.t)({
				en: "Access key refreshed successfully",
				es: "Clave de acceso actualizada con éxito",
				fr: "Clé d'accès actualisée avec succès"
			}),
			description: (0, express_intlayer.t)({
				en: "The access key has been refreshed successfully",
				es: "La clave de acceso ha sido actualizada con éxito",
				fr: "La clé d'accès a été actualisée avec succès"
			}),
			data: newAccessKey
		});
		res.json(responseData);
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};

//#endregion
exports.addNewAccessKey = addNewAccessKey;
exports.deleteAccessKey = deleteAccessKey;
exports.refreshAccessKey = refreshAccessKey;
//# sourceMappingURL=projectAccessKey.controller.cjs.map