import { Preferences } from '@capacitor/preferences';

const REFRESH_TOKEN_KEY = 'auth_refresh_token';

export const useAuthStorage = () => {
  const setRefreshToken = async (token) => {
    if (!token) {
      await Preferences.remove({ key: REFRESH_TOKEN_KEY });
      return;
    }

    await Preferences.set({
      key: REFRESH_TOKEN_KEY,
      value: token,
    });
  };

  const getRefreshToken = async () => {
    const { value } = await Preferences.get({ key: REFRESH_TOKEN_KEY });
    return value;
  };

  const clearRefreshToken = async () => {
    await Preferences.remove({ key: REFRESH_TOKEN_KEY });
  };

  return {
    setRefreshToken,
    getRefreshToken,
    clearRefreshToken,
  };
};
