import { formatResponse } from "../utils/responseData.mjs";
import { ErrorHandler } from "../utils/errors/ErrorHandler.mjs";
import { checkIntlayerConfig, exchangeCodeForToken, getAuthorizationUrl, getGitHubTokenFromUser, getRepositoryFileContents, getUserRepos } from "../services/github.service.mjs";

//#region src/controllers/github.controller.ts
const getAuthUrl = async (request, reply) => {
	const { redirectUri, login } = request.query;
	if (!redirectUri) return ErrorHandler.handleGenericErrorResponse(reply, "GITHUB_REDIRECT_URI_MISSING");
	try {
		const responseData = formatResponse({ data: { authUrl: getAuthorizationUrl(redirectUri, login) } });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
const authCallback = async (request, reply) => {
	const { code } = request.body;
	if (!code) return ErrorHandler.handleGenericErrorResponse(reply, "GITHUB_CODE_MISSING");
	try {
		const responseData = formatResponse({ data: { token: await exchangeCodeForToken(code) } });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
const listRepos = async (request, reply) => {
	const { token } = request.query;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getGitHubTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken) return ErrorHandler.handleGenericErrorResponse(reply, "GITHUB_TOKEN_MISSING");
		const responseData = formatResponse({ data: await getUserRepos(accessToken) });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
/**
* Check if intlayer.config.ts (or candidates) exists in a repository
*/
const checkConfig = async (request, reply) => {
	const { token, owner, repository, branch = "main" } = request.body;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getGitHubTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken || !owner || !repository) return ErrorHandler.handleGenericErrorResponse(reply, "GITHUB_CHECK_CONFIG_MISSING_PARAMS");
		const configPaths = await checkIntlayerConfig(accessToken, owner, repository, branch);
		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, owner, repository, branch = "main", path = "intlayer.config.ts" } = request.body;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getGitHubTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken || !owner || !repository) return ErrorHandler.handleGenericErrorResponse(reply, "GITHUB_GET_CONFIG_FILE_MISSING_PARAMS");
		const content = await getRepositoryFileContents(accessToken, owner, repository, path, branch);
		if (!content) return ErrorHandler.handleGenericErrorResponse(reply, "GITHUB_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, listRepos };
//# sourceMappingURL=github.controller.mjs.map