// @bun
import {
  getCommandName,
  init_commands1 as init_commands
} from "./chunk-4vz571bb.js";
import {
  getGlobalConfig,
  init_config1 as init_config,
  saveGlobalConfig
} from "./chunk-7rrkrej7.js";
import {
  __esm
} from "./chunk-hhsxm2yr.js";

// src/utils/suggestions/intelligentSuggestions.ts
import { existsSync } from "fs";
import { join } from "path";
function detectContext(cwd) {
  const context = {
    cwd,
    isGitRepo: false,
    hasPackageJson: false,
    hasDockerfile: false,
    hasPythonFiles: false,
    hasTypeScriptFiles: false,
    hasRustFiles: false,
    hasGoFiles: false
  };
  try {
    context.isGitRepo = existsSync(join(cwd, ".git"));
    context.hasPackageJson = existsSync(join(cwd, "package.json"));
    context.hasDockerfile = existsSync(join(cwd, "Dockerfile"));
    context.hasPythonFiles = existsSync(join(cwd, "requirements.txt")) || existsSync(join(cwd, "setup.py")) || existsSync(join(cwd, "pyproject.toml"));
    context.hasTypeScriptFiles = existsSync(join(cwd, "tsconfig.json")) || context.hasPackageJson;
    context.hasRustFiles = existsSync(join(cwd, "Cargo.toml"));
    context.hasGoFiles = existsSync(join(cwd, "go.mod"));
  } catch {}
  return context;
}
function getContextKey(context) {
  const keys = [];
  if (context.isGitRepo)
    keys.push("git");
  if (context.hasPackageJson)
    keys.push("node");
  if (context.hasDockerfile)
    keys.push("docker");
  if (context.hasPythonFiles)
    keys.push("python");
  if (context.hasTypeScriptFiles)
    keys.push("typescript");
  if (context.hasRustFiles)
    keys.push("rust");
  if (context.hasGoFiles)
    keys.push("go");
  return keys.length > 0 ? keys.join(",") : "generic";
}
function recordCommandAssociation(currentCommand, nextCommand) {
  if (!currentCommand || !nextCommand || currentCommand === nextCommand) {
    return;
  }
  saveGlobalConfig((config) => {
    const suggestions = config.intelligentSuggestions ?? {};
    const associations = suggestions.commandAssociations ?? {};
    const key = currentCommand;
    const existing = associations[key] ?? [];
    const associationIndex = existing.findIndex((a) => a.nextCommand === nextCommand);
    let updated;
    if (associationIndex >= 0) {
      updated = [...existing];
      updated[associationIndex] = {
        ...updated[associationIndex],
        count: updated[associationIndex].count + 1,
        lastUsedAt: Date.now()
      };
    } else {
      updated = [
        ...existing,
        {
          command: currentCommand,
          nextCommand,
          count: 1,
          lastUsedAt: Date.now()
        }
      ];
    }
    return {
      ...config,
      intelligentSuggestions: {
        ...suggestions,
        commandAssociations: {
          ...associations,
          [key]: updated
        },
        lastCommand: nextCommand
      }
    };
  });
}
function getCommandAssociations(lastCommand) {
  if (!lastCommand)
    return [];
  const config = getGlobalConfig();
  const suggestions = config.intelligentSuggestions;
  if (!suggestions?.commandAssociations)
    return [];
  const associations = suggestions.commandAssociations[lastCommand] ?? [];
  const sorted = [...associations].sort((a, b) => {
    const daysSinceA = (Date.now() - a.lastUsedAt) / (1000 * 60 * 60 * 24);
    const daysSinceB = (Date.now() - b.lastUsedAt) / (1000 * 60 * 60 * 24);
    const recencyA = 0.5 ** (daysSinceA / 7);
    const recencyB = 0.5 ** (daysSinceB / 7);
    const scoreA = a.count * Math.max(recencyA, 0.1);
    const scoreB = b.count * Math.max(recencyB, 0.1);
    return scoreB - scoreA;
  });
  return sorted.slice(0, 3).map((a) => a.nextCommand);
}
function getContextBasedRecommendations(context, commands) {
  const contextKey = getContextKey(context);
  const contextTypes = contextKey.split(",");
  const recommendations = new Set;
  for (const type of contextTypes) {
    const recs = CONTEXT_RECOMMENDATIONS[type];
    if (recs) {
      recs.forEach((cmd) => recommendations.add(cmd));
    }
  }
  const config = getGlobalConfig();
  const preferences = config.intelligentSuggestions?.contextPreferences ?? {};
  const userPrefs = preferences[contextKey] ?? [];
  userPrefs.forEach((cmd) => recommendations.add(cmd));
  const commandNames = new Set(commands.map((cmd) => getCommandName(cmd)));
  return Array.from(recommendations).filter((cmd) => commandNames.has(cmd));
}
function recordContextPreference(context, commandName) {
  const contextKey = getContextKey(context);
  saveGlobalConfig((config) => {
    const suggestions = config.intelligentSuggestions ?? {};
    const preferences = suggestions.contextPreferences ?? {};
    const existing = preferences[contextKey] ?? [];
    if (!existing.includes(commandName)) {
      return {
        ...config,
        intelligentSuggestions: {
          ...suggestions,
          contextPreferences: {
            ...preferences,
            [contextKey]: [...existing, commandName].slice(0, 10)
          }
        }
      };
    }
    return config;
  });
}
function getLastCommand() {
  const config = getGlobalConfig();
  return config.intelligentSuggestions?.lastCommand;
}
var CONTEXT_RECOMMENDATIONS;
var init_intelligentSuggestions = __esm(() => {
  init_config();
  init_commands();
  CONTEXT_RECOMMENDATIONS = {
    git: ["commit", "diff", "commit-push-pr", "branch"],
    node: ["audit", "init"],
    typescript: ["audit"],
    python: ["audit"],
    rust: ["audit"],
    go: ["audit"],
    docker: ["audit"]
  };
});

export { detectContext, recordCommandAssociation, getCommandAssociations, getContextBasedRecommendations, recordContextPreference, getLastCommand, init_intelligentSuggestions };

//# debugId=676D20ED20BAFB0964756E2164756E21
//# sourceMappingURL=chunk-8xjhgqt3.js.map
