import * as path from 'node:path';
import { resolveDataDir } from '../../../../comm/data-dir.js';

export interface ResolveBrowserDataDirOpts {
  isPkg?: boolean;
  execDir?: string;
  monorepoRoot?: string;
}

export function resolveBrowserDataDir(opts: ResolveBrowserDataDirOpts = {}): string {
  if (process.env.NETA_BROWSER_DATA_DIR) return path.resolve(process.env.NETA_BROWSER_DATA_DIR);
  const isPkg = opts.isPkg ?? !!(process as any).pkg;
  if (isPkg) {
    const dataDir = resolveDataDir({ isPkg: true, execDir: opts.execDir });
    return path.join(dataDir, '.netabrowser');
  }
  const monorepoRoot = opts.monorepoRoot
    ?? path.resolve(__dirname, '../../../../../../../');
  return path.join(monorepoRoot, '.netabrowser-data');
}

export function getProfileDir(sessionName: string): string {
  return path.join(resolveBrowserDataDir(), 'profiles', sessionName);
}

export function getStateDir(sessionName: string): string {
  return path.join(resolveBrowserDataDir(), 'states', `${sessionName}.json`);
}
