/*
 * 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/form-engine/element/suggest/result-container.js";import o from"@typo3/core/document-service.js";import i from"@typo3/backend/form-engine.js";import n from"@typo3/core/event/regular-event.js";import u from"@typo3/core/event/debounce-event.js";import a from"@typo3/core/ajax/ajax-request.js";import{selector as d}from"@typo3/core/literals.js";class m{constructor(t){this.currentRequest=null,this.handleKeyDown=e=>{if(e.key==="ArrowDown"){e.preventDefault(),JSON.parse(this.resultContainer.getAttribute("results"))?.length>0&&(this.resultContainer.hidden=!1),this.resultContainer.querySelector("typo3-backend-formengine-suggest-result-item")?.focus();return}e.key==="Escape"&&(e.preventDefault(),this.resultContainer.hidden=!0)},this.element=t,o.ready().then(()=>{this.initialize(t),this.registerEvents()})}initialize(t){const e=t.closest(".t3-form-suggest-container");this.resultContainer=document.createElement("typo3-backend-formengine-suggest-result-container"),this.resultContainer.hidden=!0,e.append(this.resultContainer)}registerEvents(){new n("typo3:formengine:suggest-item-chosen",t=>{let e="";this.element.dataset.fieldtype==="select"?e=t.detail.element.uid:e=t.detail.element.table+"_"+t.detail.element.uid,i.setSelectOptionFromExternalSource(this.element.dataset.field,e,t.detail.element.label,t.detail.element.label),i.markFieldAsChanged(document.querySelector(d`input[name="${this.element.dataset.field}"]`)),this.resultContainer.hidden=!0}).bindTo(this.resultContainer),new n("focus",()=>{JSON.parse(this.resultContainer.getAttribute("results"))?.length>0&&(this.resultContainer.hidden=!1)}).bindTo(this.element),new n("blur",t=>{t.relatedTarget?.tagName.toLowerCase()!=="typo3-backend-formengine-suggest-result-item"&&(this.resultContainer.hidden=!0)}).bindTo(this.element),new u("input",t=>{this.currentRequest instanceof a&&this.currentRequest.abort();const e=t.target;if(e.value.length<parseInt(e.dataset.minchars,10))return;const r=parseInt(e.dataset.uid,10);this.currentRequest=new a(TYPO3.settings.ajaxUrls.record_suggest),this.currentRequest.post({value:e.value,tableName:e.dataset.tablename,fieldName:e.dataset.fieldname,uid:isNaN(r)?null:r,pid:parseInt(e.dataset.pid,10),dataStructureIdentifier:e.dataset.datastructureidentifier,flexFormSheetName:e.dataset.flexformsheetname,flexFormFieldName:e.dataset.flexformfieldname,flexFormContainerName:e.dataset.flexformcontainername,flexFormContainerFieldName:e.dataset.flexformcontainerfieldname,recordTypeValue:e.dataset.recordtypevalue}).then(async s=>{const l=await s.raw().text();this.resultContainer.setAttribute("results",l),this.resultContainer.hidden=!1})}).bindTo(this.element),new n("keydown",this.handleKeyDown).bindTo(this.element)}}export{m as default};
