import { CliRuntime } from './classes.cliruntime.js';

export * from './classes.cliruntime.js';
export * from './commands.discover.js';
export * from './commands.inspect.js';
export * from './commands.list.js';
export * from './commands.setup.js';

export const runCli = async (argvArg = process.argv.slice(2)) => {
  const runtime = new CliRuntime();
  const [commandArg = 'list', integrationArg] = argvArg;

  if (commandArg === 'list') {
    console.log(await runtime.list());
    return;
  }

  if (commandArg === 'inspect') {
    console.log(await runtime.inspect(integrationArg || 'hue'));
    return;
  }

  if (commandArg === 'discover') {
    console.log(JSON.stringify(await runtime.discover(), null, 2));
    return;
  }

  if (commandArg === 'setup') {
    console.log(JSON.stringify(await runtime.setup(integrationArg || 'hue'), null, 2));
    return;
  }

  throw new Error(`Unknown integrations CLI command: ${commandArg}`);
};

if (process.argv[1]?.endsWith('/cli.js') || process.argv[1]?.endsWith('/cli.ts.js')) {
  runCli().catch((errorArg) => {
    console.error(errorArg.message);
    process.exit(1);
  });
}
