// Monorepo-aware lint-staged configuration
module.exports = {
  // TypeScript files across all packages
  '**/*.{ts,tsx}': (filenames) => {
    const commands = [`eslint ${filenames.join(' ')} --fix --max-warnings=0`];

    // Only run tsc in packages that have tsconfig.json
    // NOTE: lint-staged passes absolute paths, so we need to match both:
    // - Relative: apps/api/src/file.ts
    // - Absolute: /home/user/project/apps/api/src/file.ts
    const packages = [
      ...new Set(
        filenames.map((f) => {
          const match = f.match(/(?:^|\/)(apps|packages)\/([^/]+)\//);
          return match ? `${match[1]}/${match[2]}` : null;
        }),
      ),
    ].filter(Boolean);

    packages.forEach((pkg) => {
      commands.push(`tsc --project ${pkg}/tsconfig.json --noEmit`);
    });

    return commands;
  },

  // Format all files
  '**/*.{js,jsx,ts,tsx,json,md,yml,yaml}': ['prettier --write'],
};
