const require_chunk = require("./chunk-DakpK96I.cjs");
let path = require("path");
path = require_chunk.__toESM(path, 1);
let fs = require("fs");
fs = require_chunk.__toESM(fs, 1);
let google_auth_library = require("google-auth-library");
let readline = require("readline");
readline = require_chunk.__toESM(readline, 1);
//#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.default.readFileSync(credentialsPath, "utf-8"));
	const { client_id, client_secret, redirect_uris } = credentials.installed ?? credentials.web;
	const oAuth2Client = new google_auth_library.OAuth2Client(client_id, client_secret, redirect_uris[0]);
	if (fs.default.existsSync(tokenPath)) {
		const token = JSON.parse(fs.default.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.default.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.default.mkdirSync(path.default.dirname(tokenPath), { recursive: true });
	fs.default.writeFileSync(tokenPath, JSON.stringify(tokens));
	return oAuth2Client;
}
//#endregion
exports.getGmailAuth = getGmailAuth;

//# sourceMappingURL=gmail-auth-DxakCtGm.cjs.map