/*
 * 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 u from"@typo3/core/ajax/ajax-request.js";import p from"@typo3/core/document-service.js";import h from"@typo3/core/event/debounce-event.js";import c from"@typo3/core/event/regular-event.js";var o;(function(s){s.toggleButton=".t3js-form-field-slug-toggle",s.recreateButton=".t3js-form-field-slug-recreate",s.inputField=".t3js-form-field-slug-input",s.readOnlyField=".t3js-form-field-slug-readonly",s.hiddenField=".t3js-form-field-slug-hidden"})(o||(o={}));var n;(function(s){s.AUTO="auto",s.RECREATE="recreate",s.MANUAL="manual"})(n||(n={}));class f{constructor(t,i){this.options=null,this.fullElement=null,this.manuallyChanged=!1,this.readOnlyField=null,this.inputField=null,this.hiddenField=null,this.request=null,this.fieldsToListenOn={},this.options=i,this.fieldsToListenOn=this.options.listenerFieldNames||{},p.ready().then(l=>{this.fullElement=l.querySelector(t),this.inputField=this.fullElement.querySelector(o.inputField),this.readOnlyField=this.fullElement.querySelector(o.readOnlyField),this.hiddenField=this.fullElement.querySelector(o.hiddenField),this.registerEvents()})}registerEvents(){const t=Object.values(this.getAvailableFieldsForProposalGeneration()).map(e=>`[id="${e.id}"]`),i=this.fullElement.querySelector(o.recreateButton);t.length>0&&this.options.command==="new"&&new h("input",()=>{this.manuallyChanged||this.sendSlugProposal(n.AUTO)}).delegateTo(document,t.join(",")),t.length>0||this.hasPostModifiersDefined()?new c("click",e=>{e.preventDefault(),this.readOnlyField.classList.contains("hidden")&&(this.readOnlyField.classList.toggle("hidden",!1),this.inputField.classList.toggle("hidden",!0)),this.sendSlugProposal(n.RECREATE)}).bindTo(i):(i.classList.add("disabled"),i.disabled=!0),new h("input",()=>{this.manuallyChanged=!0,this.sendSlugProposal(n.MANUAL)}).bindTo(this.inputField);const l=this.fullElement.querySelector(o.toggleButton);new c("click",e=>{e.preventDefault();const a=this.readOnlyField.classList.contains("hidden");if(this.readOnlyField.classList.toggle("hidden",!a),this.inputField.classList.toggle("hidden",a),!a){this.hiddenField.value=this.inputField.value;return}this.inputField.value!==this.readOnlyField.value?this.readOnlyField.value=this.inputField.value:(this.manuallyChanged=!1,this.fullElement.querySelector(".t3js-form-proposal-accepted").classList.add("hidden"),this.fullElement.querySelector(".t3js-form-proposal-different").classList.add("hidden")),this.hiddenField.value=this.readOnlyField.value}).bindTo(l)}sendSlugProposal(t){const i={};t===n.AUTO||t===n.RECREATE?(Object.entries(this.getAvailableFieldsForProposalGeneration()).forEach(l=>{i[l[0]]=l[1].value}),this.options.includeUidInValues===!0&&(i.uid=this.options.recordId.toString())):i.manual=this.inputField.value,this.request instanceof u&&this.request.abort(),this.request=new u(TYPO3.settings.ajaxUrls.record_slug_suggest),this.request.post({values:i,mode:t,tableName:this.options.tableName,pageId:this.options.pageId,parentPageId:this.options.parentPageId,recordId:this.options.recordId,language:this.options.language,fieldName:this.options.fieldName,command:this.options.command,signature:this.options.signature}).then(async l=>{const e=await l.resolve(),a="/"+e.proposal.replace(/^\//,""),d=this.fullElement.querySelector(".t3js-form-proposal-accepted"),r=this.fullElement.querySelector(".t3js-form-proposal-different");d.classList.toggle("hidden",e.hasConflicts),r.classList.toggle("hidden",!e.hasConflicts),(e.hasConflicts?r:d).querySelector("span").innerText=a,this.hiddenField.value!==e.proposal&&this.fullElement.querySelector("input[data-formengine-input-name]").dispatchEvent(new Event("change",{bubbles:!0,cancelable:!0})),t===n.AUTO||t===n.RECREATE?(this.readOnlyField.value=e.proposal,this.hiddenField.value=e.proposal,this.inputField.value=e.proposal):this.hiddenField.value=e.proposal}).finally(()=>{this.request=null})}getAvailableFieldsForProposalGeneration(){const t={};for(const[i,l]of Object.entries(this.fieldsToListenOn)){let e=document.querySelector('[data-formengine-input-name="'+l+'"]');e===null&&(e=document.querySelector('[name="'+l+'"]')),e!==null&&(t[i]=e)}return t}hasPostModifiersDefined(){return Array.isArray(this.options.config.generatorOptions.postModifiers)&&this.options.config.generatorOptions.postModifiers.length>0}}export{f as default};
