import{parseRegex as e}from"./lib/regex-parse.js";import{isStringEmpty as t}from"./lib/utils.js";const s=()=>!0,i=()=>!1;class l{constructor(e){this._host=e,this._logs=[],this._logsFiltered=[],this._limit=1/0,this._filterCallback=s,this._selection=new Set,this._selectionLast=null,this._focusedIndex=null}set limit(e){const t="number"==typeof e&&!Number.isNaN(e);(t?e:1/0)!==this._limit&&(this._limit=t?e:1/0,this._updateList())}set filter(e){if(null==e)this._filterCallback=s;else{const t=this._getMessageFilterCallback(e.message),s=this._getMetadataFilterCallback(e.metadata);this._filterCallback=e=>t(e)&&s(e)}this._updateList({forceFilter:!0})}getList(){return this._logsFiltered}get listLength(){return this._logsFiltered.length}append(e){this._updateList({newLogs:e})}clear(){this._logs=[],this._logsFiltered=[],this._selection.clear(),this._updateList(),this.clearFocus()}select(e){const t=this._getLogByFilteredIndex(e);this._selection=null!=t?new Set([t]):new Set,this._selectionLast=t,this._host.requestUpdate(),this._host._onSelectionChanged()}toggleSelection(e){const t=this._getLogByFilteredIndex(e);null!=t&&(this._selection.has(t)?(this._selection.delete(t),this._selectionLast=Array.from(this._selection).pop()):(this._selection.add(t),this._selectionLast=this._logsFiltered[e]),this._host.requestUpdate(),this._host._onSelectionChanged())}extendSelection(e,t){if(0===this._selection.size)return void this.select(e);const s=this._selectionLast??Array.from(this._selection).pop(),i=this._logsFiltered.indexOf(s),l=e,n=Math.min(i,l),o=Math.max(i,l),h=this._logsFiltered.slice(Math.max(0,n),o+1);"replace"===t&&this._selection.clear(),h.forEach((e=>this._selection.add(e))),this._host.requestUpdate(),this._host._onSelectionChanged()}clearSelection(){this._selection.clear(),this._selectionLast=null,this._host.requestUpdate(),this._host._onSelectionChanged()}selectAll(){this._logsFiltered.length>0&&(this._selection=new Set(this._logsFiltered),this._selectionLast=this._logsFiltered[this._logsFiltered.length-1],this._host.requestUpdate(),this._host._onSelectionChanged())}get selectionLength(){return this._selection.size}isSelected(e){const t=this._getLogByFilteredIndex(e);return this._selection.has(t)}isSelectionEmpty(){return 0===this._selection.size}getSelectedLogs(){return this._logs.filter((e=>this._selection.has(e)))}getFirstSelectedLogIndex(){if(0===this._selection.size)return-1;const e=this._selection.values().next().value;return this._logs.findIndex((t=>e===t))}focus(e,t=!0){null!=e&&(e<0||e>this._logsFiltered.length-1)||this._focusedIndex!==e&&(this._focusedIndex=e,t&&this._host._onFocusedLogChange(e))}moveFocus(e,t){null!=this._focusedIndex?("up"===e&&this._focusedIndex>0&&this.focus(this._focusedIndex-1),"down"===e&&this._focusedIndex<this._logsFiltered.length-1&&this.focus(this._focusedIndex+1)):null!=t&&("up"===e&&this.focus(t.last),"down"===e&&this.focus(t.first))}clearFocus(e=!0){this.focus(null,e)}isFocusedIndexInRange({first:e,last:t}){return null!=this._focusedIndex&&this._focusedIndex>=e&&this._focusedIndex<=t}_getLogByFilteredIndex(e){return this._logsFiltered[e]}_updateList(e={}){const t=e.newLogs??[],s=e.forceFilter??!1;let i=!1;const l=e=>{i=this._selection.delete(e)||i},n=t.length>this._limit?t.slice(t.length-this._limit):t.slice(),o=this._logs.length+n.length,h=o>=this._limit?o-this._limit:0,c=this._logs.splice(0,h);if(this._logs.push(...n),s)this._logsFiltered=[],this._logs.forEach((e=>{this._filterCallback(e)?this._logsFiltered.push(e):l(e)})),this.clearFocus();else{const e=c.filter(this._filterCallback);this._logsFiltered.splice(0,e.length);const t=n.filter(this._filterCallback);this._logsFiltered.push(...t),null!=this._focusedIndex&&e.length>0&&(this._focusedIndex>e.length?this.focus(this._focusedIndex-e.length):this.clearFocus())}c.forEach(l),this._selection.has(this._selectionLast)||(this._selectionLast=null),this._host.requestUpdate(),i&&this._host._onSelectionChanged()}_getMetadataFilterCallback(e){if(null==e||0===e.length)return s;const t={};if(Array.isArray(e))for(const s of e)null==t[s.metadata]&&(t[s.metadata]=[]),t[s.metadata].push(s.value);const i=Object.entries(t);return e=>i.every((([t,s])=>{const i=e.metadata.find((e=>e.name===t));return s.includes(i?.value)}))}_getMessageFilterCallback(l){if(null==l||t(l.value))return s;if("strict"===l.type)return e=>e.message.includes(l.value);if("regex"===l.type)try{const t=e(l.value);return e=>null!=e.message.match(t)}catch{return i}const n=l.value.trim().toLowerCase().split(" ").filter((e=>e.length>0));return e=>{const t=e.message.toLowerCase();return n.every((e=>t.includes(e)))}}}export{l as LogsController};
//# sourceMappingURL=logs-controller.js.map
