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,getBitbucketTokenFromUser as a,getRepositoryFileContents as o,getUserRepositories as s}from"../services/bitbucket.service.mjs";const c=async(n,r)=>{let{redirectUri:a}=n.query;if(!a)return t.handleGenericErrorResponse(r,`BITBUCKET_REDIRECT_URI_MISSING`);try{let t=e({data:{authUrl:i(a)}});return r.send(t)}catch(e){return t.handleAppErrorResponse(r,e)}},l=async(n,i)=>{let{code:a}=n.body;if(!a)return t.handleGenericErrorResponse(i,`BITBUCKET_CODE_MISSING`);try{let t=e({data:{token:await r(a)}});return i.send(t)}catch(e){return t.handleAppErrorResponse(i,e)}},u=async(n,r)=>{let{token:i}=n.query,o=n.locals?.user?.id;try{let n=i;if(!n&&o&&(n=await a(String(o))??void 0),!n)return t.handleGenericErrorResponse(r,`BITBUCKET_TOKEN_MISSING`);let c=e({data:await s(n)});return r.send(c)}catch(e){return t.handleAppErrorResponse(r,e)}},d=async(r,i)=>{let{token:o,workspace:s,repoSlug:c,branch:l=`main`}=r.body,u=r.locals?.user?.id;try{let r=o;if(!r&&u&&(r=await a(String(u))??void 0),!r||!s||!c)return t.handleGenericErrorResponse(i,`BITBUCKET_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,workspace:s,repoSlug:c,branch:l=`main`,path:u=`intlayer.config.ts`}=n.body,d=n.locals?.user?.id;try{let n=i;if(!n&&d&&(n=await a(String(d))??void 0),!n||!s||!c)return t.handleGenericErrorResponse(r,`BITBUCKET_GET_CONFIG_FILE_MISSING_PARAMS`);let f=await o(n,s,c,u,l);if(!f)return t.handleGenericErrorResponse(r,`BITBUCKET_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 listRepos};
//# sourceMappingURL=bitbucket.controller.mjs.map