import services from '../../../../constants/endpoints.json';
import { trimXML } from '../../helpers/createXMLS';
import { request } from '../../helpers/request';
import { DocumentDetailDataResponse } from './types';

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

export class DocumentStorageAPI {
  private config = services.documentStorage;

  async documentDetailDataGet(token: string, documentId: string) {
    const attributes = [
      { name: 'ID_Login', value: token },
      { name: 'ID', value: documentId },
    ];
    return request(this.config.name, this.config.entities.documentDetailData, attributes).then(
      data => {
        let res: DocumentDetailDataResponse | undefined;
        parseString(trimXML(data), (_err: any, result: any) => {
          const item = result.DocumentDetailDataResponse.DocumentDetailDataResult[0];
          res = {
            id: documentId,
            documentContent: item,
          };
        });
        return res;
      },
    );
  }
}
