import { readFile } from 'node:fs/promises';
import { readJsonConfig, writeJsonConfig } from './json-config.mjs';
import { c as resolve } from '../shared/ccjk.bQ7Dh1g4.mjs';
import '../shared/ccjk.RyizuzOI.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import './fs-operations.mjs';
import 'node:crypto';
import 'node:fs';

async function installRecommendedHooks(hookIds) {
  if (!hookIds.length)
    return 0;
  const templatesPath = resolve(
    import.meta.dirname ?? new URL(".", import.meta.url).pathname,
    "../data/hook-templates.json"
  );
  const raw = await readFile(templatesPath, "utf-8");
  const allTemplates = JSON.parse(raw);
  const selected = allTemplates.filter((t) => hookIds.includes(t.id));
  if (!selected.length)
    return 0;
  const settings = readJsonConfig("settings") ?? {};
  const hooks = settings.hooks ?? {};
  let added = 0;
  for (const tpl of selected) {
    const event = tpl.event;
    const existing = hooks[event] ?? [];
    const alreadyExists = existing.some((h) => h.command === tpl.command);
    if (alreadyExists)
      continue;
    const entry = {
      type: "command",
      command: tpl.command
    };
    if (tpl.timeout) {
      entry.timeout = tpl.timeout;
    }
    hooks[event] = [...existing, entry];
    added++;
  }
  if (added > 0) {
    settings.hooks = hooks;
    writeJsonConfig("settings", settings);
  }
  return added;
}

export { installRecommendedHooks };
