import { HistoryQuery } from '@empathyco/x-types' ; import { ActionsClass } from '../../../../store/actions.types' ; import { isArrayEmpty } from '../../../../utils/array' ; import { normalizeString } from '../../../../utils/normalize' ; import { Pair } from '../../../../utils/types' ; import { HistoryQueriesActionContext , HistoryQueriesXStoreModule } from '../types' ; /** Regex for splitting a query into its words. */ const SPLIT_WORDS_REGEX = /[\s-]/ ; /**
 * Class implementation for the  HistoryQueriesActions . addQueryToHistory action.
 *
 *  public export class AddQueryToHistoryAction implements ActionsClass < HistoryQueriesXStoreModule > { /**
     * Default implementation for the  HistoryQueriesActions . addQueryToHistory .
     *
     *  param context - The  https of the
     * actions, provided by Vuex.
     *  param query returns addQueryToHistory ( { state , dispatch } , query ) { const normalizedQuery = normalizeString ( query ) ; if ( ! normalizedQuery ) { return ; } if ( isArrayEmpty ( state . historyQueries ) ) { return dispatch ( 'setHistoryQueries' , [ this . createHistoryQuery ( query ) ] ) ; } const newHistory = this . createNewHistory ( state . historyQueries , normalizedQuery ) ; if ( newHistory ) { newHistory . unshift ( this . createHistoryQuery ( query ) ) ; return dispatch ( 'setHistoryQueries' , newHistory ) ; } } param query returns internal protected createHistoryQuery ( query ) { return { query : query . trim ( ) , timestamp : Date . now ( ) , modelName : 'HistoryQuery' } ; } /**
     * Creates a new history from the old one to store the new query. Depending on the comparison
     * between the new query, and the last one in the history, it can return three different things:
     * - If it the last stored query should be replaced with the new one
     * ( AddQueryToHistoryAction . isReplaceAction returns true), it returns a copy of the old
     * history, without the new query and the first item.
     * - If the new query should be simply added to the history
     * ( AddQueryToHistoryAction . isAddAction returns true), It returns a copy of the old
     * history but without the new query if it was present.
     * - In any other case, the query shouldn't be saved, so this method returns `null`.
     *
     *  param currentHistory param normalizedQuery returns internal protected createNewHistory ( currentHistory , normalizedQuery ) { const normalizedLastQuery = normalizeString ( currentHistory [ 0 ] . query ) ; const queriesTuple = [ normalizedLastQuery , normalizedQuery ] ; const newWords = normalizedQuery . split ( SPLIT_WORDS_REGEX ) ; const lastWords = normalizedLastQuery . split ( SPLIT_WORDS_REGEX ) ; const wordsTuple = [ lastWords , newWords ] ; return this . isReplaceAction ( wordsTuple , queriesTuple ) ? this . removeNewQueryFromHistory ( currentHistory . slice ( 1 ) , normalizedQuery ) : this . isAddAction ( wordsTuple , queriesTuple ) ? this . removeNewQueryFromHistory ( currentHistory , normalizedQuery ) : null ; } param currentHistory param normalizedQuery returns internal protected removeNewQueryFromHistory ( currentHistory , normalizedQuery ) { return currentHistory . filter ( historyQuery => normalizeString ( historyQuery . query ) !== normalizedQuery ) ; } param wordsTuple param queriesTuple returns internal protected isAddAction ( [ lastWords , newWords ] , [ lastQuery , newQuery ] ) { return newWords . length !== lastWords . length || ! lastQuery . includes ( newQuery ) ; } param wordsTuple param queriesTuple returns internal protected isReplaceAction ( [ lastWords , newWords ] , [ lastQuery , newQuery ] ) { return lastQuery === newQuery || this . isQueryBeingRefined ( lastWords , newWords ) ; } param lastWords param newWords returns internal protected isQueryBeingRefined ( lastWords , newWords ) { const refinedWordIndex = lastWords . length - 1 ; const lastRefinedWord = lastWords [ refinedWordIndex ] ; const newRefinedWord = newWords [ refinedWordIndex ] ; return ( ! ! lastRefinedWord && ! ! newRefinedWord && newRefinedWord !== lastRefinedWord && newRefinedWord . includes ( lastRefinedWord ) ) ; } } const addQueryToHistoryAction = new AddQueryToHistoryAction ( ) ; public export const addQueryToHistory = addQueryToHistoryAction . addQueryToHistory . bind ( addQueryToHistoryAction ) ;