import { mutate } from 'swr' ; import { SetOptions } from '@firebase/firestore-types' ; import { fuego } from '../context' ; import { empty } from '../helpers/empty' ; import { collectionCache } from '../classes/Cache' ; import { Document } from '../types/Document' ; /**
 * Function that, when called, refreshes all queries that match this document path.
 *
 * This can be useful for a pull to refresh that isn't on the same screen as the `useCollection` hook, for example.
 */ const revalidateDocument = ( path ) => { return mutate ( path ) ; } ; /**
 * Function that, when called, refreshes all queries that match this document path.
 *
 * This can be useful for a pull to refresh that isn't on the same screen as the `useCollection` hook, for example.
 */ const revalidateCollection = ( path ) => { const promises = [ ] ; collectionCache . getSWRKeysFromCollectionPath ( path ) . forEach ( key => { promises . push ( mutate ( key ) ) ; } ) ; return Promise . all ( promises ) ; } ; const set = < Data extends object = { } , Doc extends Document = Document < Data > > ( path , data , options ? , /**
 * If true, the local cache won't be updated. Default `false`.
 */ ignoreLocalMutation = false ) => { if ( path === null ) return null ; const isDocument = path . trim ( ) . split ( '/' ) . filter ( Boolean ) . length % 2 === 0 ; if ( ! isDocument ) throw new Error ( `[@nandorojo/swr-firestore] error: called set() function with path: ${ path }. This is not a valid document path. 
      
data: ${ JSON . stringify ( data ) }` ) ; if ( ! ignoreLocalMutation ) { mutate ( path , ( prevState = empty . object ) => { if ( ! options ?. merge ) return data ; return { ... prevState , ... data , } ; } , false ) ; } let collection = path . split ( `/` ) . filter ( Boolean ) ; const docId = collection . pop ( ) ; collection = collection . join ( '/' ) ; collectionCache . getSWRKeysFromCollectionPath ( collection ) . forEach ( key => { mutate ( key , ( currentState = empty . array ) => { if ( ! currentState . some ( doc => doc . id === docId ) ) { return currentState ; } return currentState . map ( ( document = empty . object as Doc ) => { if ( document . id === docId ) { if ( ! options ?. merge ) return document ; return { ... document , ... data } ; } return document ; } ) ; } , false ) ; } ) ; return fuego . db . doc ( path ) . set ( data , options ) ; } ; const update = < Data extends object = { } , Doc extends Document = Document < Data > > ( path , data , /**
 * If true, the local cache won't be updated. Default `false`.
 */ ignoreLocalMutation = false ) => { if ( path === null ) return null ; const isDocument = path . trim ( ) . split ( '/' ) . filter ( Boolean ) . length % 2 === 0 ; if ( ! isDocument ) throw new Error ( `[@nandorojo/swr-firestore] error: called update function with path: ${ path }. This is not a valid document path. 
      
data: ${ JSON . stringify ( data ) }` ) ; if ( ! ignoreLocalMutation ) { mutate ( path , ( prevState = empty . object ) => { return { ... prevState , ... data , } ; } , false ) ; } let collection = path . split ( `/` ) . filter ( Boolean ) ; const docId = collection . pop ( ) ; collection = collection . join ( '/' ) ; collectionCache . getSWRKeysFromCollectionPath ( collection ) . forEach ( key => { mutate ( key , ( currentState = empty . array ) : Doc [ ] => { if ( ! currentState . some ( doc => doc . id === docId ) ) { return currentState ; } return currentState . map ( ( document = empty . object as Doc ) => { if ( document . id === docId ) { return { ... document , ... data } ; } return document ; } ) ; } , false ) ; } ) ; return fuego . db . doc ( path ) . update ( data ) ; } ; const deleteDocument = < Data extends object = { } , Doc extends Document = Document < Data > > ( path , /**
 * If true, the local cache won't be updated immediately. Default `false`.
 */ ignoreLocalMutation = false ) => { if ( path === null ) return null ; const isDocument = path . trim ( ) . split ( '/' ) . filter ( Boolean ) . length % 2 === 0 ; if ( ! isDocument ) throw new Error ( `[@nandorojo/swr-firestore] error: called delete() function with path: ${ path }. This is not a valid document path.` ) ; if ( ! ignoreLocalMutation ) { mutate ( path , null , false ) ; let collection = path . split ( `/` ) . filter ( Boolean ) ; const docId = collection . pop ( ) ; collection = collection . join ( '/' ) ; collectionCache . getSWRKeysFromCollectionPath ( collection ) . forEach ( key => { mutate ( key , ( currentState = empty . array ) => { if ( ! currentState . some ( doc => doc && doc . id === docId ) ) { return currentState ; } return currentState . filter ( document => { if ( ! document ) return false ; if ( document . id === docId ) { return false ; } return true ; } ) ; } , false ) ; } ) ; } return fuego . db . doc ( path ) . delete ( ) ; } ; export { set , update , revalidateDocument , revalidateCollection , deleteDocument } ;