/*
 * 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{html as l,LitElement as m}from"lit";import{customElement as f,query as u}from"lit/decorators.js";import h from"@typo3/core/ajax/ajax-request.js";import"@typo3/backend/tree/tree-toolbar.js";import g from"@typo3/backend/element-browser.js";import b from"@typo3/backend/link-browser.js";import"@typo3/backend/element/icon-element.js";import{FileStorageTree as w}from"@typo3/backend/tree/file-storage-tree.js";var d=function(n,e,t,r){var o=arguments.length,i=o<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,s;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(s=n[c])&&(i=(o<3?s(i):o>3?s(e,t,i):s(e,t))||i);return o>3&&i&&Object.defineProperty(e,t,i),i};let p=class extends w{createNodeContentAction(e){return this.settings.actions.includes("link")?l`<span class=node-action @click=${()=>this.linkItem(e)}> <typo3-backend-icon identifier=actions-link size=small></typo3-backend-icon> </span>`:this.settings.actions.includes("select")?l`<span class=node-action @click=${()=>this.selectItem(e)}> <typo3-backend-icon identifier=actions-link size=small></typo3-backend-icon> </span>`:super.createNodeContentAction(e)}linkItem(e){b.finalizeFunction("t3://folder?storage="+e.storage+"&identifier="+e.pathIdentifier)}selectItem(e){g.insertElement(e.recordType,e.identifier,e.name,e.identifier,!0)}};p=d([f("typo3-backend-component-filestorage-browser-tree")],p);let a=class extends m{constructor(){super(...arguments),this.activeFolder="",this.actions=[],this.selectActiveNode=e=>{const t=e.detail.nodes;e.detail.nodes=t.map(r=>(decodeURIComponent(r.identifier)===this.activeFolder&&(r.checked=!0),r))},this.loadFolderDetails=e=>{const t=e.detail.node;if(!t.checked)return;const r=document.location.href+"&contentOnly=1&expandFolder="+t.identifier;new h(r).get().then(o=>o.resolve()).then(o=>{const i=document.querySelector(".element-browser-main-content .element-browser-body");i.innerHTML=o})}}firstUpdated(){this.activeFolder=this.getAttribute("active-folder")||""}createRenderRoot(){return this}render(){this.hasAttribute("tree-actions")&&this.getAttribute("tree-actions").length&&(this.actions=JSON.parse(this.getAttribute("tree-actions")));const e={dataUrl:top.TYPO3.settings.ajaxUrls.filestorage_tree_data,rootlineUrl:top.TYPO3.settings.ajaxUrls.filestorage_tree_rootline,filterUrl:top.TYPO3.settings.ajaxUrls.filestorage_tree_filter,showIcons:!0,actions:this.actions},t=()=>{const r=this.querySelector("typo3-backend-tree-toolbar");r.tree=this.tree,this.activeFolder&&this.expandToActiveFolder()};return l`<typo3-backend-tree-toolbar .tree=${this.tree}></typo3-backend-tree-toolbar><typo3-backend-component-filestorage-browser-tree .setup=${e} @tree:initialized=${t} @typo3:tree:node-selected=${this.loadFolderDetails} @typo3:tree:nodes-prepared=${this.selectActiveNode}></typo3-backend-component-filestorage-browser-tree>`}async expandToActiveFolder(){if(!this.activeFolder||!this.tree.settings.rootlineUrl)return;const e=this.tree.nodes.find(t=>decodeURIComponent(t.identifier)===this.activeFolder);if(e){await this.tree.expandNodeParents(e);return}try{const t=new URL(this.tree.settings.rootlineUrl,window.location.origin);t.searchParams.set("identifier",this.activeFolder);const r=await new h(t.toString()).get({cache:"no-cache"}),{rootline:o}=await r.resolve();o&&o.length>0&&(o.pop(),await this.tree.expandParents(o.map(i=>encodeURIComponent(i))))}catch(t){console.debug("Could not expand to active folder:",t)}}};d([u("typo3-backend-component-filestorage-browser-tree")],a.prototype,"tree",void 0),a=d([f("typo3-backend-component-filestorage-browser")],a);export{a as FileStorageBrowser,p as FileStorageBrowserTree};
