/*
 * 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 a from"@typo3/backend/form-engine.js";import s from"@typo3/backend/form-engine-validation.js";import d from"@typo3/core/event/regular-event.js";import{selector as p}from"@typo3/core/literals.js";class i{constructor(){this.registerSortableEventHandler=e=>{this.registerKeyboardEventHandler(e);const n=e.closest(".form-wizards-wrap").querySelector(".form-wizards-item-aside");n!==null&&n.addEventListener("click",r=>{const t=r.target.closest(".t3js-btn-option");if(t===null)return;r.preventDefault();const o=t.dataset.fieldname,c=a.getFieldElement(o).get(0),l=a.getFieldElement(o,"_avail").get(0);t.classList.contains("t3js-btn-moveoption-top")?i.moveOptionToTop(e):t.classList.contains("t3js-btn-moveoption-up")?i.moveOptionUp(e):t.classList.contains("t3js-btn-moveoption-down")?i.moveOptionDown(e):t.classList.contains("t3js-btn-moveoption-bottom")?i.moveOptionToBottom(e):t.classList.contains("t3js-btn-removeoption")&&i.removeOption(e,l),a.updateHiddenFieldValueFromSelect(e,c),s.markFieldAsChanged(l),s.validateField(l)})},this.registerKeyboardEventHandler=e=>{const n=e.dataset.formengineInputName,r=a.getFieldElement(n).get(0),t=a.getFieldElement(n,"_avail").get(0);new d("keydown",o=>{(o.code==="Delete"||o.code==="Backspace")&&(o.preventDefault(),i.removeOption(e,t)),o.code==="ArrowUp"&&o.altKey&&(o.preventDefault(),i.moveOptionUp(e)),o.code==="ArrowDown"&&o.altKey&&(o.preventDefault(),i.moveOptionDown(e)),o.code==="ArrowUp"&&o.altKey&&o.shiftKey&&(o.preventDefault(),i.moveOptionToTop(e)),o.code==="ArrowDown"&&o.altKey&&o.shiftKey&&(o.preventDefault(),i.moveOptionToBottom(e)),o.defaultPrevented&&(a.updateHiddenFieldValueFromSelect(e,r),s.markFieldAsChanged(t),s.validateField(t))}).bindTo(e)}}static moveOptionToTop(e){Array.from(e.querySelectorAll(":checked")).reverse().forEach(n=>{e.insertBefore(n,e.firstElementChild)})}static moveOptionToBottom(e){e.querySelectorAll(":checked").forEach(n=>{e.insertBefore(n,null)})}static moveOptionUp(e){const n=Array.from(e.children),r=Array.from(e.querySelectorAll(":checked"));for(const t of r){if(n.indexOf(t)===0&&t.previousElementSibling===null)break;e.insertBefore(t,t.previousElementSibling)}}static moveOptionDown(e){const n=Array.from(e.children).reverse(),r=Array.from(e.querySelectorAll(":checked")).reverse();for(const t of r){if(n.indexOf(t)===0&&t.nextElementSibling===null)break;e.insertBefore(t,t.nextElementSibling.nextElementSibling)}}static removeOption(e,n){const r=e.selectedIndex;e.querySelectorAll(":checked").forEach(t=>{const o=n.querySelector(p`option[value="${t.value}"]`);o!==null&&(o.classList.remove("hidden"),o.disabled=!1,a.enableOptGroup(o)),e.removeChild(t)}),e.selectedIndex=r>0?r-1:0}}export{i as AbstractSortableSelectItems};
