import{formatResponse as e}from"../utils/responseData.mjs";import{ErrorHandler as t}from"../utils/errors/ErrorHandler.mjs";import{checkIntlayerConfig as n,exchangeCodeForToken as r,getAuthorizationUrl as i,getGitLabTokenFromUser as a,getRepositoryFileContents as o,getUserProjects as s}from"../services/gitlab.service.mjs";const c=async(n,r)=>{let{redirectUri:a,instanceUrl:o,login:s}=n.query;if(!a)return t.handleGenericErrorResponse(r,`GITLAB_REDIRECT_URI_MISSING`);try{let t=e({data:{authUrl:i(a,o,s)}});return r.send(t)}catch(e){return t.handleAppErrorResponse(r,e)}},l=async(n,i)=>{let{code:a,redirectUri:o,instanceUrl:s}=n.body;if(!a||!o)return t.handleGenericErrorResponse(i,`GITLAB_CODE_MISSING`);try{let t=e({data:{token:await r(a,o,s)}});return i.send(t)}catch(e){return t.handleAppErrorResponse(i,e)}},u=async(n,r)=>{let{token:i,instanceUrl:o}=n.query,c=n.locals?.user?.id;try{let n=i;if(!n&&c&&(n=await a(String(c))??void 0),!n)return t.handleGenericErrorResponse(r,`GITLAB_TOKEN_MISSING`);let l=e({data:await s(n,o)});return r.send(l)}catch(e){return t.handleAppErrorResponse(r,e)}},d=async(r,i)=>{let{token:o,projectId:s,branch:c=`main`,instanceUrl:l}=r.body,u=r.locals?.user?.id;try{let r=o;if(!r&&u&&(r=await a(String(u))??void 0),!r||!s)return t.handleGenericErrorResponse(i,`GITLAB_CHECK_CONFIG_MISSING_PARAMS`);let d=await n(r,s,c,l),f=e({data:{hasConfig:d.length>0,configPaths:d}});return i.send(f)}catch(e){return t.handleAppErrorResponse(i,e)}},f=async(n,r)=>{let{token:i,projectId:s,branch:c=`main`,path:l=`intlayer.config.ts`,instanceUrl:u}=n.body,d=n.locals?.user?.id;try{let n=i;if(!n&&d&&(n=await a(String(d))??void 0),!n||!s)return t.handleGenericErrorResponse(r,`GITLAB_GET_CONFIG_FILE_MISSING_PARAMS`);let f=await o(n,s,l,c,u);if(!f)return t.handleGenericErrorResponse(r,`GITLAB_CONFIG_FILE_NOT_FOUND`);let p=e({data:{content:f}});return r.send(p)}catch(e){return t.handleAppErrorResponse(r,e)}};export{l as authCallback,d as checkConfig,c as getAuthUrl,f as getConfigFile,u as listProjects};
//# sourceMappingURL=gitlab.controller.mjs.map