import*as Common from"../common/common.js";import*as Components from"../components/components.js";import*as i18n from"../i18n/i18n.js";import*as Platform from"../platform/platform.js";import*as TextUtils from"../text_utils/text_utils.js";import*as UI from"../ui/ui.js";const UIStrings={matchesCountS:"Matches Count {PH1}",lineS:"Line {PH1}",showDMore:"Show {PH1} more"},str_=i18n.i18n.registerUIStrings("search/SearchResultsPane.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class SearchResultsPane extends UI.Widget.VBox{constructor(e){super(!0),this._searchConfig=e,this._searchResults=[],this._treeOutline=new UI.TreeOutline.TreeOutlineInShadow,this._treeOutline.hideOverflow(),this._treeOutline.registerRequiredCSS("search/searchResultsPane.css",{enableLegacyPatching:!0}),this.contentElement.appendChild(this._treeOutline.element),this._matchesExpandedCount=0}addSearchResult(e){this._searchResults.push(e),this._addTreeElement(e)}_addTreeElement(e){const t=new SearchResultsTreeElement(this._searchConfig,e);this._treeOutline.appendChild(t),this._treeOutline.selectedTreeElement||t.select(!0,!0),this._matchesExpandedCount<matchesExpandedByDefault&&t.expand(),this._matchesExpandedCount+=e.matchesCount()}}export const matchesExpandedByDefault=20;export const matchesShownAtOnce=20;export class SearchResultsTreeElement extends UI.TreeOutline.TreeElement{constructor(e,t){super("",!0),this._searchConfig=e,this._searchResult=t,this._initialized=!1,this.toggleOnClick=!0}onexpand(){this._initialized||(this._updateMatchesUI(),this._initialized=!0)}_updateMatchesUI(){this.removeChildren();const e=Math.min(this._searchResult.matchesCount(),20);e<this._searchResult.matchesCount()?(this._appendSearchMatches(0,e-1),this._appendShowMoreMatchesElement(e-1)):this._appendSearchMatches(0,e)}onattach(){this._updateSearchMatches()}_updateSearchMatches(){this.listItemElement.classList.add("search-result");const e=s(this._searchResult.label(),"search-result-file-name");e.appendChild(s("\u2014","search-result-dash")),e.appendChild(s(this._searchResult.description(),"search-result-qualifier")),this.tooltip=this._searchResult.description(),this.listItemElement.appendChild(e);const t=document.createElement("span");function s(e,t){const s=document.createElement("span");return s.className=t,s.textContent=e,s}t.className="search-result-matches-count",t.textContent=`${this._searchResult.matchesCount()}`,UI.ARIAUtils.setAccessibleName(t,i18nString(UIStrings.matchesCountS,{PH1:this._searchResult.matchesCount()})),this.listItemElement.appendChild(t),this.expanded&&this._updateMatchesUI()}_appendSearchMatches(e,t){const s=this._searchResult,n=this._searchConfig.queries(),a=[];for(let i=0;i<n.length;++i)a.push(Platform.StringUtilities.createSearchRegex(n[i],!this._searchConfig.ignoreCase(),this._searchConfig.isRegex()));for(let i=e;i<t;++i){const e=s.matchLineContent(i).trim();let t=[];for(let s=0;s<a.length;++s)t=t.concat(this._regexMatchRanges(e,a[s]));const n=Components.Linkifier.Linkifier.linkifyRevealable(s.matchRevealable(i),"");n.classList.add("search-match-link");const h=document.createElement("span");h.classList.add("search-match-line-number");const c=s.matchLabel(i);h.textContent=c,"number"!==typeof c||isNaN(c)?UI.ARIAUtils.setAccessibleName(h,c):UI.ARIAUtils.setAccessibleName(h,i18nString(UIStrings.lineS,{PH1:c})),n.appendChild(h);const l=this._createContentSpan(e,t);n.appendChild(l);const r=new UI.TreeOutline.TreeElement;this.appendChild(r),r.listItemElement.className="search-match",r.listItemElement.appendChild(n),r.listItemElement.addEventListener("keydown",(e=>{"Enter"===e.key&&(e.consume(!0),Common.Revealer.reveal(s.matchRevealable(i)))})),r.tooltip=e}}_appendShowMoreMatchesElement(e){const t=this._searchResult.matchesCount()-e,s=i18nString(UIStrings.showDMore,{PH1:t}),n=new UI.TreeOutline.TreeElement(s);this.appendChild(n),n.listItemElement.classList.add("show-more-matches"),n.onselect=this._showMoreMatchesElementSelected.bind(this,n,e)}_createContentSpan(e,t){let s=0;t.length>0&&t[0].offset>20&&(s=15),e=e.substring(s,1e3+s),s&&(t=t.map((e=>new TextUtils.TextRange.SourceRange(e.offset-s+1,e.length))),e="\u2026"+e);const n=document.createElement("span");return n.className="search-match-content",n.textContent=e,UI.ARIAUtils.setAccessibleName(n,`${e} line`),UI.UIUtils.highlightRangesWithStyleClass(n,t,"highlighted-match"),n}_regexMatchRanges(e,t){let s;t.lastIndex=0;const n=[];for(;t.lastIndex<e.length&&(s=t.exec(e));)n.push(new TextUtils.TextRange.SourceRange(s.index,s[0].length));return n}_showMoreMatchesElementSelected(e,t){return this.removeChild(e),this._appendSearchMatches(t,this._searchResult.matchesCount()),!1}}