/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import * as models from './models' ; param inputText param matches param original export const convertMatchedTextIntoMatchedOption = < T > ( inputText , matches , original ) : models . MatchedOption < T > => { const initialSegment = { text : inputText , startIndex : 0 , endIndex : inputText . length , data : { matched : false } } ; const matchedSegments = matches . reduce < models . ISegement [ ] > ( ( segements , [ startIndex , originalEndIndex ] ) => { const endIndex = originalEndIndex + 1 ; const segementIndexWhereEntityBelongs = segements . findIndex ( seg => seg . startIndex <= startIndex && endIndex <= seg . endIndex ) ; const prevSegements = segements . slice ( 0 , segementIndexWhereEntityBelongs ) ; const nextSegements = segements . slice ( segementIndexWhereEntityBelongs + 1 , segements . length ) ; const segementWhereEntityBelongs = segements [ segementIndexWhereEntityBelongs ] ; const prevSegementEndIndex = startIndex - segementWhereEntityBelongs . startIndex ; const prevSegementText = segementWhereEntityBelongs . text . substring ( 0 , prevSegementEndIndex ) ; const prevSegement = { ... segementWhereEntityBelongs , text : prevSegementText , endIndex : startIndex , } ; const nextSegementStartIndex = endIndex - segementWhereEntityBelongs . startIndex ; const nextSegementText = segementWhereEntityBelongs . text . substring ( nextSegementStartIndex , segementWhereEntityBelongs . text . length ) ; const nextSegement = { ... segementWhereEntityBelongs , text : nextSegementText , startIndex : endIndex , } ; const newSegement = { text : segementWhereEntityBelongs . text . substring ( prevSegementEndIndex , nextSegementStartIndex ) , startIndex : startIndex , endIndex : endIndex , data : { matched : true } } ; const newSegements = [ ] ; if ( prevSegement . startIndex !== prevSegement . endIndex ) { newSegements . push ( prevSegement ) ; } if ( newSegement . startIndex !== newSegement . endIndex ) { newSegements . push ( newSegement ) ; } if ( nextSegement . startIndex !== nextSegement . endIndex ) { newSegements . push ( nextSegement ) ; } return [ ... prevSegements , ... newSegements , ... nextSegements ] ; } , [ initialSegment ] ) ; const matchedStrings = matchedSegments . map ( ( { text , data } ) => ( { text , matched : data . matched } ) ) ; return { highlighted : false , original , matchedStrings } ; } ;