export const heightLight={
    highlightSearchContentByTerms:(content:string, terms:string[])=> {
        if (content===null || content==='') return '';
        if ((terms||[]).length===0) return content;
        let processContent = content||'';
        if(typeof(content)!=='string'){
            processContent = `${content}`||''
        }
         
        (terms||[]).forEach((term:string) => {
            processContent = processContent.replace(term, `<em>${term}</em>`);
        })
      
        return highlightSearchContent(processContent);
    },
    heightLightString:(content:string)=>{
        return highlightSearchContent(content)
    }
}

const highlightSearchContent=(content:string)=> {
    if (content===null || content==='') return '';
  
    const startFlag = '<em>';
    const endFlag = '</em>';
  
    const start = content.indexOf(startFlag);
    const end = content.indexOf(endFlag);
    const beforeStr = content.substr(0, start);
  
    const middleStr = content.substr(start + startFlag.length, end - start - startFlag.length);
    const afterStr = content.substr(end + endFlag.length);
  
    return (
        <>
            {
                start > -1 ? <span>
                    {beforeStr}
                    <span style={{ color: '#f50' }}>{middleStr}</span>
                    {highlightSearchContent(afterStr)}
                </span> : <span>{content}</span>
            }
        </>
    )
}