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