import { Injectable } from '@angular/core';
import { StorageService } from 'src/app/core/services/storage.service';
import { User } from '../models/user';
import { StoreInstanceService } from './store-instance.service';
const initialState = {
  user: {} as User,
  accesKey: '',
};

@Injectable({
  providedIn: 'root',
})
export class UserStore extends StoreInstanceService<typeof initialState> {
  constructor(public storage: StorageService) {
    super(initialState);

    if (this.storage.getObject('znak'))
      this.setUser(this.storage.getObject('znak'));
  }

  public setUser(user: any) {
    this.update((state) => {
      return { ...state, user };
    });
  }

  public setSessionUserExpired() {
    this.update((state) => {
      return { ...state, user: { ...state.user, isExpired: true } };
    });
  }

  public resetUser(user: User) {
    this.update((state) => {
      return { ...state, user };
    });
  }

  public updateAccesKey(key: string) {
    this.update((state) => {
      return { ...state, accesKey: key };
    });
  }
}
