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