import services from '../../../../constants/endpoints.json';
import { trimXML } from '../../helpers/createXMLS';
import { getParam, request } from '../../helpers/request';
import { ExportPersonResponse } from './type';

const parseString = require('react-native-xml2js').parseString;

export class ReportsAPI {
  private config = services.reports;

  async exportPersonGet(token: string, exportId: string, format: string) {
    const attributes = [
      { name: 'ID_Login', value: token },
      { name: 'ID', value: exportId },
      { name: 'FileFormat', value: format },
    ];
    return request(this.config.name, this.config.entities.exportPerson, attributes).then(data => {
      let res: ExportPersonResponse | undefined;
      parseString(trimXML(data), (_err: any, result: any) => {
        const item = result.ExportPersonResponse.ExportPersonResult[0];
        res = {
          content: getParam(item, 'Content'),
          fileName: getParam(item, 'FileName'),
          format,
        };
      });
      return res;
    });
  }
}
