import { Parser } from 'xml2js';
import axios from 'axios';

const getAadharAddressFromXml = async (url: string) => {
  try {
    const response = await axios.get<string>(url);
    const xml = response.data;

    const parser = new Parser({ explicitArray: false });
    const result = await parser.parseStringPromise(xml);

    const poa = result?.Certificate?.CertificateData?.KycRes?.UidData?.Poa?.$;
    if (!poa) return false;

    const addressParts = [
      poa.co,
      poa.house,
      poa.street,
      poa.lm,
      poa.loc,
      poa.vtc,
      poa.dist,
      poa.po,
      poa.state,
      poa.pc,
    ].filter(Boolean); // Removes undefined or empty values

    return addressParts.join(', ');

  } catch (error: any) {
    return false;
  }
};

export default getAadharAddressFromXml;
