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


export const set_item = async (key, data) => {
  if (key && data) {
    try {
    await Keychain.setInternetCredentials(key, key, data);
    } catch (err) {
      console.log('Cannot save:', key, err);
    }
  }
};


export const get_item = async (key) => {
  try {
    const creds = await Keychain.getInternetCredentials(key);
    return creds ? creds.password : null;
  } catch (err) {
    console.log('Cannot get:', key, err);
    return null;
  }
};


// export const clear_item = async (key) => {
//   try {
//    await Keychain.resetInternetCredentials(key);
//     console.log('Credentials reset for:', key);
//   } catch (err) {
//     console.log('Cannot reset:', key, err);
//   }
// };

export const clear_item = async (key) => {
  try {
    await Keychain.resetInternetCredentials({
      server: key,   // 👈 yaha object dena zaroori hai
    });
    console.log('Credentials reset for:', key);
  } catch (err) {
    console.log('Cannot reset:', key, err);
  }
};


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