import { Injectable } from 'angular-ts-decorators' ; import autobind from 'autobind-decorator' ; import { BookmarkMappingNotFoundError } from '../../../shared/errors/errors' ; import { StoreKey } from '../../../shared/store/store.enum' ; import { StoreService } from '../../../shared/store/store.service' ; import { BookmarkIdMapping } from './bookmark-id-mapper.interface' ; @ autobind @ Injectable ( 'BookmarkIdMapperService' ) export class BookmarkIdMapperService { $q : ng . IQService ; storeSvc : StoreService ; static $inject = [ '$q' , 'StoreService' ] ; constructor ( $q , StoreSvc ) { this . $q = $q ; this . storeSvc = StoreSvc ; } add ( newMappings ) { const newMappingsArr = Array . isArray ( newMappings ) ? newMappings : [ newMappings ] ; return this . storeSvc . get < BookmarkIdMapping [ ] > ( StoreKey . BookmarkIdMappings ) . then ( ( idMappings ) => { return idMappings . concat ( newMappingsArr ) ; } ) . then ( ( updatedMappings ) => { return this . set ( updatedMappings ) ; } ) ; } clear ( ) { return this . storeSvc . remove ( StoreKey . BookmarkIdMappings ) ; } createMapping ( syncedId , nativeId ? ) { return { nativeId , syncedId } ; } get ( nativeId , syncedId ? ) { return this . storeSvc . get < BookmarkIdMapping [ ] > ( StoreKey . BookmarkIdMappings ) . then ( ( idMappings ) => { let mapping ; if ( nativeId != null ) { mapping = idMappings . find ( ( x ) => { return x . nativeId === nativeId ; } ) ; } else if ( syncedId != null ) { mapping = idMappings . find ( ( x ) => { return x . syncedId === syncedId ; } ) ; } return mapping ; } ) ; } remove ( syncedIds , nativeIds ? ) { const syncedIdsArr = syncedIds != null ? ( Array . isArray ( syncedIds ) ? syncedIds : [ syncedIds ] ) : null ; const nativeIdsArr = nativeIds != null ? ( Array . isArray ( nativeIds ) ? nativeIds : [ nativeIds ] ) : null ; return this . storeSvc . get < BookmarkIdMapping [ ] > ( StoreKey . BookmarkIdMappings ) . then ( ( idMappings ) => { const idMappingsLessSynced = syncedIdsArr == null ? idMappings : syncedIdsArr . reduce ( ( acc , val ) => { const indexToRemove = acc . findIndex ( ( x ) => { return x . syncedId === val ; } ) ; if ( indexToRemove < 0 ) { throw new Error ( 'Bookmark ID mapping to remove could not be determined' ) ; } return acc . filter ( ( x , index ) => { return index !== indexToRemove ; } ) ; } , idMappings ) ; const idMappingsLessNative = nativeIdsArr == null ? idMappingsLessSynced : nativeIdsArr . reduce ( ( acc , val ) => { const indexToRemove = acc . findIndex ( ( x ) => { return x . nativeId === val ; } ) ; if ( indexToRemove < 0 ) { throw new Error ( 'Bookmark ID mapping to remove could not be determined' ) ; } return acc . filter ( ( x , index ) => { return index !== indexToRemove ; } ) ; } , idMappingsLessSynced ) ; return this . set ( idMappingsLessNative ) ; } ) . catch ( ( err ) => { throw new BookmarkMappingNotFoundError ( undefined , err ) ; } ) ; } set ( idMappings ) { const sortedMappings = idMappings . sort ( ( x , y ) => { return x . syncedId - y . syncedId ; } ) ; return this . storeSvc . set ( StoreKey . BookmarkIdMappings , sortedMappings ) ; } }