import { Injectable } from '@angular/core'
import { localstoreService } from './localstore.service'
import { SHA1 }  from  'crypto-js'

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

  // main data
  private _document = {}
  // local storage keyName
  private keyName: string;

  constructor() {

    this.keyName = (SHA1(this.constructor.name+ 'view-publication-list/local')).toString()

    setTimeout(()=>{
      let restore = localstoreService.get(this.keyName, {})
      this._document = restore.document || {}
    }, 10)

  }

  get documents() {
    return this._document
  }

  getDocument(folderId) {
    return this._document[folderId]
  }


  add(folderId, document = []) {
    this._document[folderId] = document

 

    setTimeout(()=> {
      localstoreService.set(this.keyName, {
        document: this._document
      })
    }, 100)
  }

}


export  const PublicationListStorage =  new PublicationListService()
