import { queryBM , queryRecentLyClosed , queryTab } from '@/api' ; import { getKeys , isEachEligible } from '@/common/utils' ; import store from '@/store' ; const extConfig = store . state . config ; function searchKeyword ( splitSearchStr ) { const temp = [ ] ; Object . values ( extConfig ) . forEach ( item => { if ( isEachEligible ( splitSearchStr , `${ item . desc } ${ item . keyword }` ) ) { temp . push ( { type : 'keyword' , title : `Search ${ item . desc }` , subtitle : `Search ${ item . desc } for "..."` , keyword : item . keyword , id : item . desc , } ) ; } } ) ; return temp ; } function filterKeyword ( splitSearchStr ) { const temp = [ ] ; getKeys ( extConfig ) . some ( key => { const value = extConfig [ key ] ; if ( value . keyword === splitSearchStr [ 0 ] ) { temp . length = 0 ; temp . push ( { type : key , searchQueue : splitSearchStr . slice ( 1 ) , } ) ; return true ; } if ( value . isDefault ) { temp . push ( { type : key , searchQueue : splitSearchStr , } ) ; } return false ; } ) ; temp . unshift ( { type : 'keyword' , searchQueue : splitSearchStr , } ) ; return temp ; } async function searchFromType ( item ) { let result = [ ] ; switch ( item . type ) { case 'keyword' : result = searchKeyword ( item . searchQueue ) ; break ; case 'bookmark' : result = await queryBM ( item . searchQueue ) ; break ; case 'tab' : result = await queryTab ( item . searchQueue ) ; break ; case 'RCT' : result = await queryRecentLyClosed ( item . searchQueue ) ; break ; default : break ; } return result ; } async function filterSearchData ( searchStr ) { const temp = [ ] ; const strArr = searchStr . replace ( /  +/g , ' ' ) . split ( ' ' ) ; const searchPromises = filterKeyword ( strArr ) . map ( async ( item ) => { const itemResult = await searchFromType ( item ) ; return itemResult ; } ) ; for ( const searchPromise of searchPromises ) { temp . push ( ... ( await searchPromise ) ) ; } return temp ; } export default filterSearchData ;