import { join } from "node:path";
import { existsSync, unlinkSync } from "node:fs";
import { keychainAvailable, keychainSet, keychainGet, keychainDelete, WIGOLO_SERVICE } from "./keychain.js";
import { encryptToFile, decryptFromFile } from "./key-crypto.js";
import { providerEnvVar } from "../integrations/cloud/llm/select.js";
const PICKER_PROVIDERS = [
  "anthropic",
  "openai",
  "gemini",
  "custom"
];
const STORE_PROVIDERS = ["anthropic", "openai", "gemini", "groq"];
const _resolveMemo = /* @__PURE__ */ new Map();
function memoKey(provider, dataDir) {
  return `${provider}:${dataDir}`;
}
function invalidateMemo(provider) {
  const prefix = `${provider}:`;
  for (const k of _resolveMemo.keys()) {
    if (k.startsWith(prefix)) _resolveMemo.delete(k);
  }
}
function clearKeyStoreMemo() {
  _resolveMemo.clear();
}
function keychainKey(provider) {
  return `${WIGOLO_SERVICE}-${provider}`;
}
function encFilePath(provider, dataDir) {
  return join(dataDir, "keys", `${provider}.enc`);
}
async function storeKey(provider, value, opts) {
  invalidateMemo(provider);
  if (keychainAvailable()) {
    try {
      keychainSet(keychainKey(provider), provider, value);
      invalidateMemo(provider);
      return { location: "keychain" };
    } catch {
    }
  }
  await encryptToFile(value, opts.dataDir, encFilePath(provider, opts.dataDir));
  invalidateMemo(provider);
  return { location: "file" };
}
async function readKey(provider, opts) {
  if (keychainAvailable()) {
    const kc = keychainGet(keychainKey(provider), provider);
    if (kc !== null) return { value: kc, location: "keychain" };
  }
  const filePath = encFilePath(provider, opts.dataDir);
  if (existsSync(filePath)) {
    try {
      const value = await decryptFromFile(opts.dataDir, filePath);
      return { value, location: "file" };
    } catch {
    }
  }
  return null;
}
async function deleteKey(provider, opts) {
  if (keychainAvailable()) {
    keychainDelete(keychainKey(provider), provider);
  }
  const filePath = encFilePath(provider, opts.dataDir);
  if (existsSync(filePath)) {
    try {
      unlinkSync(filePath);
    } catch {
    }
  }
  invalidateMemo(provider);
}
async function resolveProviderKey(provider, opts) {
  const cacheKey = memoKey(provider, opts.dataDir);
  let storedValue;
  if (_resolveMemo.has(cacheKey)) {
    storedValue = _resolveMemo.get(cacheKey) ?? null;
  } else {
    const stored = await readKey(provider, opts);
    storedValue = stored ? stored.value : null;
    _resolveMemo.set(cacheKey, storedValue);
  }
  if (storedValue !== null) return storedValue;
  const envVar = providerEnvVar(provider);
  const envValue = process.env[envVar];
  return envValue || void 0;
}
async function listProviders(opts) {
  const results = [];
  for (const provider of STORE_PROVIDERS) {
    const found = await readKey(provider, opts);
    if (found !== null) {
      results.push({ provider, location: found.location });
    }
  }
  return results;
}
export {
  PICKER_PROVIDERS,
  clearKeyStoreMemo,
  deleteKey,
  listProviders,
  readKey,
  resolveProviderKey,
  storeKey
};
//# sourceMappingURL=key-store.js.map