import React from "react";
// import AsyncStorage from '@react-native-async-storage/async-storage';
import * as Keychain from 'react-native-keychain';

// const setProjectDataLocal = async (value) => {
//     try {
//       await AsyncStorage.setItem('', value)
//     } catch (e) {
//       // saving error
//     }
// }

export const set_item = async (key,data) => {
  if(key!='' && key!=null && data!='' && data!=null){    
    //Store the credentials
    // await Keychain.setGenericPassword(key, data);  
    //await Keychain.resetGenericPassword();
    await Keychain.setInternetCredentials(key, key, data)
    .catch((err)=>{
        console.log('cannot reset :',key);
    });    
  }  
};

export const get_item=async(key)=>{
    // Retrieve the credentials
    return await Keychain.getInternetCredentials(key)
    .then((res)=>{
        return res.password;
    })
    .catch((err)=>{
        console.log('cannot access value for :',key);
        return '';
    });
}

export const clear_item=async(key)=>{
    // Retrieve the credentials
    await Keychain.resetInternetCredentials(key)
    .catch((err)=>{
        console.log('cannot reset :',key);
    });
}

export const clear_all=async()=>{
    // Retrieve the credentials
    const allkeys=await Keychain.getAllGenericPasswordServices();
    console.log(allkeys);
}