/*
 * 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 D from"tablesort";import"tablesort.dotsep.js";import"tablesort.number.js";import{IconElement as L}from"@typo3/backend/element/icon-element.js";import{Sizes as T}from"@typo3/backend/enum/icon-types.js";class x extends D{init(o,g){let r,l;if(this.table=o,this.thead=!1,this.options=g,o.rows&&o.rows.length>0)if(o.tHead&&o.tHead.rows.length>0){for(let t=0;t<o.tHead.rows.length;t++)if(o.tHead.rows[t].getAttribute("data-sort-method")==="thead"){r=o.tHead.rows[t];break}r||(r=o.tHead.rows[o.tHead.rows.length-1]),this.thead=!0}else r=o.rows[0];if(!r)return;const h=t=>{t.preventDefault(),t.stopImmediatePropagation();const n=t.currentTarget.dataset.sortDirection,e=t.currentTarget.closest("th, td");this.current===e&&this.current.ariaSort===n||(e.ariaSort=n==="ascending"?"descending":"ascending",this.current&&this.current!==e&&this.current.removeAttribute("aria-sort"),this.current=e,this.sortTable(e))};for(let t=0;t<r.cells.length;t++){const n=r.cells[t];if(n.setAttribute("role","columnheader"),n.getAttribute("data-sort-method")!=="none"){const e=document.createElement("button");e.classList.add("dropdown-toggle","dropdown-toggle-link"),e.dataset.bsToggle="dropdown",e.dataset.sortingToggle="true",e.type="button",e.ariaExpanded="false",e.textContent=n.textContent;const c=new L;c.identifier="empty-empty",c.size=T.small;const m=document.createElement("div");m.appendChild(c),e.appendChild(m),n.replaceChildren(e);const p=document.createElement("div");p.classList.add("dropdown-menu");const s=top.TYPO3.lang["labels.sorting.asc"]||"Sort ascending",i=document.createElement("button");i.classList.add("dropdown-item"),i.type="button",i.title=s,i.ariaLabel=s,i.dataset.sortDirection="ascending",i.addEventListener("click",h,!1);const a=document.createElement("span");a.classList.add("dropdown-item-columns"),i.appendChild(a);const b=document.createElement("span");b.classList.add("dropdown-item-column","dropdown-item-column-icon","text-primary"),a.appendChild(b);const f=new L;f.identifier="empty-empty",f.size=T.small,b.appendChild(f);const w=document.createElement("span");w.classList.add("dropdown-item-column","dropdown-item-column-title"),w.textContent=s,a.appendChild(w),p.appendChild(i);const y=top.TYPO3.lang["labels.sorting.desc"]||"Sort descending",d=document.createElement("button");d.classList.add("dropdown-item"),d.type="button",d.title=y,d.ariaLabel=y,d.dataset.sortDirection="descending",d.addEventListener("click",h,!1);const u=document.createElement("span");u.classList.add("dropdown-item-columns"),d.appendChild(u);const S=document.createElement("span");S.classList.add("dropdown-item-column","dropdown-item-column-icon","text-primary"),u.appendChild(S);const C=new L;C.identifier="empty-empty",C.size=T.small,S.appendChild(C);const E=document.createElement("span");E.classList.add("dropdown-item-column","dropdown-item-column-title"),E.textContent=y,u.appendChild(E),p.appendChild(d),n.appendChild(p),n.getAttribute("data-sort-default")!==null&&(l=n)}}l&&(this.current=l,this.sortTable(l))}}class I{constructor(o){o.addEventListener("afterSort",g=>{const r=g.target;r.tHead.querySelectorAll(".dropdown-toggle[data-sorting-toggle]").forEach(s=>{s.querySelector(":scope > div").classList.remove("text-primary");const a=s.querySelector("typo3-backend-icon");a.identifier="empty-empty",a.classList.remove("text-primary")}),r.tHead.querySelectorAll(".dropdown-toggle[data-sorting-toggle] + .dropdown-menu typo3-backend-icon").forEach(s=>{s.identifier="empty-empty"});const t=r.tHead.querySelector("th[aria-sort]"),n=t.querySelector(".dropdown-toggle[data-sorting-toggle]");n.querySelector(":scope > div").classList.add("text-primary");const c=n.querySelector("typo3-backend-icon"),m=t.querySelector(".dropdown-menu");t.ariaSort==="ascending"?c.identifier="actions-sort-amount-up":c.identifier="actions-sort-amount-down",m.querySelectorAll(".dropdown-item").forEach(s=>{const i=s.dataset.sortDirection,a=s.querySelector("typo3-backend-icon");i===t.ariaSort?a.identifier="actions-dot":a.identifier="empty-empty"})}),new x(o)}}export{I as default};
