export const CHROME_DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36';

export interface ChromeConfig {
    extensions?: Record<string, any>;
    extensionsPath?: string;
    profilePath?: string;
    profileUser?: string;
    debugPort?: number;
    disabledComponents?: string[];
    headlessArgs?: string[];
    dockerArgs?: string[];
    securityArgs?: string[];
    deterministicRenderingArgs?: string[];
}

export const DEFAULT_CHROME_CONFIG: ChromeConfig = {
    extensions: {},
    extensionsPath: 'chrome_extensions',
    profilePath: 'chrome_profile',
    profileUser: 'Default',
    debugPort: 9222,
    disabledComponents: [
        'Translate',
        'AcceptCHFrame',
        'OptimizationHints',
        'ProcessPerSiteUpToMainFrameThreshold',
        'InterestFeedContentSuggestions',
        'CalculateNativeWinOcclusion',
        'BackForwardCache',
        'HeavyAdPrivacyMitigations',
        'LazyFrameLoading',
        'ImprovedCookieControls',
        'PrivacySandboxSettings4',
        'AutofillServerCommunication',
        'CertificateTransparencyComponentUpdater',
        'DestroyProfileOnBrowserClose',
        'CrashReporting',
        'OverscrollHistoryNavigation',
        'InfiniteSessionRestore'
    ],
    headlessArgs: [
        '--headless=new',
        '--test-type',
        '--test-type=gpu'
    ],
    dockerArgs: [
        '--no-sandbox',
        '--disable-gpu-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--no-xshm'
    ],
    securityArgs: [
        '--disable-web-security',
        '--disable-site-isolation-trials',
        '--disable-features=IsolateOrigins,site-per-process',
        '--allow-running-insecure-content',
        '--ignore-certificate-errors',
        '--ignore-ssl-errors',
        '--ignore-certificate-errors-spki-list',
        '--allow-insecure-localhost'
    ],
    deterministicRenderingArgs: [
        '--deterministic-mode',
        '--js-flags=--random-seed=1157259159',
        '--force-device-scale-factor=1',
        '--hide-scrollbars',
        '--enable-webgl',
        '--font-render-hinting=none',
        '--force-color-profile=srgb',
        '--disable-partial-raster',
        '--disable-skia-runtime-opts',
        '--disable-2d-canvas-clip-aa',
        '--disable-lazy-loading',
        '--disable-renderer-backgrounding',
        '--disable-background-networking',
        '--disable-background-timer-throttling',
        '--disable-backgrounding-occluded-windows',
        '--disable-ipc-flooding-protection',
        '--disable-extensions-http-throttling',
        '--disable-field-trial-config',
        '--disable-back-forward-cache'
    ]
};

export function getChromeArgs(config: ChromeConfig = DEFAULT_CHROME_CONFIG): string[] {
    const args: string[] = [];

    // Add headless arguments if configured
    if (config.headlessArgs) {
        args.push(...config.headlessArgs);
    }

    // Add Docker-specific arguments if running in Docker
    if (config.dockerArgs) {
        args.push(...config.dockerArgs);
    }

    // Add security-related arguments
    if (config.securityArgs) {
        args.push(...config.securityArgs);
    }

    // Add deterministic rendering arguments
    if (config.deterministicRenderingArgs) {
        args.push(...config.deterministicRenderingArgs);
    }

    // Add any additional arguments from the config
    if (config.disabledComponents) {
        args.push('--disable-features=' + config.disabledComponents.join(','));
    }

    // Profile data dir setup
    if (config.profileUser) {
        args.push('--profile-directory=' + config.profileUser);
    }

    // Extensions
    args.push('--allow-legacy-extension-manifests');
    args.push('--allow-pre-commit-input');

    // Browser window and viewport setup
    args.push('--install-autogenerated-theme=0,0,0');

    // IO: stdin/stdout, debug port config
    args.push('--log-level=2');
    args.push('--enable-logging=stderr');
    args.push('--remote-debugging-address=0.0.0.0');
    args.push('--remote-debugging-port=' + config.debugPort);

    // Suppress first-run features, popups, hints, updates, etc.
    args.push('--no-pings');
    args.push('--no-first-run');
    args.push('--no-default-browser-check');
    args.push('--no-startup-window');
    args.push('--disable-default-apps');
    args.push('--ash-no-nudges');
    args.push('--disable-infobars');
    args.push('--disable-search-engine-choice-screen');
    args.push('--disable-session-crashed-bubble');
    args.push('--simulate-outdated-no-au="Tue, 31 Dec 2099 23:59:59 GMT"');
    args.push('--hide-crash-restore-bubble');
    args.push('--suppress-message-center-popups');
    args.push('--disable-client-side-phishing-detection');
    args.push('--disable-domain-reliability');
    args.push('--disable-component-update');
    args.push('--disable-datasaver-prompt');
    args.push('--disable-hang-monitor');
    args.push('--disable-session-crashed-bubble');
    args.push('--disable-speech-synthesis-api');
    args.push('--disable-speech-api');
    args.push('--disable-print-preview');
    args.push('--safebrowsing-disable-auto-update');
    args.push('--deny-permission-prompts');
    args.push('--disable-external-intent-requests');
    args.push('--disable-notifications');
    args.push('--disable-desktop-notifications');
    args.push('--noerrdialogs');
    args.push('--disable-popup-blocking');
    args.push('--disable-prompt-on-repost');
    args.push('--silent-debugger-extension-api');
    args.push('--block-new-web-contents');
    args.push('--metrics-recording-only');
    args.push('--disable-breakpad');

    return args;
}
