import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { join } from "path";
import { homedir } from "os";
const DEFAULT_STORAGE_CONFIG = {
  mode: "sqlite",
  chromadb: {
    enabled: false
  }
};
const STACKMEMORY_DIR = join(homedir(), ".stackmemory");
const CONFIG_FILE = join(STACKMEMORY_DIR, "storage-config.json");
function loadStorageConfig() {
  try {
    if (existsSync(CONFIG_FILE)) {
      const content = readFileSync(CONFIG_FILE, "utf-8");
      const config = JSON.parse(content);
      return {
        mode: config.mode || DEFAULT_STORAGE_CONFIG.mode,
        chromadb: {
          ...DEFAULT_STORAGE_CONFIG.chromadb,
          ...config.chromadb
        }
      };
    }
  } catch (error) {
    console.warn("Failed to load storage config, using defaults:", error);
  }
  return DEFAULT_STORAGE_CONFIG;
}
function saveStorageConfig(config) {
  try {
    if (!existsSync(STACKMEMORY_DIR)) {
      mkdirSync(STACKMEMORY_DIR, { recursive: true });
    }
    writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), "utf-8");
  } catch (error) {
    console.error("Failed to save storage config:", error);
    throw error;
  }
}
function isChromaDBEnabled() {
  const config = loadStorageConfig();
  if (!config.chromadb.enabled) {
    return false;
  }
  const apiKey = config.chromadb.apiKey || process.env["CHROMADB_API_KEY"];
  if (!apiKey) {
    return false;
  }
  return true;
}
function getStorageMode() {
  const config = loadStorageConfig();
  if (config.mode === "hybrid" && !isChromaDBEnabled()) {
    return "sqlite";
  }
  return config.mode;
}
function getChromaDBConfig() {
  if (!isChromaDBEnabled()) {
    return null;
  }
  const config = loadStorageConfig();
  const apiKey = config.chromadb.apiKey || process.env["CHROMADB_API_KEY"];
  const apiUrl = config.chromadb.apiUrl || process.env["CHROMADB_API_URL"] || "https://api.trychroma.com";
  return {
    enabled: true,
    apiKey,
    apiUrl,
    tenant: config.chromadb.tenant || process.env["CHROMADB_TENANT"] || "default_tenant",
    database: config.chromadb.database || process.env["CHROMADB_DATABASE"] || "default_database"
  };
}
function enableChromaDB(chromaConfig) {
  const config = loadStorageConfig();
  config.mode = "hybrid";
  config.chromadb = {
    enabled: true,
    apiKey: chromaConfig.apiKey,
    apiUrl: chromaConfig.apiUrl || "https://api.trychroma.com",
    tenant: chromaConfig.tenant || "default_tenant",
    database: chromaConfig.database || "default_database"
  };
  saveStorageConfig(config);
}
function disableChromaDB() {
  const config = loadStorageConfig();
  config.mode = "sqlite";
  config.chromadb = {
    enabled: false
  };
  saveStorageConfig(config);
}
function getStorageModeDescription() {
  const mode = getStorageMode();
  if (mode === "hybrid") {
    return "Hybrid (SQLite + ChromaDB for semantic search and cloud backup)";
  }
  return "SQLite (local storage only, fast, no external dependencies)";
}
export {
  disableChromaDB,
  enableChromaDB,
  getChromaDBConfig,
  getStorageMode,
  getStorageModeDescription,
  isChromaDBEnabled,
  loadStorageConfig,
  saveStorageConfig
};
//# sourceMappingURL=storage-config.js.map
