const require_chunk = require('./chunk-CUT6urMc.cjs');
const node_fs = require_chunk.__toESM(require("node:fs"));
const node_path = require_chunk.__toESM(require("node:path"));
const node_fs_promises = require_chunk.__toESM(require("node:fs/promises"));
const node_os = require_chunk.__toESM(require("node:os"));

//#region src/auth/credentials.ts
/**
* Get the path to the credentials directory
*/
function getCredentialsDir(options) {
	const root = options?.root ?? (0, node_os.homedir)();
	return (0, node_path.join)(root, ".gkm");
}
/**
* Get the path to the credentials file
*/
function getCredentialsPath(options) {
	return (0, node_path.join)(getCredentialsDir(options), "credentials.json");
}
/**
* Ensure the credentials directory exists
*/
function ensureCredentialsDir(options) {
	const dir = getCredentialsDir(options);
	if (!(0, node_fs.existsSync)(dir)) (0, node_fs.mkdirSync)(dir, {
		recursive: true,
		mode: 448
	});
}
/**
* Read stored credentials from disk
*/
async function readCredentials(options) {
	const path = getCredentialsPath(options);
	if (!(0, node_fs.existsSync)(path)) return {};
	try {
		const content = await (0, node_fs_promises.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 (0, node_fs_promises.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
Object.defineProperty(exports, 'getCredentialsPath', {
  enumerable: true,
  get: function () {
    return getCredentialsPath;
  }
});
Object.defineProperty(exports, 'getDokployCredentials', {
  enumerable: true,
  get: function () {
    return getDokployCredentials;
  }
});
Object.defineProperty(exports, 'getDokployRegistryId', {
  enumerable: true,
  get: function () {
    return getDokployRegistryId;
  }
});
Object.defineProperty(exports, 'getDokployToken', {
  enumerable: true,
  get: function () {
    return getDokployToken;
  }
});
Object.defineProperty(exports, 'getHostingerToken', {
  enumerable: true,
  get: function () {
    return getHostingerToken;
  }
});
Object.defineProperty(exports, 'removeDokployCredentials', {
  enumerable: true,
  get: function () {
    return removeDokployCredentials;
  }
});
Object.defineProperty(exports, 'storeDokployCredentials', {
  enumerable: true,
  get: function () {
    return storeDokployCredentials;
  }
});
Object.defineProperty(exports, 'storeDokployRegistryId', {
  enumerable: true,
  get: function () {
    return storeDokployRegistryId;
  }
});
//# sourceMappingURL=credentials-C8DWtnMY.cjs.map