import * as path from 'node:path';

export interface ResolveOpts {
  isPkg?: boolean;
  execPath?: string;
}

export function resolveChromiumPath(opts: ResolveOpts = {}): string {
  if (process.env.NETA_CHROMIUM_PATH) return process.env.NETA_CHROMIUM_PATH;
  const isPkg = opts.isPkg ?? !!(process as any).pkg;
  if (isPkg) {
    const execPath = opts.execPath ?? process.execPath;
    return path.join(path.dirname(execPath), 'chromium', 'win64', 'chrome.exe').replace(/\\/g, '/');
  }
  // dev 模式：编译后 __dirname = packages/backend/dist/modules/netaclaw/browser-daemon/runtime
  // 上溯 7 层到 monorepo root，再进 packages/netabrowser-cli
  return path.resolve(__dirname, '../../../../../../../packages/netabrowser-cli/chromium/win64/chrome.exe').replace(/\\/g, '/');
}

export interface LaunchArgsOpts {
  fingerprintArgs: string[];
  extraArgs?: string[];
}

export function buildLaunchArgs(opts: LaunchArgsOpts): string[] {
  return [
    '--lang=zh-CN',
    '--accept-lang=zh-CN,zh,en-US,en',
    '--disable-infobars',
    ...opts.fingerprintArgs,
    ...(opts.extraArgs ?? []),
  ];
}
