import { ensureAuthProfileStore, loadAuthProfileStore, resolveAuthStorePath, saveAuthProfileStore } from "./store.js";
//#region src/auth/profiles/profiles.ts
function hasKey(cred) {
	if (cred.type === "api_key") return !!cred.key;
	if (cred.type === "token") return !!cred.token;
	if (cred.type === "oauth") return !!cred.access;
	return false;
}
function getExpires(cred) {
	if (cred.type === "token") return cred.expires;
	if (cred.type === "oauth") return cred.expires;
}
/** Get all profiles for a provider */
function listProfilesForProvider(provider, dataDir) {
	const store = loadAuthProfileStore(dataDir);
	const profiles = [];
	for (const [profileId, cred] of Object.entries(store.profiles)) if (cred.provider === provider) profiles.push({
		profileId,
		provider: cred.provider,
		type: cred.type,
		email: cred.email,
		hasKey: hasKey(cred),
		expires: getExpires(cred)
	});
	return profiles;
}
/** Get all auth profiles */
function listAllProfiles(dataDir) {
	const store = loadAuthProfileStore(dataDir);
	const profiles = [];
	for (const [profileId, cred] of Object.entries(store.profiles)) profiles.push({
		profileId,
		provider: cred.provider,
		type: cred.type,
		email: cred.email,
		hasKey: hasKey(cred),
		expires: getExpires(cred)
	});
	return profiles;
}
/** Get profile by ID */
function getProfile(profileId, dataDir) {
	return loadAuthProfileStore(dataDir).profiles[profileId];
}
/** Upsert (insert or update) an auth profile */
function upsertAuthProfile(profile, dataDir) {
	const store = ensureAuthProfileStore(dataDir);
	store.profiles[profile.profileId] = profile.credential;
	saveAuthProfileStore(store, dataDir);
}
/** Remove an auth profile */
function removeAuthProfile(profileId, dataDir) {
	const store = loadAuthProfileStore(dataDir);
	if (store.profiles[profileId]) {
		delete store.profiles[profileId];
		saveAuthProfileStore(store, dataDir);
		return true;
	}
	return false;
}
/** Set profile order for a provider */
function setAuthProfileOrder(provider, profileIds, dataDir) {
	const store = ensureAuthProfileStore(dataDir);
	if (!store.order) store.order = {};
	store.order[provider] = profileIds;
	saveAuthProfileStore(store, dataDir);
}
/** Get profile order for a provider */
function getAuthProfileOrder(provider, dataDir) {
	return loadAuthProfileStore(dataDir).order?.[provider];
}
/** Get all providers with profiles */
function getProvidersWithProfiles(dataDir) {
	const store = loadAuthProfileStore(dataDir);
	const providers = /* @__PURE__ */ new Set();
	for (const cred of Object.values(store.profiles)) providers.add(cred.provider);
	return Array.from(providers);
}
/** Get auth path for display */
function getAuthStorePath(dataDir) {
	return resolveAuthStorePath(dataDir);
}
//#endregion
export { getAuthProfileOrder, getAuthStorePath, getProfile, getProvidersWithProfiles, listAllProfiles, listProfilesForProvider, removeAuthProfile, setAuthProfileOrder, upsertAuthProfile };

//# sourceMappingURL=profiles.js.map