import*as Platform from"../platform/platform.js";export class FilterSuggestionBuilder{constructor(t,e){this._keys=t,this._valueSorter=e||((t,e)=>e.sort()),this._valuesMap=new Map}completions(t,e,s){if(!e&&!s)return Promise.resolve([]);const r=e.startsWith("-");r&&(e=e.substring(1));const o=r?"-":"",i=e.indexOf(":"),a=[];if(-1===i){const t=new RegExp("^"+Platform.StringUtilities.escapeForRegExp(e),"i");for(const e of this._keys)t.test(e)&&a.push({text:o+e+":"})}else{const t=e.substring(0,i).toLowerCase(),s=e.substring(i+1),r=new RegExp("^"+Platform.StringUtilities.escapeForRegExp(s),"i"),l=Array.from(this._valuesMap.get(t)||new Set);this._valueSorter(t,l);for(const e of l)r.test(e)&&e!==s&&a.push({text:o+t+":"+e})}return Promise.resolve(a)}addItem(t,e){if(!e)return;let s=this._valuesMap.get(t);s||(s=new Set,this._valuesMap.set(t,s)),s.add(e)}clear(){this._valuesMap.clear()}}