export interface PwCookie {
  name: string;
  value: string;
  domain: string;
  path: string;
  /** Unix seconds; -1 for a session cookie. */
  expires: number;
  secure?: boolean;
  httpOnly?: boolean;
  sameSite?: string;
}

export interface StorageState {
  cookies: PwCookie[];
}

/** Convert a Playwright storageState into a Netscape cookies.txt for yt-dlp. */
export function playwrightToNetscape(state: StorageState): string {
  const lines = ['# Netscape HTTP Cookie File'];
  for (const c of state.cookies) {
    const includeSub = c.domain.startsWith('.') ? 'TRUE' : 'FALSE';
    const secure = c.secure ? 'TRUE' : 'FALSE';
    const expiry = c.expires && c.expires > 0 ? Math.floor(c.expires) : 0;
    lines.push([c.domain, includeSub, c.path || '/', secure, String(expiry), c.name, c.value].join('\t'));
  }
  return `${lines.join('\n')}\n`;
}
