import { formatResponse } from "../utils/responseData.mjs";
import { ErrorHandler } from "../utils/errors/ErrorHandler.mjs";
import { checkIntlayerConfig, exchangeCodeForToken, getAuthorizationUrl, getBitbucketTokenFromUser, getRepositoryFileContents, getUserRepositories } from "../services/bitbucket.service.mjs";

//#region src/controllers/bitbucket.controller.ts
const getAuthUrl = async (request, reply) => {
	const { redirectUri } = request.query;
	if (!redirectUri) return ErrorHandler.handleGenericErrorResponse(reply, "BITBUCKET_REDIRECT_URI_MISSING");
	try {
		const responseData = formatResponse({ data: { authUrl: getAuthorizationUrl(redirectUri) } });
		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, "BITBUCKET_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 getBitbucketTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken) return ErrorHandler.handleGenericErrorResponse(reply, "BITBUCKET_TOKEN_MISSING");
		const responseData = formatResponse({ data: await getUserRepositories(accessToken) });
		return reply.send(responseData);
	} catch (error) {
		return ErrorHandler.handleAppErrorResponse(reply, error);
	}
};
/**
* Check if intlayer.config.ts (or candidates) exists in a Bitbucket repository
*/
const checkConfig = async (request, reply) => {
	const { token, workspace, repoSlug, branch = "main" } = request.body;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getBitbucketTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken || !workspace || !repoSlug) return ErrorHandler.handleGenericErrorResponse(reply, "BITBUCKET_CHECK_CONFIG_MISSING_PARAMS");
		const configPaths = await checkIntlayerConfig(accessToken, workspace, repoSlug, 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, workspace, repoSlug, branch = "main", path = "intlayer.config.ts" } = request.body;
	const userId = request.locals?.user?.id;
	try {
		let accessToken = token;
		if (!accessToken && userId) accessToken = await getBitbucketTokenFromUser(String(userId)) ?? void 0;
		if (!accessToken || !workspace || !repoSlug) return ErrorHandler.handleGenericErrorResponse(reply, "BITBUCKET_GET_CONFIG_FILE_MISSING_PARAMS");
		const content = await getRepositoryFileContents(accessToken, workspace, repoSlug, path, branch);
		if (!content) return ErrorHandler.handleGenericErrorResponse(reply, "BITBUCKET_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=bitbucket.controller.mjs.map