/*
 * 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 m,{Sizes as b}from"@typo3/backend/modal.js";import"@typo3/backend/element/icon-element.js";import"@typo3/backend/live-search/element/hint.js";import"@typo3/backend/live-search/element/result/result-pagination.js";import"@typo3/backend/live-search/element/search-option-item.js";import"@typo3/backend/live-search/live-search-shortcut.js";import q from"@typo3/core/document-service.js";import c from"@typo3/core/event/regular-event.js";import w from"@typo3/core/event/debounce-event.js";import{SeverityEnum as E}from"@typo3/backend/enum/severity.js";import R from"@typo3/core/ajax/ajax-request.js";import d from"@typo3/backend/storage/browser-session.js";import{componentName as k}from"@typo3/backend/live-search/element/result/result-container.js";import{ModuleStateStorage as O}from"@typo3/backend/storage/module-state-storage.js";import T from"~labels/core.core";class C{constructor(){this.currentSearchRequest=null,this.search=async t=>{if(t.get("query").toString()==="")this.updateSearchResults();else{const o=document.querySelector(k),l=document.querySelector("typo3-backend-live-search-result-pagination");o.loading=!0,l.loading=!0,this.currentSearchRequest?.abort();try{this.currentSearchRequest=new R(TYPO3.settings.ajaxUrls.livesearch);const r=await(await this.currentSearchRequest.post(t)).raw().json();this.currentSearchRequest=null,this.updateSearchResults(r)}catch(s){if(this.updateSearchResults(null,!0),s instanceof DOMException&&s.name==="AbortError")return;throw s}}},q.ready().then(()=>{this.registerEvents()})}registerEvents(){new c("typo3:live-search:trigger-open",()=>{m.currentModal||this.openSearchModal()}).bindTo(document)}openSearchModal(){const t=new URL(TYPO3.settings.ajaxUrls.livesearch_form,window.location.origin),n=O.current("web");n.identifier&&t.searchParams.set("pageId",n.identifier),t.searchParams.set("query",d.get("livesearch-term")??""),t.searchParams.set("offset",d.get("livesearch-offset")??"0");const o=Object.entries(d.getByPrefix("livesearch-option-")).filter(r=>r[1]==="1").map(r=>{const e=r[0].replace("livesearch-option-",""),[a,p]=e.split("-",2);return{key:a,value:p}}),l=this.composeSearchOptions(o);for(const[r,e]of Object.entries(l))for(const a of e)t.searchParams.append(`${r}[]`,a);const s=m.advanced({type:m.types.ajax,content:t.toString(),title:T.get("labels.search"),severity:E.notice,size:b.large,ajaxCallback:()=>{const r=s.querySelector("typo3-backend-live-search"),e=r.querySelector("form"),a=e.querySelector('input[type="search"]'),p=e.querySelector('input[name="offset"]');new c("livesearch:demand-changed",()=>{p.value="0",e.requestSubmit()}).bindTo(r),new c("livesearch:pagination-selected",h=>{p.value=h.detail.offset.toString(10),e.requestSubmit()}).bindTo(r),new c("submit",h=>{h.preventDefault();const u=new FormData(e);this.search(u).then(()=>{const g=u.get("query").toString(),v=u.get("offset")?.toString();d.set("livesearch-term",g),v&&d.set("livesearch-offset",v)});const i=e.querySelector("[data-active-options-counter]"),S=parseInt(i.dataset.activeOptionsCounter,10);i.querySelector("output").textContent=S.toString(10),i.classList.toggle("hidden",S===0)}).bindTo(e),new c("search",()=>{a.value===""&&e.requestSubmit()}).bindTo(a);const y=document.querySelector("typo3-backend-live-search-result-container");new c("live-search:item-chosen",()=>{m.dismiss()}).bindTo(y),new c("typo3:live-search:option-invoked",h=>{const u=e.querySelector("[data-active-options-counter]");let i=parseInt(u.dataset.activeOptionsCounter,10);i=h.detail.active?i+1:i-1,u.dataset.activeOptionsCounter=i.toString(10),r.dispatchEvent(new CustomEvent("livesearch:demand-changed"))}).bindTo(r),new w("input",()=>{r.dispatchEvent(new CustomEvent("livesearch:demand-changed"))}).bindTo(a),new c("keydown",this.handleKeyDown).bindTo(a),e.requestSubmit()}});["modal-loaded","typo3-modal-shown"].forEach(r=>{s.addEventListener(r,()=>{const e=s.querySelector('input[type="search"]');e!==null&&(e.focus(),e.select())})})}composeSearchOptions(t){const n={};return t.forEach(o=>{n[o.key]===void 0&&(n[o.key]=[]),n[o.key].push(o.value)}),n}handleKeyDown(t){if(t.key!=="ArrowDown")return;t.preventDefault(),document.querySelector("typo3-backend-live-search").querySelector("typo3-backend-live-search-result-item")?.focus()}updateSearchResults(t=null,n=!1){const o=document.querySelector("typo3-backend-live-search-result-container");o.results=t?.results??null,o.loading=!1,o.hasErrors=n;const l=document.querySelector("typo3-backend-live-search-result-pagination");l.pagination=t?.pagination??null,l.loading=!1}}let f;top.TYPO3.LiveSearch?f=top.TYPO3.LiveSearch:(f=new C,top.TYPO3.LiveSearch=f);var j=f;export{j as default};
