/*
* 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{Collapse as u}from"bootstrap";import{AbstractInteractableModule as g}from"@typo3/install/module/abstract-interactable-module.js";import m from"@typo3/backend/notification.js";import d from"@typo3/core/ajax/ajax-request.js";import r from"@typo3/install/router.js";import f from"@typo3/core/event/debounce-event.js";import"@typo3/backend/element/icon-element.js";import h from"@typo3/core/event/regular-event.js";var n;(function(i){i.fulltextSearch=".t3js-upgradeDocs-fulltext-search",i.changeLogsForVersionContainer=".t3js-version-changes",i.changeLogsForVersion=".t3js-changelog-list",i.selectPureField=".t3js-upgradeDocs-select-pure",i.upgradeDoc=".t3js-upgrade-doc"})(n||(n={}));class y extends g{initialize(o){super.initialize(o),this.loadModuleFrameAgnostic("select-pure").then(()=>{this.getContent()}),new h("click",(s,a)=>{this.markRead(a)}).delegateTo(o,".t3js-upgradeDocs-markRead"),new h("click",(s,a)=>{this.unmarkRead(a)}).delegateTo(o,".t3js-upgradeDocs-unmarkRead")}getContent(){const o=this.getModalBody();new d(r.getUrl("upgradeDocsGetContent")).get({cache:"no-cache"}).then(async s=>{const a=await s.resolve();a.success===!0&&a.html!=="undefined"&&a.html.length>0&&(o.innerHTML=a.html,this.initializeFullTextSearch(),this.initializeSelectPure(),this.loadChangelogs())},s=>{r.handleAjaxError(s,o)})}loadChangelogs(){const o=[],s=this.getModalBody();this.currentModal.querySelectorAll(n.changeLogsForVersionContainer).forEach(a=>{const e=new d(r.getUrl("upgradeDocsGetChangelogForVersion")).withQueryArguments({install:{version:a.dataset.version}}).get({cache:"no-cache"}).then(async t=>{const l=await t.resolve();if(l.success===!0){const c=a,p=c.querySelector(n.changeLogsForVersion);p.innerHTML=l.html,this.moveNotRelevantDocuments(p),c.querySelector(".t3js-panel-loading").remove()}else m.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},t=>{r.handleAjaxError(t,s)});o.push(e)}),Promise.all(o).then(()=>{this.fulltextSearchField.disabled=!1,this.appendItemsToSelectPure()})}initializeFullTextSearch(){this.fulltextSearchField=this.findInModal(n.fulltextSearch);const o=this.fulltextSearchField;new h("search",()=>{o.value===""&&this.combinedFilterSearch()}).bindTo(o),o.focus(),new f("keyup",()=>{this.combinedFilterSearch()}).bindTo(o)}initializeSelectPure(){this.selectPureField=this.getModalBody().querySelector(n.selectPureField),this.selectPureField.addEventListener("change",()=>{this.combinedFilterSearch(),this.selectPureField.close()})}appendItemsToSelectPure(){let o="";this.currentModal.querySelectorAll(n.upgradeDoc).forEach(e=>{o+=e.dataset.itemTags+","});const a=[...new Set(o.slice(0,-1).split(",")).values()].reduce((e,t)=>{const l=t.toLowerCase();return e.every(c=>c.toLowerCase()!==l)&&e.push(t),e},[]).sort((e,t)=>e.toLowerCase().localeCompare(t.toLowerCase()));this.selectPureField.enable();for(const e of a){const t=this.selectPureField.ownerDocument.createElement("option-pure");t.textContent=e,t.setAttribute("value",e),this.selectPureField.appendChild(t)}}combinedFilterSearch(){const o=this.getModalBody(),s=o.querySelectorAll(n.upgradeDoc);if(this.selectPureField.values.length<1&&this.fulltextSearchField.value.length<1){this.currentModal.querySelectorAll(".panel-version .panel-collapse.show").forEach(t=>{new h("hidden.bs.collapse",()=>{this.currentModal.querySelectorAll(".panel-version .panel-collapse.collapsing").length===0&&s.forEach(l=>{l.classList.remove("hidden","searchhit","filterhit")})},{once:!0}).bindTo(t),u.getOrCreateInstance(t).hide()});return}if(s.forEach(e=>{e.classList.remove("searchhit","filterhit")}),this.selectPureField.values.length>0){s.forEach(t=>{t.classList.add("hidden"),t.classList.remove("filterhit")});const e=this.selectPureField.values.map(t=>'[data-item-tags*="'+t+'"]').join("");o.querySelectorAll(e).forEach(t=>{t.classList.remove("hidden"),t.classList.add("searchhit","filterhit")})}else s.forEach(e=>{e.classList.add("filterhit"),e.classList.remove("hidden")});const a=this.fulltextSearchField.value;o.querySelectorAll(".filterhit").forEach(e=>{e.textContent.toLowerCase().trim().includes(a.toLowerCase())?(e.classList.remove("hidden"),e.classList.add("searchhit")):(e.classList.remove("searchhit"),e.classList.add("hidden"))}),o.querySelectorAll(".searchhit").forEach(e=>{const t=e.closest(".panel-collapse");window.setTimeout(()=>{u.getOrCreateInstance(t).show()},20)}),o.querySelectorAll(".panel-version").forEach(e=>{if(e.querySelectorAll(".searchhit, .filterhit").length<1){const t=e.querySelector(":scope > .panel-collapse");u.getOrCreateInstance(t).hide()}})}moveNotRelevantDocuments(o){this.findInModal(".panel-body-read").append(o.querySelector('[data-item-state="read"]')??""),this.findInModal(".panel-body-not-affected").append(o.querySelector('[data-item-state="notAffected"]')??"")}markRead(o){const s=this.getModalBody(),a=this.getModuleContent().dataset.upgradeDocsMarkReadToken,e=o.closest("button");e.classList.toggle("t3js-upgradeDocs-unmarkRead"),e.classList.toggle("t3js-upgradeDocs-markRead"),e.querySelectorAll("typo3-backend-icon,.t3js-icon").forEach(t=>{t.outerHTML=''}),this.findInModal(".panel-body-read").append(e.closest(".panel")),new d(r.getUrl()).post({install:{ignoreFile:e.dataset.filepath,token:a,action:"upgradeDocsMarkRead"}}).catch(t=>{r.handleAjaxError(t,s)})}unmarkRead(o){const s=this.getModalBody(),a=this.getModuleContent().dataset.upgradeDocsUnmarkReadToken,e=o.closest("button"),t=e.closest(".panel").dataset.itemVersion;e.classList.toggle("t3js-upgradeDocs-markRead"),e.classList.toggle("t3js-upgradeDocs-unmarkRead"),e.querySelectorAll("typo3-backend-icon,.t3js-icon").forEach(l=>{l.outerHTML=''}),this.findInModal('*[data-group-version="'+t+'"] .panel-body').append(e.closest(".panel")),new d(r.getUrl()).post({install:{ignoreFile:e.dataset.filepath,token:a,action:"upgradeDocsUnmarkRead"}}).catch(l=>{r.handleAjaxError(l,s)})}}var S=new y;export{S as default};