#!/usr/bin/env node
import { createHook } from './_runtime.mjs';

const BLOCKED_DIRS = [
  'node_modules/',
  '.git/objects/',
  'dist/',
  'build/',
  'vendor/',
  '.next/',
  'coverage/',
  '__pycache__/',
  '.tox/',
  'target/debug/',
  'target/release/',
  '.gradle/',
  'Pods/',
];

const normalize = (value) =>
  String(value ?? '')
    .replace(/\\/g, '/')
    .toLowerCase();

/** Blocks reads and searches inside generated or dependency-heavy directories. */
export const scoutGuard = async (context) => {
  if (context.event !== 'PreToolUse') return { decision: 'allow' };
  const blockedDir = context.filePaths.reduce((match, filePath) => {
    if (match) return match;
    const normalizedPath = normalize(filePath);
    return (
      BLOCKED_DIRS.find(
        (dir) => normalizedPath.startsWith(dir) || normalizedPath.includes(`/${dir}`),
      ) ?? ''
    );
  }, '');
  return blockedDir
    ? {
        decision: 'deny',
        reason: `Blocked: accessing ${blockedDir} wastes context. Use search tools or package documentation instead.`,
      }
    : { decision: 'allow' };
};

createHook(scoutGuard);
