import { writable } from 'svelte/store';
import * as api from './api';

const { subscribe, set } = writable<boolean>(false);

export const loggedIn = {
  subscribe,
  fetchIsLoggedIn(): Promise<void> {
    return api.isLoggedIn().then((isLoggedIn: boolean) => {
      set(isLoggedIn);
    });
  },
  fetchLogin(credentials: ICredentials): Promise<void> {
    return api.login(credentials).then(() => {
      return this.fetchIsLoggedIn();
    });
  },
};
