/*
 * 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 i from"jquery";import*as I from"@typo3/form/backend/form-editor/helper.js";import _ from"@typo3/backend/icons.js";import T from"sortablejs";var u;(function(e){e.hidden="hidden",e.connect="connect",e.line="line",e.last="last"})(u||(u={}));const K={domElementClassNames:{collapsed:"collapsed",expanded:"expanded",hasChildren:"has-children",sortable:"sortable",noNesting:"no-nesting"},domElementDataAttributeNames:{abstractType:"data-element-abstract-type"},domElementDataAttributeValues:{collapse:"actions-chevron-end",expander:"treeExpander",title:"treeTitle"},isSortable:!0};let P=null,F=null,f=null;const p={};function d(){return F}function r(e){return m().isUndefinedOrNull(e)?I.setConfiguration(P):I.setConfiguration(e)}function m(){return d().getUtility()}function N(e,t,n){return d().assert(e,t,n)}function A(){return d().getRootFormElement()}function W(){return d().getCurrentlySelectedFormElement()}function h(){return d().getPublisherSubscriber()}function b(e,t){return d().getFormElementDefinition(e,t)}function C(e){const t=document.createElement("span");return t.classList.add("formeditor-tree-line","formeditor-tree-line--"+e),t}function k(e,t,n){N(i.type(e)==="object",'Invalid parameter "formElement"',1478715704);const a=i("<li></li>");b(e,"_isCompositeFormElement")||a.addClass(r().getDomElementClassName("noNesting"));const l=i("<div></div>").addClass("formeditor-tree-item").attr(r().getDomElementDataAttribute("elementIdentifier"),e.get("__identifierPath")).append(i("<span></span>").addClass("formeditor-tree-title").attr(r().getDomElementDataAttribute("identifier"),r().getDomElementDataAttributeValue("title")).append(S(e)));b(e,"_isCompositeFormElement")&&l.attr(r().getDomElementDataAttribute("abstractType"),"isCompositeFormElement"),b(e,"_isTopLevelFormElement")&&l.attr(r().getDomElementDataAttribute("abstractType"),"isTopLevelFormElement");const s=document.createElement("span");s.classList.add("caret");const o=i("<span></span>").addClass("formeditor-tree-expander").attr("data-identifier",r().getDomElementDataAttributeValue("expander")).append(s);l.prepend(o),_.getIcon(b(e,"iconIdentifier"),_.sizes.small,null,_.states.default).then(function(D){o.after(i("<span></span>").addClass("formeditor-tree-icon").addClass(r().getDomElementClassName("icon")).attr("title","id = "+e.get("identifier")).append(D)),b(e,"_isCompositeFormElement")?e.get("renderables")&&e.get("renderables").length>0?(o.before(C(t!=n?u.connect:u.last)),a.addClass(r().getDomElementClassName("hasChildren"))):o.before(C(u.connect)).remove():(l.prepend(C(t!=n?u.connect:u.last)),o.remove());let c=e.get("__parentRenderable");for(;c&&c.get("__identifierPath")!==A().get("__identifierPath");)c.get("__identifierPath")===d().getLastFormElementWithinParentFormElement(c).get("__identifierPath")?l.prepend(C(u.hidden)):l.prepend(C(u.line)),c=c.get("__parentRenderable")}),a.append(l),h().publish("view/tree/render/listItemAdded",[a,e]);const g=e.get("renderables");let y=null;if(i.type(g)==="array"){y=i("<ol></ol>");for(let D=0,c=g.length;D<c;++D)y.append(k(g[D],D+1,c))}return y&&a.append(y),a}function j(){const e={handle:"div"+r().getDomElementDataAttribute("elementIdentifier","bracesWithKey"),draggable:"li",animation:200,fallbackTolerance:200,fallbackOnBody:!0,swapThreshold:.6,dragClass:"formeditor-sortable-drag",ghostClass:"formeditor-sortable-ghost",onChange:function(n){let a;const l=L(i(n.item));l&&(a=d().findEnclosingCompositeFormElementWhichIsNotOnTopLevel(l)),h().publish("view/tree/dnd/change",[i(n.item),l,a])},onEnd:function(n){const a=v(i(n.item)),l=x(i(n.item),"prev"),s=x(i(n.item),"next");h().publish("view/tree/dnd/update",[i(n.item),a,l,s]),h().publish("view/tree/dnd/stop",[v(i(n.item))])}},t=f.get(0).querySelector("ol."+r().getDomElementClassName("sortable"));new T(t,{...e,group:"tree-step-nodes",put:["tree-step-nodes"]}),t.querySelectorAll("ol").forEach(function(n){new T(n,{...e,group:"tree-leaves-nodes",pull:["tree-leaves-nodes"]})})}function V(){const e=function(t){if(b(t,"_isCompositeFormElement")){const a=E(t);a.length&&(a.closest("li").hasClass(r().getDomElementClassName("expanded"))?p[t.get("__identifierPath")]=!0:p[t.get("__identifierPath")]=!1),m().isUndefinedOrNull(p[t.get("__identifierPath")])&&(p[t.get("__identifierPath")]=!0)}const n=t.get("renderables");if(i.type(n)==="array")for(let a=0,l=n.length;a<l;++a)e(n[a])};e(A());for(const t of Object.keys(p))try{d().getFormElementByIdentifierPath(t)}catch{delete p[t]}}function R(){for(const e of Object.keys(p)){const t=E(e);t.length&&(p[e]?t.closest("li").removeClass(r().getDomElementClassName("collapsed")).addClass(r().getDomElementClassName("expanded")):t.closest("li").addClass(r().getDomElementClassName("collapsed")).removeClass(r().getDomElementClassName("expanded")))}}function w(e){N(i.type(e)==="object",'Invalid parameter "formElement"',1478721208);const t=i("<ol></ol>").addClass(r().getDomElementClassName("sortable"));if(i.type(e.get("renderables"))==="array")for(let n=0,a=e.get("renderables").length;n<a;++n)t.append(k(e.get("renderables")[n],n+1,a));return t}function B(e){d().getUtility().isUndefinedOrNull(e)&&(e=A()),V(),f.off().empty().append(w(e));let t=0;f.on("click",function(n){const a=i(n.target).closest(r().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).attr(r().getDomElementDataAttribute("elementIdentifier"));m().isUndefinedOrNull(a)||!m().isNonEmptyString(a)||(t++,t===1&&setTimeout(function(){t===1?h().publish("view/tree/node/clicked",[a]):$(a),t=0},300))}),i(r().getDomElementDataIdentifierSelector("expander"),f).on("click",function(){i(this).closest("li").toggleClass(r().getDomElementClassName("collapsed")).toggleClass(r().getDomElementClassName("expanded"))}),P.isSortable&&j(),R()}function q(){return i(r().getDomElementDataAttribute("elementIdentifier","bracesWithKey"),f)}function O(e){return i(e).find(r().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).first()}function v(e){return O(i(e)).attr(r().getDomElementDataAttribute("elementIdentifier"))}function U(e){return i(e).parent().closest("li").find(r().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).first()}function L(e){return U(e).attr(r().getDomElementDataAttribute("elementIdentifier"))}function x(e,t){m().isUndefinedOrNull(t)&&(t="prev");const n=v(e);return e=t==="prev"?i(e).prev("li"):i(e).next("li"),e.find(r().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).not(r().getDomElementDataAttribute("elementIdentifier","bracesWithKeyValue",[n])).first().attr(r().getDomElementDataAttribute("elementIdentifier"))}function H(e,t){let n;m().isUndefinedOrNull(e)?n=S(t):(n=document.createElement("span"),n.textContent=e),i(r().getDomElementDataIdentifierSelector("title"),E(t)).get(0).replaceChildren(n)}function E(e){let t;return typeof e=="string"?t=e:m().isUndefinedOrNull(e)?t=W().get("__identifierPath"):t=e.get("__identifierPath"),i(r().getDomElementDataAttribute("elementIdentifier","bracesWithKeyValue",[t]),f)}function S(e){m().isUndefinedOrNull(e)&&(e=W()),N(i.type(e)==="object",'Invalid parameter "formElement"',1478719287);const t=document.createElement("span");t.textContent=e.get("label")?e.get("label"):e.get("identifier");const n=document.createElement("small");return n.textContent="("+b(e,"label")+")",t.appendChild(n),t}function z(){return f}function $(e){const t=E(e),a=i(r().getDomElementDataIdentifierSelector("title"),t).children()[0].childNodes[0].nodeValue.trim();let l=!0;const s=i("<input>").attr("class","formeditor-tree-edit").attr("type","text").attr("value",a).on("click",o=>{o.stopPropagation()}).on("keyup",function(o){if(o.keyCode===13||o.keyCode===9){const g=this.value.trim();m().isNonEmptyString(g)&&g!==a?(l=!1,s.remove(),h().publish("view/tree/node/changed",[e,g])):(l=!1,s.remove())}else o.keyCode===27&&(l=!1,s.remove())}).on("blur",function(){if(l){const o=this.value.trim();s.remove(),m().isNonEmptyString(o)&&o!==a&&h().publish("view/tree/node/changed",[e,o])}});t.append(s),s.focus()}function G(e,t,n){return F=e,N(i.type(t)==="object",'Invalid parameter "appendToDomElement"',1478714814),f=i(t),P=i.extend(!0,K,n||{}),I.bootstrap(F),this}export{G as bootstrap,S as buildTitleByFormElement,q as getAllTreeNodes,L as getParentTreeNodeIdentifierPathWithinDomElement,U as getParentTreeNodeWithinDomElement,x as getSiblingTreeNodeIdentifierPathWithinDomElement,z as getTreeDomElement,E as getTreeNode,v as getTreeNodeIdentifierPathWithinDomElement,O as getTreeNodeWithinDomElement,w as renderCompositeFormElementChildsAsSortableList,B as renew,H as setTreeNodeTitle};
