import { IDataStore, ISecureStore } from './stores'
import { getKeyForUser } from './auth'
import User from '../models/user'

export default class UsersStore {
  private dataStore: IDataStore
  private secureStore: ISecureStore

  private users: User[]

  public constructor(dataStore: IDataStore, secureStore: ISecureStore) {
    this.dataStore = dataStore
    this.secureStore = secureStore
    this.users = []
  }

  public getUsers(): ReadonlyArray<User> {
    // TODO: Should this be a copy/snapshot?
    return this.users
  }

  public addUser(user: User) {
    this.secureStore.setItem(getKeyForUser(user), user.login, user.token)

    this.users.push(user)

    this.save()
  }

  public loadFromStore() {
    const raw = this.dataStore.getItem('users')
    if (!raw || !raw.length) {
      return
    }

    const rawUsers: any[] = JSON.parse(raw)
    const usersWithTokens = rawUsers.map(user => {
      const userWithoutToken = new User(user.login, user.endpoint, '', user.email, user.avatarURL)
      return userWithoutToken.withToken(this.secureStore.getItem(getKeyForUser(userWithoutToken), user.login))
    })
    this.users = usersWithTokens
  }

  private save() {
    const usersWithoutTokens = this.users.map(user => user.withToken(''))
    this.dataStore.setItem('users', JSON.stringify(usersWithoutTokens))
  }
}

import { IDataStore, ISecureStore } from './stores'
import { getKeyForUser } from './auth'
import User from '../models/user'

export default class UsersStore {
  private dataStore: IDataStore
  private secureStore: ISecureStore

  private users: User[]

  public constructor(dataStore: IDataStore, secureStore: ISecureStore) {
    this.dataStore = dataStore
    this.secureStore = secureStore
    this.users = []
  }

  public getUsers(): ReadonlyArray<User> {
    return this.users.slice()
  }

  public addUser(user: User) {
    this.secureStore.setItem(getKeyForUser(user), user.login, user.token)

    this.users.push(user)

    this.save()
  }

  public loadFromStore() {
    const raw = this.dataStore.getItem('users')
    if (!raw || !raw.length) {
      return
    }

    const rawUsers: any[] = JSON.parse(raw)
    const usersWithTokens = rawUsers.map(user => {
      const userWithoutToken = new User(user.login, user.endpoint, '', user.email, user.avatarURL)
      return userWithoutToken.withToken(this.secureStore.getItem(getKeyForUser(userWithoutToken), user.login))
    })
    this.users = usersWithTokens
  }

  private save() {
    const usersWithoutTokens = this.users.map(user => user.withToken(''))
    this.dataStore.setItem('users', JSON.stringify(usersWithoutTokens))
  }
}

Copy the array before returning it
Copy the array before returning it
