/*
 * 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"bootstrap";import j from"@typo3/core/ajax/ajax-request.js";import{AbstractInteractableModule as E}from"@typo3/install/module/abstract-interactable-module.js";import k from"@typo3/backend/modal.js";import p from"@typo3/backend/notification.js";import M from"@typo3/install/ajax/ajax-queue.js";import S from"@typo3/install/router.js";import b from"@typo3/core/event/regular-event.js";var i;(function(h){h.extensionContainer=".t3js-extensionScanner-extension",h.numberOfFiles=".t3js-extensionScanner-number-of-files",h.scanSingleTrigger=".t3js-extensionScanner-scan-single",h.extensionScanButton=".t3js-extensionScanner-scan-all"})(i||(i={}));class I extends E{constructor(){super(...arguments),this.listOfAffectedRestFileHashes=[]}initialize(n){super.initialize(n),Promise.all([this.loadModuleFrameAgnostic("@typo3/backend/element/progress-bar-element.js")]).then(()=>{this.getData()}),new b("typo3-modal-hide",()=>{M.flush()}).bindTo(n),new b("click",(s,t)=>{s.preventDefault();const e=t.closest(i.extensionContainer).dataset.extension;this.scanSingleExtension(e)}).delegateTo(n,i.scanSingleTrigger),new b("click",s=>{s.preventDefault(),this.setModalButtonsState(!1);const t=n.querySelectorAll(i.extensionContainer);this.scanAll(t)}).delegateTo(n,i.extensionScanButton)}getData(){const n=this.getModalBody();new j(S.getUrl("extensionScannerGetData")).get().then(async s=>{const t=await s.resolve();t.success===!0?(n.innerHTML=t.html,k.setButtons(t.buttons),this.setupEventListeners()):p.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},s=>{S.handleAjaxError(s,n)})}setupEventListeners(){this.currentModal.querySelectorAll(i.extensionContainer).forEach(n=>{new b("show.bs.collapse",s=>{const t=s.currentTarget;if(typeof t.dataset.scanned>"u"){const e=t.dataset.extension;this.scanSingleExtension(e),t.dataset.scanned=String(!0)}}).bindTo(n)})}getExtensionSelector(n){return i.extensionContainer+"-"+n}async scanAll(n){n.forEach(t=>{t.classList.remove("panel-danger","panel-warning","panel-success");const e=t.querySelector(".panel-progress-bar");e.style.width=String(0),e.setAttribute("aria-valuenow",String(0)),e.querySelector("span").innerText="0%"}),this.setProgressForAll();const s=[...n].map(async t=>{const e=t.dataset.extension;try{await this.scanSingleExtension(e)}finally{t.dataset.scanned=String(!0)}});try{await Promise.allSettled(s)}finally{this.setModalButtonsState(!0),p.success("Scan finished","All extensions have been scanned.");try{const e=await(await new j(S.getUrl()).post({install:{action:"extensionScannerMarkFullyScannedRestFiles",token:this.getModuleContent().dataset.extensionScannerMarkFullyScannedRestFilesToken,hashes:Array.from(new Set(this.listOfAffectedRestFileHashes))}})).resolve();e.success===!0&&p.success("Marked not affected files","Marked "+e.markedAsNotAffected+" ReST files as not affected.")}catch(t){S.handleAjaxError(t,this.getModalBody())}}}setStatusMessageForScan(n,s,t){const y=this.findInModal(this.getExtensionSelector(n)).querySelector(i.numberOfFiles);y.innerText="Checked "+s+" of "+t+" files"}setProgressForScan(n,s,t){const e=s/t*100,g=this.findInModal(this.getExtensionSelector(n)).querySelector(".panel-progress-bar");g.style.width=e+"%",g.setAttribute("aria-valuenow",String(e)),g.querySelector("span").innerText=e+"%"}setProgressForAll(){const n=this.currentModal.querySelectorAll(i.extensionContainer).length,s=this.currentModal.querySelectorAll(i.extensionContainer+".t3js-extensionscan-finished").length,t=`Scanning extensions (${s} of ${n} done)\u2026`,e=this.findInModal(".t3js-extensionScanner-progress-all-extension");e.removeAttribute("hidden"),e.max=n,e.value=s,e.label=t}async scanSingleExtension(n){const s=this.getModuleContent().dataset.extensionScannerFilesToken,t=this.getModalBody(),e=this.findInModal(this.getExtensionSelector(n)),y="#t3js-extensionScanner-file-hit-template",g="#t3js-extensionScanner-file-hit-rest-template";let q=!1;e.classList.add("panel-default"),e.classList.remove("panel-danger","panel-warning","panel-success","t3js-extensionscan-finished"),e.dataset.hasRun="true";const T=e.querySelector(".t3js-extensionScanner-scan-single");T.innerText="Scanning...",T.disabled=!0,e.querySelector(".t3js-extensionScanner-extension-body-loc").innerText="0",e.querySelector(".t3js-extensionScanner-extension-body-ignored-files").innerText="0",e.querySelector(".t3js-extensionScanner-extension-body-ignored-lines").innerText="0",this.setProgressForAll();try{const f=await(await new j(S.getUrl()).post({install:{action:"extensionScannerFiles",token:s,extension:n}})).resolve();if(f.success===!0&&Array.isArray(f.files)){const x=f.files.length;if(x<=0){p.warning("No files found","The extension "+n+" contains no scannable files");return}this.setStatusMessageForScan(n,0,x),e.querySelector(".t3js-extensionScanner-extension-body").innerText="",e.classList.add("panel-has-progress");let c=0;const F=f.files.map(v=>new Promise((P,C)=>{M.add({method:"POST",data:{install:{action:"extensionScannerScanFile",token:this.getModuleContent().dataset.extensionScannerScanFileToken,extension:n,file:v}},url:S.getUrl(),onfulfilled:async m=>{const d=await m.resolve();if(c++,this.setStatusMessageForScan(n,c,x),this.setProgressForScan(n,c,x),d.success&&Array.isArray(d.matches)&&d.matches.forEach(a=>{q=!0;const r=t.querySelector(y+" .panel").cloneNode(!0);r.querySelector(".t3js-extensionScanner-hit-file-panel-head").setAttribute("data-bs-target","#collapse"+a.uniqueId),r.querySelector(".t3js-extensionScanner-hit-file-panel-head").setAttribute("aria-controls","collapse"+a.uniqueId),r.querySelector(".t3js-extensionScanner-hit-file-panel-body").setAttribute("id","collapse"+a.uniqueId),r.querySelector(".t3js-extensionScanner-hit-filename").innerText=v,r.querySelector(".t3js-extensionScanner-hit-message").innerText=a.message,a.indicator==="strong"?r.querySelector(".t3js-extensionScanner-hit-file-panel-head .t3js-extensionScanner-hit-badges").innerHTML+='<span class="badge badge-danger" title="Reliable match, false positive unlikely">strong</span>':r.querySelector(".t3js-extensionScanner-hit-file-panel-head .t3js-extensionScanner-hit-badges").innerHTML+='<span class="badge badge-warning" title="Probable match, but can be a false positive">weak</span>',a.silenced===!0&&(r.querySelector(".t3js-extensionScanner-hit-file-panel-head .t3js-extensionScanner-hit-badges").innerHTML+='<span class="badge badge-info" title="Match has been annotated by extension author as false positive match">silenced</span>'),r.querySelector(".t3js-extensionScanner-hit-file-lineContent").innerText=a.lineContent,r.querySelector(".t3js-extensionScanner-hit-file-line").innerText=a.line+": ",Array.isArray(a.restFiles)&&a.restFiles.forEach(o=>{const l=t.querySelector(g+" .panel").cloneNode(!0);l.querySelector(".t3js-extensionScanner-hit-rest-panel-head").setAttribute("data-bs-target","#collapse"+o.uniqueId),l.querySelector(".t3js-extensionScanner-hit-rest-panel-head").setAttribute("aria-controls","collapse"+o.uniqueId),l.querySelector(".t3js-extensionScanner-hit-rest-panel-head .t3js-extensionScanner-hit-rest-badge").innerText=o.version,l.querySelector(".t3js-extensionScanner-hit-rest-panel-body").setAttribute("id","collapse"+o.uniqueId),l.querySelector(".t3js-extensionScanner-hit-rest-headline").innerText=o.headline,l.querySelector(".t3js-extensionScanner-hit-rest-body").innerText=o.content,l.classList.add("panel-"+o.class),r.querySelector(".t3js-extensionScanner-hit-file-rest-container").append(l),this.listOfAffectedRestFileHashes.push(o.file_hash)});const u=r.querySelectorAll(".panel-breaking, .t3js-extensionScanner-hit-file-rest-container").length>0?"panel-danger":"panel-warning";r.classList.add(u),r.classList.remove("panel-default");const L=e.querySelector(".t3js-extensionScanner-extension-body");L.classList.remove("hide"),L.append(r),e.classList.remove("panel-default"),u==="panel-danger"&&(e.classList.remove("panel-warning"),e.classList.add(u)),u==="panel-warning"&&!e.classList.contains("panel-danger")&&e.classList.add(u)}),d.success){const a=parseInt(e.querySelector(".t3js-extensionScanner-extension-body-loc").innerText,10);if(e.querySelector(".t3js-extensionScanner-extension-body-loc").innerText=String(a+d.effectiveCodeLines),d.isFileIgnored){const u=parseInt(e.querySelector(".t3js-extensionScanner-extension-body-ignored-files").innerText,10);e.querySelector(".t3js-extensionScanner-extension-body-ignored-files").innerText=String(u+1)}const r=parseInt(e.querySelector(".t3js-extensionScanner-extension-body-ignored-lines").innerText,10);e.querySelector(".t3js-extensionScanner-extension-body-ignored-lines").innerText=String(r+d.ignoredLines)}P()},onrejected:m=>{C(),c=c+1,this.setStatusMessageForScan(n,c,x),this.setProgressForScan(n,c,x),e.classList.remove("panel-has-progress"),this.setProgressForAll(),console.error(m)}})}));await Promise.allSettled(F),q||(e.classList.remove("panel-default"),e.classList.add("panel-success")),e.classList.add("t3js-extensionscan-finished"),e.classList.remove("panel-has-progress"),this.setProgressForAll();const w=e.querySelector(".t3js-extensionScanner-scan-single");w.innerText="Rescan",w.disabled=!1}else p.error("Oops, an error occurred","Please look at the browser console output for details"),console.error(f)}catch(A){S.handleAjaxError(A,t)}}}var O=new I;export{O as default};
