text: import { Alert, Platform } from "react-native";
  import IAP from "react-native-iap";
  import {
    storeData,
    getLatestSubscriptionTicket,
    requestSubscription,
  } from "constants";
  
  import { GET, POST } from "./methods";
  import { LOGIN_FAILED } from "./errors";
  
  const renewSubscription = async () => {
    await IAP.initConnection();
    if (Platform.OS === "ios") await IAP.clearTransactionIOS();
    const receipt = await getLatestSubscriptionTicket();
    let subscriptionResult = await POST({
      path: "/subscribe",
      body: {
        receipt,
      },
    });
  
    if (subscriptionResult.status === 200) return subscriptionResult;
  
    const renewedReceipt = await requestSubscription();
  
    if (!renewedReceipt)
      return Alert.alert("Echec du renouvellement de l'abonnement");
    subscriptionResult = await POST({
      path: "/subscribe",
      body: {
        receipt: renewedReceipt,
      },
    });
  
    if (subscriptionResult.status !== 200) {
      return Alert.alert("Echec du renouvellement de l'abonnement");
    }
  
    return subscriptionResult;
  };
  
  export const login = async ({ email, password }) => {
    if (!email || !password) {
      return Alert.alert("Echec de la connexion", LOGIN_FAILED, [
        {
          title: "ok",
        },
      ]);
    }
  
    let result = await POST({
      path: "/login",
      body: {
        email,
        password,
        platform: Platform.OS,
      },
    });
  
    switch (result.status) {
      case 404: {
        return Alert.alert("Echec de la connexion", LOGIN_FAILED, [
          {
            title: "ok",
          },
        ]);
      }
  
      case 403: {
        // L'abonnement a expire
        result = await renewSubscription();
        if (!result.ok)
          return Alert.alert(
            "Echec de la connexion",
            "Une erreur inconnue est survenue lors de l'abonnement.",
            [
              {
                title: "ok",
              },
            ]
          );
      }
    }
  
    await storeData("userAuthentificationData", { email, password });
    const { data } = await result.json();
    return data;
  };
  
  export const register = async ({
    lastname,
    firstname,
    email,
    phoneNumber,
    password,
  }) => {
    let result = await POST({
      path: "/register",
      body: {
        lastname,
        firstname,
        phoneNumber,
        email,
        password,
      },
    });
  
    switch (result.status) {
      case 422:
        return Alert.alert(
          "Echec de l'inscription",
          "Veuillez vérifier le contenu des champs et réessayer.",
          [{ title: "ok" }]
        );
  
      case 409:
        return Alert.alert(
          "Echec de l'inscription",
          "Un compte avec cet e-mail existe déjà.",
          [{ title: "ok" }]
        );
  
      case 200: {
        result = await renewSubscription();
        if (!result.ok)
          return Alert.alert("Attention !", "Compte créé sans abonnement", [
            { title: "ok" },
          ]);
      }
    }
  
    await storeData("userAuthentificationData", { email, password });
    const { data } = await result.json();
    return data;
  };
  
  export const disconnect = async () => {
    await GET({
      path: "/disconnect",
    });
  
    return true;
  };
  