import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { AES, enc, SHA1 }  from  'crypto-js'
import { Publication } from 'src/app/models/publication';

@Injectable({
  providedIn: 'root'
})
export class PresidentialActionsStoreService {

  // main data
  private _list: Publication[] = []
  // local storage keyName
  private keyName: string; 

  constructor() {

    this.keyName = (SHA1(this.constructor.name+ 'presidentialActions/local')).toString()

    setTimeout(()=>{
      let restore = localstoreService.get(this.keyName, [])
      this._list = restore.list
    }, 10)

  }

  get list() {
    return this._list
  }

  reset(list: Publication[]) {
    this._list = list

    this.save(this._list)
  }

  private save(list: Publication[]) {
    setTimeout(()=> { 
      localstoreService.set(this.keyName, {
        list: this._list
      })
    }, 10)
  }

}


export const PresidentialActionsStore = new PresidentialActionsStoreService() 