/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
import"@typo3/backend/element/spinner-element.js";import m from"@typo3/backend/live-search/live-search-configurator.js";import{LitElement as p,html as l,css as v}from"lit";import{property as y,customElement as h}from"lit/decorators.js";import{until as b}from"lit/directives/until.js";import"@typo3/backend/live-search/element/provider/default-result-item.js";import"@typo3/backend/live-search/element/result/item/item.js";var u=function(o,e,t,n){var i=arguments.length,r=i<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,s;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(o,e,t,n);else for(var c=o.length-1;c>=0;c--)(s=o[c])&&(r=(i<3?s(r):i>3?s(e,t,r):s(e,t))||r);return i>3&&r&&Object.defineProperty(e,t,r),r};const f="typo3-backend-live-search-result-item-container";let a=class extends p{constructor(){super(...arguments),this.results=null}connectedCallback(){super.connectedCallback(),this.addEventListener("scroll",this.onScroll)}disconnectedCallback(){this.removeEventListener("scroll",this.onScroll),super.disconnectedCallback()}createRenderRoot(){return this}render(){const e={},t=this.results.filter(n=>n!==null);return t.length!==this.results.length&&console.warn('The result set contained "null" values, indicating something went wrong while building the search results. Affected values were removed to no break the user interface.'),t.forEach(n=>{n.typeLabel in e?e[n.typeLabel].push(n):e[n.typeLabel]=[n]}),l`<typo3-backend-live-search-result-list>${this.renderGroupedResults(e)}</typo3-backend-live-search-result-list>`}renderGroupedResults(e){const t=[];for(const[n,i]of Object.entries(e)){const r=i.length;t.push(l`<h6 class=livesearch-result-item-group-label>${n} (${r})</h6>`),t.push(...i.map(s=>l`${b(this.renderResultItem(s),l`<typo3-backend-spinner></typo3-backend-spinner>`)}`))}return l`${t}`}async renderResultItem(e){const t=m.getRenderers();let n;return t[e.provider]!==void 0?(await import(t[e.provider].module),n=t[e.provider].callback(e)):n=l`<typo3-backend-live-search-result-item-default title="${e.typeLabel}: ${e.itemTitle}" .icon=${e.icon} .language=${e.language} .itemTitle=${e.itemTitle} .typeLabel=${e.typeLabel} .extraData=${e.extraData}></typo3-backend-live-search-result-item-default>`,l`<typo3-backend-live-search-result-item .resultItem=${e} @click=${()=>this.invokeAction(e,e.defaultAction)} @focus=${()=>this.requestActions(e)}>${n}</typo3-backend-live-search-result-item>`}requestActions(e){this.parentElement.dispatchEvent(new CustomEvent("livesearch:request-actions",{detail:{resultItem:e}}))}invokeAction(e,t){this.parentElement.dispatchEvent(new CustomEvent("livesearch:invoke-action",{detail:{resultItem:e,action:t}}))}onScroll(e){this.querySelectorAll(".livesearch-result-item-group-label").forEach(t=>{t.classList.toggle("sticky",t.offsetTop<=e.target.scrollTop)})}};u([y({type:Object,attribute:!1})],a.prototype,"results",void 0),a=u([h("typo3-backend-live-search-result-item-container")],a);let d=class extends p{static{this.styles=v`:host{display:block}`}connectedCallback(){this.parentContainer=this.closest("typo3-backend-live-search-result-container"),this.resultItemDetailContainer=this.parentContainer.querySelector("typo3-backend-live-search-result-item-detail-container"),super.connectedCallback(),this.addEventListener("keydown",this.handleKeyDown),this.addEventListener("keyup",this.handleKeyUp)}disconnectedCallback(){this.removeEventListener("keydown",this.handleKeyDown),this.removeEventListener("keyup",this.handleKeyUp),super.disconnectedCallback()}render(){return l`<slot></slot>`}handleKeyDown(e){if(!["ArrowDown","ArrowUp","ArrowRight"].includes(e.key))return;const t="typo3-backend-live-search-result-item";if(document.activeElement.tagName.toLowerCase()!==t)return;e.preventDefault();let n;if(e.key==="ArrowDown"){let i=document.activeElement.nextElementSibling;for(;i!==null&&i.tagName.toLowerCase()!==t;)i=i.nextElementSibling;n=i}else if(e.key==="ArrowUp"){let i=document.activeElement.previousElementSibling;for(;i!==null&&i.tagName.toLowerCase()!==t;)i=i.previousElementSibling;n=i,n===null&&(n=document.querySelector("typo3-backend-live-search").querySelector('input[type="search"]'))}else e.key==="ArrowRight"&&(n=this.resultItemDetailContainer.querySelector("typo3-backend-live-search-result-item-action"));n!==null&&n.focus()}handleKeyUp(e){if(!["Enter"," "].includes(e.key))return;e.preventDefault();const t=e.target.resultItem;this.invokeAction(t)}invokeAction(e){this.parentContainer.dispatchEvent(new CustomEvent("livesearch:invoke-action",{detail:{resultItem:e,action:e.actions[0]}}))}};d=u([h("typo3-backend-live-search-result-list")],d);export{a as ItemContainer,d as ResultList,f as componentName};
