import { formatResponse } from "../utils/responseData.mjs";
import { ErrorHandler } from "../utils/errors/ErrorHandler.mjs";
import { checkIntlayerConfig, exchangeCodeForToken, getAuthorizationUrl, getGitLabTokenFromUser, getRepositoryFileContents, getUserProjects } from "../services/gitlab.service.mjs";

//#region src/controllers/gitlab.controller.ts
const getAuthUrl = async (request, reply) => {
	const { redirectUri, instanceUrl, login } = request.query;
	if (!redirectUri) return ErrorHandler.handleGenericErrorResponse(reply, "GITLAB_REDIRECT_URI_MISSING");
	try {
		const responseData = formatResponse({ data: { authUrl: getAuthorizationUrl(redirectUri, instanceUrl, login) } });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
const authCallback = async (request, reply) => {
	const { code, redirectUri, instanceUrl } = request.body;
	if (!code || !redirectUri) return ErrorHandler.handleGenericErrorResponse(reply, "GITLAB_CODE_MISSING");
	try {
		const responseData = formatResponse({ data: { token: await exchangeCodeForToken(code, redirectUri, instanceUrl) } });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
const listProjects = async (request, reply) => {
	const { token, instanceUrl } = request.query;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getGitLabTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken) return ErrorHandler.handleGenericErrorResponse(reply, "GITLAB_TOKEN_MISSING");
		const responseData = formatResponse({ data: await getUserProjects(accessToken, instanceUrl) });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
/**
* Check if intlayer.config.ts (or candidates) exists in a GitLab repository
*/
const checkConfig = async (request, reply) => {
	const { token, projectId, branch = "main", instanceUrl } = request.body;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getGitLabTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken || !projectId) return ErrorHandler.handleGenericErrorResponse(reply, "GITLAB_CHECK_CONFIG_MISSING_PARAMS");
		const configPaths = await checkIntlayerConfig(accessToken, projectId, branch, instanceUrl);
		const responseData = formatResponse({ data: {
			hasConfig: configPaths.length > 0,
			configPaths
		} });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
const getConfigFile = async (request, reply) => {
	const { token, projectId, branch = "main", path = "intlayer.config.ts", instanceUrl } = request.body;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getGitLabTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken || !projectId) return ErrorHandler.handleGenericErrorResponse(reply, "GITLAB_GET_CONFIG_FILE_MISSING_PARAMS");
		const content = await getRepositoryFileContents(accessToken, projectId, path, branch, instanceUrl);
		if (!content) return ErrorHandler.handleGenericErrorResponse(reply, "GITLAB_CONFIG_FILE_NOT_FOUND");
		const responseData = formatResponse({ data: { content } });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};

//#endregion
export { authCallback, checkConfig, getAuthUrl, getConfigFile, listProjects };
//# sourceMappingURL=gitlab.controller.mjs.map