/*
 * 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 e from"jquery";import*as S from"@typo3/form/backend/form-editor/helper.js";import E from"@typo3/backend/icons.js";import M from"sortablejs";const G={domElementClassNames:{formElementIsComposit:"formeditor-element-composit",formElementIsTopLevel:"formeditor-element-toplevel",noNesting:"no-nesting",selected:"selected",sortable:"sortable",previewViewPreviewElement:"formeditor-element-preview"},domElementDataAttributeNames:{abstractType:"data-element-abstract-type",noSorting:"data-no-sorting"},domElementDataAttributeValues:{abstractViewToolbar:"elementToolbar",abstractViewToolbarNewElement:"stageElementToolbarNewElement",abstractViewToolbarNewElementSplitButton:"stageElementToolbarNewElementSplitButton",abstractViewToolbarNewElementSplitButtonAfter:"stageElementToolbarNewElementSplitButtonAfter",abstractViewToolbarNewElementSplitButtonInside:"stageElementToolbarNewElementSplitButtonInside",abstractViewToolbarRemoveElement:"stageElementToolbarRemoveElement",buttonHeaderRedo:"redoButton",buttonHeaderUndo:"undoButton",buttonPaginationPrevious:"buttonPaginationPrevious",buttonPaginationNext:"buttonPaginationNext","FormElement-_ElementToolbar":"FormElement-_ElementToolbar","FormElement-_UnknownElement":"FormElement-_UnknownElement","FormElement-AdvancedPassword":"FormElement-AdvancedPassword","FormElement-Checkbox":"FormElement-Checkbox","FormElement-ContentElement":"FormElement-ContentElement","FormElement-CountrySelect":"FormElement-CountrySelect","FormElement-DatePicker":"FormElement-DatePicker","FormElement-Fieldset":"FormElement-Fieldset","FormElement-GridColumn":"FormElement-GridColumn","FormElement-GridRow":"FormElement-GridRow","FormElement-FileUpload":"FormElement-FileUpload","FormElement-Hidden":"FormElement-Hidden","FormElement-ImageUpload":"FormElement-ImageUpload","FormElement-MultiCheckbox":"FormElement-MultiCheckbox","FormElement-MultiSelect":"FormElement-MultiSelect","FormElement-Page":"FormElement-Page","FormElement-Password":"FormElement-Password","FormElement-RadioButton":"FormElement-RadioButton","FormElement-SingleSelect":"FormElement-SingleSelect","FormElement-StaticText":"FormElement-StaticText","FormElement-SummaryPage":"FormElement-SummaryPage","FormElement-Text":"FormElement-Text","FormElement-Textarea":"FormElement-Textarea","FormElement-Email":"FormElement-Email","FormElement-Url":"FormElement-Url","FormElement-Telephone":"FormElement-Telephone","FormElement-Number":"FormElement-Number","FormElement-Date":"FormElement-Date",formElementIcon:"elementIcon",iconValidator:"form-validator",multiValueContainer:"multiValueContainer",paginationTitle:"paginationTitle",stageHeadline:"formDefinitionLabel",stagePanel:"stagePanel",validatorsContainer:"validatorsContainer",validatorIcon:"validatorIcon"},isSortable:!0};let C=null,I=null,f=null;function d(){return I}function o(t){return u().isUndefinedOrNull(t)?S.setConfiguration(C):S.setConfiguration(t)}function u(){return d().getUtility()}function p(){return d().getViewModel()}function b(t,n,r){return d().assert(t,n,r)}function F(){return d().getRootFormElement()}function x(){return d().getCurrentlySelectedFormElement()}function g(){return d().getPublisherSubscriber()}function c(t,n){return d().getFormElementDefinition(t,n)}function q(t,n){u().isNonEmptyString(n)&&e(t).text(n)}function z(t,n){switch(t.get("type")){case"Checkbox":O(t,n);break;case"FileUpload":case"ImageUpload":K(t,n);break;case"CountrySelect":case"SingleSelect":case"RadioButton":case"MultiSelect":case"MultiCheckbox":H(t,n);break;case"Textarea":case"AdvancedPassword":case"Password":case"Text":case"Email":case"Url":case"Telephone":case"Number":case"DatePicker":case"Date":y(t,n);break;case"Fieldset":case"GridColumn":case"GridRow":case"SummaryPage":case"Page":case"StaticText":case"Hidden":case"ContentElement":N(t,n);break;default:break}g().publish("view/stage/abstract/render/template/perform",[t,n])}function k(t){let n,r;const a=e("<li></li>");c(t,"_isCompositeFormElement")||a.addClass(o().getDomElementClassName("noNesting")),c(t,"_isTopLevelFormElement")&&a.addClass(o().getDomElementClassName("formElementIsTopLevel")),c(t,"_isCompositeFormElement")&&a.addClass(o().getDomElementClassName("formElementIsComposit"));try{r=o().getTemplate("FormElement-"+t.get("type")).clone()}catch{r=o().getTemplate("FormElement-_UnknownElement").clone(),b(r.length>0,'No template found for element "'+t.get("__identifierPath")+'"',1478987818)}r=e("<div></div>").attr(o().getDomElementDataAttribute("elementIdentifier"),t.get("__identifierPath")).append(e(r.html()));const i=c(t,"_isCompositeFormElement");i&&r.attr(o().getDomElementDataAttribute("abstractType"),"isCompositeFormElement");const m=c(t,"_isTopLevelFormElement");if(m?r.attr(o().getDomElementDataAttribute("abstractType"),"isTopLevelFormElement"):r.addClass("formeditor-element"),a.append(r),z(t,r),m||i){n=e("<ol></ol>"),n.addClass(o().getDomElementClassName("sortable")),n.addClass("formeditor-list");const l=t.get("renderables");if(e.type(l)==="array")for(let s=0,D=l.length;s<D;++s)n.append(k(l[s]));a.append(n)}return a}function $(){const t=f.get(0).querySelectorAll("ol."+o().getDomElementClassName("sortable")),n="li:not("+o().getDomElementDataAttribute("noSorting","bracesWithKey")+")",r="div"+o().getDomElementDataAttribute("elementIdentifier","bracesWithKey");t.forEach(function(a){a.querySelectorAll(r).forEach(function(i){i.classList.add("formeditor-sortable-handle")}),new M(a,{group:"stage-nodes",handle:r,draggable:n,animation:200,swapThreshold:.6,dragClass:"formeditor-sortable-drag",ghostClass:"formeditor-sortable-ghost",onStart:function(i){g().publish("view/stage/abstract/dnd/start",[e(i.item),e(i.item)])},onChange:function(i){let m;const l=R(e(i.item));l&&(m=d().findEnclosingCompositeFormElementWhichIsNotOnTopLevel(l)),g().publish("view/stage/abstract/dnd/change",[e(i.item),l,m])},onEnd:function(i){const m=h(e(i.item)),l=P(e(i.item),"prev"),s=P(e(i.item),"next");g().publish("view/stage/abstract/dnd/update",[e(i.item),m,l,s]),g().publish("view/stage/abstract/dnd/stop",[h(e(i.item))])}})})}function J(){return f}function _(t){u().isUndefinedOrNull(t)&&(t=F()),b(e.type(t)==="object",'Invalid parameter "formElement"',1479037151);const n=document.createElement("span");return n.textContent=t.get("label")?t.get("label"):t.get("identifier"),n}function Q(t){u().isUndefinedOrNull(t)&&(t=_().textContent),e(o().getDomElementDataIdentifierSelector("stageHeadline")).text(t)}function X(){return e(o().getDomElementDataIdentifierSelector("stagePanel"))}function Y(){const t=F().get("renderables").length;p().enableButton(e(o().getDomElementDataIdentifierSelector("buttonPaginationPrevious"))),p().enableButton(e(o().getDomElementDataIdentifierSelector("buttonPaginationNext"))),d().getCurrentlySelectedPageIndex()===0&&p().disableButton(e(o().getDomElementDataIdentifierSelector("buttonPaginationPrevious"))),(t===1||d().getCurrentlySelectedPageIndex()===t-1)&&p().disableButton(e(o().getDomElementDataIdentifierSelector("buttonPaginationNext")));const n=d().getCurrentlySelectedPageIndex()+1;e(o().getDomElementDataIdentifierSelector("paginationTitle")).text(c(F(),"paginationTitle").replace("{0}",n.toString()).replace("{1}",t))}function Z(){p().enableButton(e(o().getDomElementDataIdentifierSelector("buttonHeaderUndo"))),p().enableButton(e(o().getDomElementDataIdentifierSelector("buttonHeaderRedo"))),d().getCurrentApplicationStatePosition()+1>=d().getCurrentApplicationStates()&&p().disableButton(e(o().getDomElementDataIdentifierSelector("buttonHeaderUndo"))),d().getCurrentApplicationStatePosition()===0&&p().disableButton(e(o().getDomElementDataIdentifierSelector("buttonHeaderRedo")))}function U(){return e(o().getDomElementDataAttribute("elementIdentifier","bracesWithKey"),f)}function B(t){return b(e.type(t)==="number",'Invalid parameter "pageIndex"',1478721208),e("<ol></ol>").addClass("formeditor-list").append(k(F().get("renderables")[t]))}function W(t){return e(t).parent().closest("li").find(o().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).first()}function R(t){return W(t).attr(o().getDomElementDataAttribute("elementIdentifier"))}function L(t){return e(t).find(o().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).first()}function h(t){return L(e(t)).attr(o().getDomElementDataAttribute("elementIdentifier"))}function P(t,n){u().isUndefinedOrNull(n)&&(n="prev");const r=h(t);return t=n==="prev"?e(t).prev("li"):e(t).next("li"),t.find(o().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).not(o().getDomElementDataAttribute("elementIdentifier","bracesWithKeyValue",[r])).first().attr(o().getDomElementDataAttribute("elementIdentifier"))}function ee(t){let n;return typeof t=="string"?n=t:u().isUndefinedOrNull(t)?n=x().get("__identifierPath"):n=t.get("__identifierPath"),e(o().getDomElementDataAttribute("elementIdentifier","bracesWithKeyValue",[n]),f)}function te(){e(o().getDomElementDataIdentifierSelector("abstractViewToolbar"),f).off().empty().remove()}function w(t){let n;b(e.type(t)==="object",'Invalid parameter "formElement"',1479035778);const r=c(t,void 0);return r._isTopLevelFormElement?e():(n=o().getTemplate("FormElement-_ElementToolbar").clone(),n.length?(n=e(e(n.html())),o().getTemplatePropertyDomElement("_type",n).text(c(t,"label")),o().getTemplatePropertyDomElement("_identifier",n).text(t.get("identifier")),r._isCompositeFormElement?(p().hideComponent(e(o().getDomElementDataIdentifierSelector("abstractViewToolbarNewElement"),n)),e(o().getDomElementDataIdentifierSelector("abstractViewToolbarNewElementSplitButtonAfter"),n).on("click",function(){g().publish("view/stage/abstract/elementToolbar/button/newElement/clicked",["view/insertElements/perform/after",{disableElementTypes:[],onlyEnableElementTypes:[]}])}),e(o().getDomElementDataIdentifierSelector("abstractViewToolbarNewElementSplitButtonInside"),n).on("click",function(){g().publish("view/stage/abstract/elementToolbar/button/newElement/clicked",["view/insertElements/perform/inside",{disableElementTypes:[],onlyEnableElementTypes:[]}])})):(p().hideComponent(e(o().getDomElementDataIdentifierSelector("abstractViewToolbarNewElementSplitButton"),n)),e(o().getDomElementDataIdentifierSelector("abstractViewToolbarNewElement"),n).on("click",function(){g().publish("view/stage/abstract/elementToolbar/button/newElement/clicked",["view/insertElements/perform/after",{disableElementTypes:[]}])})),e(o().getDomElementDataIdentifierSelector("abstractViewToolbarRemoveElement"),n).on("click",function(){p().showRemoveFormElementModal()}),n):e())}function ne(t,n,r){u().isUndefinedOrNull(n)&&(n=x()),r?w(n).fadeOut(0,function(){t.prepend(e(this)),e(o().getDomElementDataIdentifierSelector("abstractViewToolbar"),t).fadeIn("fast")}):t.prepend(w(n))}function oe(t,n){u().isUndefinedOrNull(t)&&(t=d().getCurrentlySelectedPageIndex()),f.off().empty().append(B(t)),f.on("click",function(r){const a=e(r.target).closest(o().getDomElementDataAttribute("elementIdentifier","bracesWithKey")).attr(o().getDomElementDataAttribute("elementIdentifier"));u().isUndefinedOrNull(a)||!u().isNonEmptyString(a)||g().publish("view/stage/element/clicked",[a])}),C.isSortable&&$(),e.type(n)==="function"&&n()}function ie(t){b(u().isNonEmptyString(t),'Invalid parameter "html"',1475424409),f.off().empty().html(t),e(":input",f).prop("disabled","disabled").on("click dblclick select focus keydown keypress keyup mousedown mouseup",function(n){return n.preventDefault()}),e("form",f).submit(function(n){return n.preventDefault()}),U().each(function(){const n=d().getFormElementByIdentifierPath(e(this).data("elementIdentifierPath"));c(n,"_isTopLevelFormElement")||e(this).attr("title","identifier: "+n.get("identifier")+" (type: "+n.get("type")+")"),c(n,"_isTopLevelFormElement")&&e(this).addClass(o().getDomElementClassName("formElementIsTopLevel")),c(n,"_isCompositeFormElement")&&e(this).addClass(o().getDomElementClassName("formElementIsComposit"))})}function v(t,n,r){e(o().getDomElementDataAttribute("templateProperty","bracesWithKey"),n).each(function(a,i){const m=e(i).attr(o().getDomElementDataAttribute("templateProperty")),l=t.get(m);e.type(r)==="function"&&r(m,l,i)})}function O(t,n){y(t,n),v(t,n,function(r,a,i){(e.type(a)==="boolean"&&a||a==="true"||a===1||a==="1")&&e(i).addClass(o().getDomElementClassName("noNesting"))})}function N(t,n){b(e.type(t)==="object",'Invalid parameter "formElement"',1479035696),v(t,n,(r,a,i)=>{q(i,a)}),E.getIcon(c(t,"iconIdentifier"),E.sizes.small,null,E.states.default,E.markupIdentifiers.inline).then(function(r){e(o().getDomElementDataIdentifierSelector("formElementIcon"),n).append(e(r).addClass(o().getDomElementClassName("icon")))}),o().getTemplatePropertyDomElement("_type",n).append(document.createTextNode(c(t,"label"))),o().getTemplatePropertyDomElement("_identifier",n).append(document.createTextNode(t.get("identifier")))}function y(t,n){b(e.type(t)==="object",'Invalid parameter "formElement"',1479035674),N(t,n);const r=e(o().getDomElementDataIdentifierSelector("validatorsContainer"),e(n)).clone();e(o().getDomElementDataIdentifierSelector("validatorsContainer"),e(n)).empty();const a=t.get("validators");if(e.type(a)==="array"){let i=0;if(a.length>0){for(let m=0,l=a.length;m<l;++m){if(a[m].identifier==="NotEmpty"){o().getTemplatePropertyDomElement("_required",n).text("*");continue}i++;const s=d().getFormEditorDefinition("validators",a[m].identifier),D=e(e(r).clone());o().getTemplatePropertyDomElement("_label",D).append(document.createTextNode(s.label)),e(o().getDomElementDataIdentifierSelector("validatorsContainer"),e(n)).append(D.html())}i>0&&E.getIcon(o().getDomElementDataAttributeValue("iconValidator"),E.sizes.small,null,E.states.default,E.markupIdentifiers.inline).then(function(m){e(o().getDomElementDataIdentifierSelector("validatorIcon"),e(n)).append(e(m).addClass(o().getDomElementClassName("icon")))})}}}function H(t,n){const r=e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).clone();e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).empty(),y(t,n);const a=e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).attr(o().getDomElementDataAttribute("templateProperty")),i=t.get(a),m=(s,D,A)=>{let V=!1;const T=e(e(r).clone());for(const j of Object.keys(A))if(A[j]===D){V=!0;break}o().getTemplatePropertyDomElement("_label",T).append(document.createTextNode(s)),V&&o().getTemplatePropertyDomElement("_label",T).addClass(o().getDomElementClassName("selected")),e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).append(T.html())};let l=t.get("defaultValue");if(d().getUtility().isUndefinedOrNull(l)?l={}:e.type(l)==="string"&&(l={0:l}),e.type(i)==="object")for(const s of Object.keys(i))m(i[s],s,l);else if(e.type(i)==="array")for(const s of Object.keys(i))u().isUndefinedOrNull(i[s]._label)?m(i[s],s,l):m(i[s]._label,i[s]._value,l)}function K(t,n){const r=e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).clone();e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).empty(),y(t,n);const a=e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).attr(o().getDomElementDataAttribute("templateProperty")),i=t.get(a),m=function(l){const s=e(e(r).clone());o().getTemplatePropertyDomElement("_value",s).append(l),e(o().getDomElementDataIdentifierSelector("multiValueContainer"),e(n)).append(s.html())};if(e.type(i)==="object")for(const l of Object.keys(i))m(i[l]);else if(e.type(i)==="array")for(let l=0,s=i.length;l<s;++l)m(i[l])}function re(t,n,r){return I=t,b(e.type(n)==="object",'Invalid parameter "appendToDomElement"',1478992119),f=e(n),C=e.extend(!0,G,r||{}),S.bootstrap(I),this}export{re as bootstrap,_ as buildTitleByFormElement,w as createAbstractViewFormElementToolbar,ne as createAndAddAbstractViewFormElementToolbar,v as eachTemplateProperty,ee as getAbstractViewFormElementDomElement,h as getAbstractViewFormElementIdentifierPathWithinDomElement,L as getAbstractViewFormElementWithinDomElement,R as getAbstractViewParentFormElementIdentifierPathWithinDomElement,W as getAbstractViewParentFormElementWithinDomElement,P as getAbstractViewSiblingFormElementIdentifierPathWithinDomElement,U as getAllFormElementDomElements,J as getStageDomElement,X as getStagePanelDomElement,te as removeAllStageToolbars,oe as renderAbstractStageArea,O as renderCheckboxTemplate,K as renderFileUploadTemplates,B as renderFormDefinitionPageAsSortableList,Y as renderPagination,ie as renderPreviewStageArea,H as renderSelectTemplates,N as renderSimpleTemplate,y as renderSimpleTemplateWithValidators,Z as renderUndoRedo,Q as setStageHeadline};
