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

import { TOKEN_STORAGE } from './storageConfig';

type AuthTokenStorageProps = {
  token: string;
  refresh_token?: string;
};

export async function storageAuthTokenSave({ token, refresh_token }: AuthTokenStorageProps) {
  await AsyncStorage.setItem(TOKEN_STORAGE, JSON.stringify({ token, refresh_token }));
}

export async function storageAuthTokenGet() {
  const response = await AsyncStorage.getItem(TOKEN_STORAGE);
  const { token, refresh_token }: AuthTokenStorageProps = response ? JSON.parse(response) : {};
  return { token, refresh_token };
}

export async function storageAuthTokenRemove() {
  await AsyncStorage.removeItem(TOKEN_STORAGE);
}
