import React, { Component } from 'react';
import { readUser } from './AuthApi'

var DBEvents = require('react-native-db-models').DBEvents;
var db = require('./persistences/db.js');
import I18n from 'react-native-i18n'
import { isDebugMode, adhesionUrl, locationActionUrl, memberActionUrl, networkActionUrl, demandeActionUrl } from "./IlinkConstants"

var serializeJSON = function (data) {
     return Object.keys(data).map(function (keyName) {
          return encodeURIComponent(keyName) + '=' + encodeURIComponent(data[keyName])
     }).join('&');
}

export const listAllMembers = async () => {
     const user = await readUser()
     if (user !== null) {
          let la = I18n.currentLocale()
          let data = {
               "tag": "member",
               "id": user.code_membre,
               la,
               "test": isDebugMode,
               "codeparrain": user.code_membre
               , "type": 'all_geolocated'
          };

          console.log("DATA to SEND", data);

          const response = await fetch(memberActionUrl, {
               method: 'POST',
               headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
               },
               body: JSON.stringify(data),
          })
          let responseText = ""
          try {
               responseText = await response.text()
               return JSON.parse(responseText)
          } catch (e) {
               console.log(e)
               console.log("error when liste member", responseText)
               let er = {
                    error: -3,
                    content: e,
                    text: responseText
               }
               return er;
          }

     }
     return null
}
