#!/usr/bin/env node
'use strict';

const path = require('path');

const COMMANDS = {
  read: {
    module: './scripts/browser-read',
    description: '读取任意网页正文内容',
  },
  interact: {
    module: './scripts/browser-interact',
    description: 'DOM 交互操作（click / fill / scroll / wait）',
  },
};

function printUsage() {
  console.log('\njs-browser-ops-skill - 通用浏览器操作工具');
  console.log('='.repeat(50));
  console.log('\n使用方法:');
  console.log('  node index.js <command> [args...] [options]\n');
  console.log('命令:');
  for (const [command, info] of Object.entries(COMMANDS)) {
    console.log(`  ${command.padEnd(12)} ${info.description}`);
  }
  console.log('\n常用选项:');
  console.log('  --recording-mode off|history|standard|debug');
  console.log('  --debug-recording');
  console.log('  --no-cache');
  console.log('  --recording-base-dir /absolute/path');
  console.log('  --run-id custom-id');
}

async function main() {
  const args = process.argv.slice(2);
  const command = args[0];

  if (!command || command === '--help' || command === '-h') {
    printUsage();
    return;
  }

  const commandInfo = COMMANDS[command];
  if (!commandInfo) {
    throw new Error(`未知命令: ${command}`);
  }

  const originalArgv = [...process.argv];
  process.argv = [process.argv[0], path.join(__dirname, 'index.js'), ...args.slice(1)];

  try {
    const scriptModule = require(commandInfo.module);
    await scriptModule.main();
  } finally {
    process.argv = originalArgv;
  }
}

if (require.main === module) {
  main().catch((error) => {
    console.error(error.message);
    process.exit(1);
  });
}

module.exports = { main };
