import { existsSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { readFile, writeFile } from "node:fs/promises";
import { homedir } from "node:os";

//#region src/auth/credentials.ts
/**
* Get the path to the credentials directory
*/
function getCredentialsDir(options) {
	const root = options?.root ?? homedir();
	return join(root, ".gkm");
}
/**
* Get the path to the credentials file
*/
function getCredentialsPath(options) {
	return join(getCredentialsDir(options), "credentials.json");
}
/**
* Ensure the credentials directory exists
*/
function ensureCredentialsDir(options) {
	const dir = getCredentialsDir(options);
	if (!existsSync(dir)) mkdirSync(dir, {
		recursive: true,
		mode: 448
	});
}
/**
* Read stored credentials from disk
*/
async function readCredentials(options) {
	const path = getCredentialsPath(options);
	if (!existsSync(path)) return {};
	try {
		const content = await readFile(path, "utf-8");
		return JSON.parse(content);
	} catch {
		return {};
	}
}
/**
* Write credentials to disk
*/
async function writeCredentials(credentials, options) {
	ensureCredentialsDir(options);
	const path = getCredentialsPath(options);
	await writeFile(path, JSON.stringify(credentials, null, 2), { mode: 384 });
}
/**
* Store Dokploy credentials
*/
async function storeDokployCredentials(token, endpoint, options) {
	const credentials = await readCredentials(options);
	credentials.dokploy = {
		token,
		endpoint,
		storedAt: (/* @__PURE__ */ new Date()).toISOString()
	};
	await writeCredentials(credentials, options);
}
/**
* Get stored Dokploy credentials
*/
async function getDokployCredentials(options) {
	const credentials = await readCredentials(options);
	if (!credentials.dokploy) return null;
	return {
		token: credentials.dokploy.token,
		endpoint: credentials.dokploy.endpoint,
		registryId: credentials.dokploy.registryId
	};
}
/**
* Remove Dokploy credentials
*/
async function removeDokployCredentials(options) {
	const credentials = await readCredentials(options);
	if (!credentials.dokploy) return false;
	delete credentials.dokploy;
	await writeCredentials(credentials, options);
	return true;
}
/**
* Get Dokploy API token, checking stored credentials first, then environment
*/
async function getDokployToken(options) {
	const envToken = process.env.DOKPLOY_API_TOKEN;
	if (envToken) return envToken;
	const stored = await getDokployCredentials(options);
	if (stored) return stored.token;
	return null;
}
/**
* Store Dokploy registry ID
*/
async function storeDokployRegistryId(registryId, options) {
	const credentials = await readCredentials(options);
	if (!credentials.dokploy) throw new Error("Dokploy credentials not found. Run \"gkm login --service dokploy\" first.");
	credentials.dokploy.registryId = registryId;
	await writeCredentials(credentials, options);
}
/**
* Get Dokploy registry ID from stored credentials
*/
async function getDokployRegistryId(options) {
	const stored = await getDokployCredentials(options);
	return stored?.registryId ?? void 0;
}
/**
* Get stored Hostinger API token
*
* Checks environment variable first (HOSTINGER_API_TOKEN),
* then falls back to stored credentials.
*/
async function getHostingerToken(options) {
	const envToken = process.env.HOSTINGER_API_TOKEN;
	if (envToken) return envToken;
	const credentials = await readCredentials(options);
	return credentials.hostinger?.token ?? null;
}

//#endregion
export { getCredentialsPath, getDokployCredentials, getDokployRegistryId, getDokployToken, getHostingerToken, removeDokployCredentials, storeDokployCredentials, storeDokployRegistryId };
//# sourceMappingURL=credentials-s1kLcIzK.mjs.map