import UserService from 'core/services/UserService';
import { makeAutoObservable } from 'mobx';
import { __getUserFromLocalStorage } from './lib/user';

const UserStore = makeAutoObservable({
  async init() {
    let user;
    if (process.env.NODE_ENV === 'development') {
      user = __getUserFromLocalStorage();
    } else {
      user = await UserService.fetchUser();
    }
    this.setUser(user);
  },
  user: undefined,
  setUser(user) {
    this.user = user;
  },
  getDomainName() {
    if (!this.user) return '';
    const { username, domain } = this.user;
    return `${domain}\\${username}`;
  },
});

export default UserStore;
