import type { ParsedNode } from './ss.js';

export function parseVMess(uri: string): ParsedNode {
  const b64 = uri.replace('vmess://', '');
  const json = JSON.parse(Buffer.from(b64, 'base64').toString());

  const name = json.ps || 'VMess';
  const server = json.add;
  const port = parseInt(json.port, 10);
  const uuid = json.id;

  let line = `${name} = vmess, ${server}, ${port}, username=${uuid}, vmess-aead=true`;

  if (json.tls === 'tls') {
    line += ', tls=true';
    if (json.sni) line += `, sni=${json.sni}`;
    line += ', skip-cert-verify=false';
  }

  if (json.net === 'ws') {
    line += ', ws=true';
    if (json.path) line += `, ws-path=${json.path}`;
    if (json.host) line += `, ws-headers=Host:${json.host}`;
  }

  return { name, type: 'vmess', server, port, surgeLine: line };
}
