import AsyncStorage from '@react-native-async-storage/async-storage';

export const saveAsyncStorage = async (name: string, value: string) => {
  await AsyncStorage.setItem(name, value);
};

export const getAsyncStorage = (name: string) => {
  return AsyncStorage.getItem(name);
};

export const deleteAsyncStorage = async (name: string) => {
  await AsyncStorage.removeItem(name);
};

/**
 * Lang qiymatini AsyncStorage ga saqlaydi
 * @param lang 'uz' | 'ru' | 'en'
 */
export const saveLang = async (lang: string) => {
  try {
    await AsyncStorage.setItem('lang', lang);
    console.log(`Language saved: ${lang}`);
  } catch (error) {
    console.error('Failed to save language', error);
  }
};

/**
 * AsyncStorage dan lang qiymatini oladi
 * Agar mavjud bo'lmasa, null qaytaradi
 */
export const getLang = async (): Promise<string | null> => {
  try {
    const lang = await AsyncStorage.getItem('lang');
    return lang;
  } catch (error) {
    console.error('Failed to get language', error);
    return null;
  }
};

/**
 * AsyncStorage dan lang ni o'chiradi
 */
export const deleteLang = async () => {
  try {
    await AsyncStorage.removeItem('lang');
  } catch (error) {
    console.error('Failed to delete language', error);
  }
};

const ACCESS = 'access_token';
const REFRESH = 'refresh_token';

export const setToken = (token: string) => AsyncStorage.setItem(ACCESS, token);

export const getToken = async () => AsyncStorage.getItem(ACCESS);

export const removeToken = () => AsyncStorage.removeItem(ACCESS);

export const setRefToken = (token: string) => AsyncStorage.setItem(REFRESH, token);

export const getRefToken = async () => AsyncStorage.getItem(REFRESH);

export const removeRefToken = () => AsyncStorage.removeItem(REFRESH);
