import path from "path";
import fs from "fs";
import { OAuth2Client } from "google-auth-library";
import readline from "readline";
//#region src/sync/gmail-auth.ts
const SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"];
async function getGmailAuth(credentialsPath, tokenPath) {
	const credentials = JSON.parse(fs.readFileSync(credentialsPath, "utf-8"));
	const { client_id, client_secret, redirect_uris } = credentials.installed ?? credentials.web;
	const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uris[0]);
	if (fs.existsSync(tokenPath)) {
		const token = JSON.parse(fs.readFileSync(tokenPath, "utf-8"));
		oAuth2Client.setCredentials(token);
		return oAuth2Client;
	}
	const authUrl = oAuth2Client.generateAuthUrl({
		access_type: "offline",
		scope: SCOPES
	});
	console.error("Authorize this app by visiting:\n" + authUrl);
	const rl = readline.createInterface({
		input: process.stdin,
		output: process.stdout
	});
	const code = await new Promise((resolve) => {
		rl.question("Enter the code from that page here: ", (c) => {
			rl.close();
			resolve(c);
		});
	});
	const { tokens } = await oAuth2Client.getToken(code);
	oAuth2Client.setCredentials(tokens);
	fs.mkdirSync(path.dirname(tokenPath), { recursive: true });
	fs.writeFileSync(tokenPath, JSON.stringify(tokens));
	return oAuth2Client;
}
//#endregion
export { getGmailAuth };

//# sourceMappingURL=gmail-auth-BP6cJwfw.js.map