import { RepoDocInfo } from '../RepoDocInfo' ; import { RepoDocInfos } from '../RepoDocInfos' ; import { Optional } from 'polar-shared/src/util/ts/Optional' ; import { Tag } from 'polar-shared/src/tags/Tags' ; import { Strings } from "polar-shared/src/util/Strings" ; import { TagMatcher2 } from "../../../../web/js/tags/TagMatcher2" ; /**
 * Keeps track of the doc index so that we can filter it in the UI and have
 * the filter events send to the index and then update component state directly.
 */ export namespace DocRepoFilters2 { export interface Filter { /**
         * When true, only show flagged documents.
         */ readonly flagged ? : boolean ; /**
         *  When true, show both archived and non-archived documents.
         */ readonly archived ? : boolean ; readonly title ? : string ; readonly tags ? : ReadonlyArray < Tag > ; } export function execute ( repoDocInfos , filter ) { repoDocInfos = doFilterValid ( repoDocInfos , filter ) ; repoDocInfos = doFilterByTitle ( repoDocInfos , filter ) ; repoDocInfos = doFilterFlagged ( repoDocInfos , filter ) ; repoDocInfos = doFilterArchived ( repoDocInfos , filter ) ; repoDocInfos = doFilterByTags ( repoDocInfos , filter ) ; return repoDocInfos ; } function doFilterValid ( repoDocs , filter ) { return repoDocs . filter ( current => RepoDocInfos . isValid ( current ) ) ; } function doFilterByTitle ( repoDocs , filter ) { if ( ! Strings . empty ( filter . title ) ) { const toSTR = ( value ) : string => { return Optional . of ( value ) . getOrElse ( "" ) . toLowerCase ( ) ; } ; const searchString = toSTR ( filter . title ) ; return repoDocs . filter ( current => { const title = toSTR ( current . title ) ; const filename = toSTR ( current . filename ) ; return title . includes ( searchString ) || filename . includes ( searchString ) ; } ) ; } return repoDocs ; } function doFilterFlagged ( repoDocs , filter ) { if ( filter . flagged ) { return repoDocs . filter ( current => current . flagged ) ; } return repoDocs ; } function doFilterArchived ( repoDocs , filter ) { if ( ! filter . archived ) { return repoDocs . filter ( current => ! current . archived ) ; } return repoDocs ; } function doFilterByTags ( repoDocs , filter ) { if ( ! filter . tags || filter . tags . length === 0 ) { return repoDocs ; } if ( filter . tags && filter . tags . length === 1 && filter . tags [ 0 ] . id === '/' ) { return repoDocs ; } const tags = filter . tags . filter ( current => current . id !== '/' ) ; return TagMatcher2 . filter ( repoDocs , tags ) ; } }