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

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

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

  constructor() {

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

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

  }

  get list() {
    return this._list
  }

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

    this.save(this._list)
  }

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


export const PublicationTravelFolderStore = new PublicationTravelFolderService() 