import { Glob } from 'bun';
import { join } from 'node:path';
import type { CommandHandler } from './command-handler';
import type { ApplicationCommandStructure } from '@projectdysnomia/dysnomia';

export async function importCommands(
  pattern: string = '**/*.command.{js,ts}',
  baseDir: string = join(process.cwd(), 'src'),
  commandRegistry: Record<string, CommandHandler<ApplicationCommandStructure>> = {},
): Promise<Record<string, CommandHandler<ApplicationCommandStructure>>> {
  const glob = new Glob(pattern);

  for await (const file of glob.scan({ cwd: baseDir, absolute: true })) {
    const command = (await import(file)).default as CommandHandler<ApplicationCommandStructure>;
    commandRegistry[command.definition.name] = command;
  }

  return commandRegistry;
}
