/**
 * React Native SQLite Demo
 * Copyright (c) 2018-2020 Bruce Lefebvre <bruce@brucelefebvre.com>
 * https://github.com/blefebvre/react-native-sqlite-demo/blob/master/LICENSE
 */ import { Database } from "./Database" ; import { List } from "../types/List" ; import { ListItem } from "../types/ListItem" ; let lists = [ ] as List [ ] ; let listIdIndex = 0 ; type ListItemMap = { [ key ] : ListItem [ ] ; } ; let listItemsMap = { } ; let listItemIdIndex = 0 ; async function createList ( newListTitle ) { const newList = { title : newListTitle , id : listIdIndex ++ } ; listItemsMap = { ... listItemsMap , [ newList . id ] : [ ] } ; lists = [ ... lists , newList ] ; } async function addListItem ( text , list ) { const newListItem = { text , done : false , id : listItemIdIndex ++ , listId : list . id } ; const listItemsForList = listItemsMap [ list . id ] ; const updatedListItemsForList = [ ... listItemsForList , newListItem ] ; listItemsMap = { ... listItemsMap , [ list . id ] : updatedListItemsForList } ; } async function getAllLists ( ) { return lists ; } async function getListItems ( list , doneItemsLast ) { console . log ( "List:" , list , "List items:" , listItemsMap [ list . id ] ) ; return listItemsMap [ list . id ] ; } async function updateListItem ( listItem ) { if ( listItem . listId !== undefined ) { const listItemsForList = listItemsMap [ listItem . listId ] ; const updatedListItemsForList = listItemsForList . map ( ( currentItem ) => { if ( currentItem . id === listItem . id ) { return listItem ; } else { return currentItem ; } } ) ; listItemsMap = { ... listItemsMap , [ listItem . listId ] : updatedListItemsForList } ; } } async function deleteList ( listToDelete ) { lists = lists . filter ( ( list ) => list . id !== listToDelete . id ) ; } export const inMemoryDatabase = { createList , addListItem , getAllLists , getListItems , updateListItem , deleteList , } ;