#!/usr/bin/env node

// src/symbols/store.ts
import fs from "fs";
import path from "path";
import os from "os";
import crypto from "crypto";

// src/symbols/types.ts
var SYMBOL_INDEX_VERSION = 1;
function detectLanguage(ext) {
  switch (ext.toLowerCase()) {
    case "ts":
    case "mts":
    case "cts":
      return "typescript";
    case "tsx":
      return "tsx";
    case "js":
    case "mjs":
    case "cjs":
    case "jsx":
      return "javascript";
    case "py":
    case "pyi":
      return "python";
    case "go":
      return "go";
    case "rs":
      return "rust";
    case "java":
      return "java";
    case "c":
    case "cc":
    case "cpp":
    case "cxx":
    case "h":
    case "hh":
    case "hpp":
    case "hxx":
      return "cpp";
    default:
      return null;
  }
}

// src/symbols/store.ts
var INDEX_DIR_NAME = "index";
function indexDir() {
  return path.join(os.homedir(), ".aicli", INDEX_DIR_NAME);
}
function projectHash(root) {
  const normalized = path.resolve(root).toLowerCase();
  return crypto.createHash("sha1").update(normalized).digest("hex").slice(0, 16);
}
function indexPath(root) {
  return path.join(indexDir(), `${projectHash(root)}.json`);
}
function loadIndex(root) {
  const p = indexPath(root);
  try {
    if (!fs.existsSync(p)) return null;
    const raw = fs.readFileSync(p, "utf-8");
    const parsed = JSON.parse(raw);
    if (parsed.version !== SYMBOL_INDEX_VERSION) return null;
    if (parsed.root && path.resolve(parsed.root) !== path.resolve(root)) {
      return null;
    }
    return parsed;
  } catch {
    return null;
  }
}
function saveIndex(index) {
  const dir = indexDir();
  fs.mkdirSync(dir, { recursive: true });
  const target = indexPath(index.root);
  const tmp = `${target}.tmp`;
  fs.writeFileSync(tmp, JSON.stringify(index), "utf-8");
  fs.renameSync(tmp, target);
}
function clearIndex(root) {
  const p = indexPath(root);
  try {
    if (fs.existsSync(p)) fs.unlinkSync(p);
  } catch {
  }
}
function emptyIndex(root) {
  return {
    version: SYMBOL_INDEX_VERSION,
    root: path.resolve(root),
    generated: (/* @__PURE__ */ new Date()).toISOString(),
    fileCount: 0,
    symbolCount: 0,
    files: {},
    symbols: []
  };
}
function upsertFileSymbols(index, file, mtime, symbols) {
  const absFile = path.resolve(file);
  const had = absFile in index.files;
  index.symbols = index.symbols.filter((s) => s.location.file !== absFile);
  index.symbols.push(...symbols);
  index.files[absFile] = mtime;
  index.symbolCount = index.symbols.length;
  if (!had) index.fileCount = Object.keys(index.files).length;
  index.generated = (/* @__PURE__ */ new Date()).toISOString();
  return index;
}
function removeFile(index, file) {
  const absFile = path.resolve(file);
  if (!(absFile in index.files)) return index;
  index.symbols = index.symbols.filter((s) => s.location.file !== absFile);
  delete index.files[absFile];
  index.symbolCount = index.symbols.length;
  index.fileCount = Object.keys(index.files).length;
  index.generated = (/* @__PURE__ */ new Date()).toISOString();
  return index;
}

export {
  detectLanguage,
  loadIndex,
  saveIndex,
  clearIndex,
  emptyIndex,
  upsertFileSymbols,
  removeFile
};
