/*
 * 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{MessageUtility as g}from"@typo3/backend/utility/message-utility.js";import p from"@typo3/backend/element-browser.js";import a from"nprogress";import c from"@typo3/core/event/regular-event.js";import f from"@typo3/backend/icons.js";import{FileListActionSelector as h,FileListActionUtility as w,FileListActionEvent as s}from"@typo3/filelist/file-list-actions.js";import y from"@typo3/backend/info-window.js";import b from"@typo3/core/ajax/ajax-request.js";class l{constructor(){this.importSelection=e=>{e.preventDefault();const t=e.target,n=e.detail.checkboxes;if(!n.length)return;const i=[];if(n.forEach(o=>{if(o.checked){const m=o.closest(h.elementSelector),r=w.getResourceForElement(m);r.type==="file"&&r.uid&&i.unshift(r)}}),!i.length)return;f.getIcon("spinner-circle",f.sizes.small,null,null,f.markupIdentifiers.inline).then(o=>{t.classList.add("disabled"),t.innerHTML=o}),a.configure({parent:".element-browser-main-content",showSpinner:!1}),a.start();const d=1/i.length;l.handleNext(i),new c("message",o=>{if(!g.verifyOrigin(o.origin))throw"Denied message sent by "+o.origin;o.data.actionName==="typo3:foreignRelation:inserted"&&(i.length>0?(a.inc(d),l.handleNext(i)):(a.done(),p.focusOpenerAndClose()))}).bindTo(window)},new c(s.primary,e=>{e.preventDefault();const t=e.detail;t.originalAction=s.primary,t.action=s.select,document.dispatchEvent(new CustomEvent(s.select,{detail:t}))}).bindTo(document),new c(s.select,e=>{e.preventDefault();const t=e.detail,n=t.resources[0];n.type==="file"&&l.insertElement(n.name,n.uid,t.originalAction===s.primary),n.type==="folder"&&this.loadContent(n)}).bindTo(document),new c(s.show,e=>{e.preventDefault();const n=e.detail.resources[0];y.showItem("_"+n.type.toUpperCase(),n.identifier)}).bindTo(document),new c("multiRecordSelection:action:import",this.importSelection).bindTo(document)}static insertElement(e,t,n){return p.insertElement("sys_file",String(t),e,String(t),n)}static handleNext(e){if(e.length>0){const t=e.pop();l.insertElement(t.name,Number(t.uid))}}async loadContent(e){if(e.type!=="folder")return;const t=document.location.href+"&contentOnly=1&expandFolder="+e.identifier,i=await(await new b(t).get()).resolve(),d=document.querySelector(".element-browser-main-content .element-browser-body");d.innerHTML=i;const o=document.querySelector("typo3-backend-component-filestorage-browser-tree");if(o){const m=encodeURIComponent(e.identifier),r=o.nodes.find(u=>u.identifier===m);r&&(await o.expandNodeParents(r),o.selectNode(r,!1))}}}var E=new l;export{E as default};
