/*
 * 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!
 */
var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};define(["require","exports","./Enum/KeyTypes","jquery","./Storage/Persistent","TYPO3/CMS/Core/lit-helper","lit/directives/unsafe-html","lit","TYPO3/CMS/Backend/Element/IconElement","TYPO3/CMS/Backend/NewContentElementWizardButton"],(function(e,t,i,n,a,l,s,o){"use strict";var c;n=__importDefault(n),function(e){e.pageTitle=".t3js-title-inlineedit",e.hiddenElements=".t3js-hidden-record",e.newButton=".t3js-toggle-new-content-element-wizard"}(c||(c={}));return new class{constructor(){this.pageId=0,this.pageOverlayId=0,this.$pageTitle=null,this.$showHiddenElementsCheckbox=null,n.default(()=>{this.initializeElements(),this.initializeEvents(),this.initializeNewContentElementWizard(),this.initializePageTitleRenaming()})}setPageId(e){this.pageId=e}setLanguageOverlayId(e){this.pageOverlayId=e}initializePageTitleRenaming(){if(!n.default.isReady)return void n.default(()=>{this.initializePageTitleRenaming()});if(this.pageId<=0)return;const e=n.default('<button type="button" class="btn btn-link" aria-label="'+TYPO3.lang.editPageTitle+'" data-action="edit"><typo3-backend-icon identifier="actions-open" size="small"></typo3-backend-icon></button>');e.on("click",()=>{this.editPageTitle()}),this.$pageTitle.on("dblclick",()=>{this.editPageTitle()}).append(e)}initializeElements(){this.$pageTitle=n.default(c.pageTitle+":first"),this.$showHiddenElementsCheckbox=n.default("#checkTt_content_showHidden")}initializeEvents(){this.$showHiddenElementsCheckbox.on("change",this.toggleContentElementVisibility)}toggleContentElementVisibility(e){const t=n.default(e.currentTarget),i=n.default(c.hiddenElements),l=n.default('<span class="form-check-spinner"><typo3-backend-icon identifier="spinner-circle" size="small"></typo3-backend-icon></span>');t.hide().after(l),t.prop("checked")?i.slideDown():i.slideUp(),a.set("moduleData.web_layout.tt_content_showHidden",t.prop("checked")?"1":"0").done(()=>{l.remove(),t.show()})}editPageTitle(){const e=n.default('<form class="t3js-title-edit-form"><div class="form-group"><div class="input-group input-group-lg"><input class="form-control t3js-title-edit-input"><button class="btn btn-default" type="button" data-action="submit"><typo3-backend-icon identifier="actions-save" size="small"></typo3-backend-icon></button> <button class="btn btn-default" type="button" data-action="cancel"><typo3-backend-icon identifier="actions-close" size="small"></typo3-backend-icon></button> </div></div></form>'),t=e.find("input");e.find('[data-action="cancel"]').on("click",()=>{e.replaceWith(this.$pageTitle),this.initializePageTitleRenaming()}),e.find('[data-action="submit"]').on("click",()=>{const i=t.val().trim();""!==i&&this.$pageTitle.text()!==i?this.saveChanges(t):e.find('[data-action="cancel"]').trigger("click")}),t.parents("form").on("submit",e=>(e.preventDefault(),!1));const a=this.$pageTitle;a.children().last().remove(),a.replaceWith(e),t.val(a.text()).focus(),t.on("keydown",t=>{switch(t.which){case i.KeyTypesEnum.ENTER:e.find('[data-action="submit"]').trigger("click");break;case i.KeyTypesEnum.ESCAPE:e.find('[data-action="cancel"]').trigger("click")}})}saveChanges(t){const i=t.parents("form.t3js-title-edit-form");i.find("button").addClass("disabled"),t.attr("disabled","disabled");let n,a={};n=this.pageOverlayId>0?this.pageOverlayId:this.pageId,a.data={},a.data.pages={},a.data.pages[n]={title:t.val()},e(["TYPO3/CMS/Backend/AjaxDataHandler"],e=>{e.process(a).then(()=>{i.find("[data-action=cancel]").trigger("click"),this.$pageTitle.text(t.val()),this.initializePageTitleRenaming(),top.document.dispatchEvent(new CustomEvent("typo3:pagetree:refresh"))}).catch(()=>{i.find("[data-action=cancel]").trigger("click")})})}initializeNewContentElementWizard(){document.querySelectorAll(c.newButton).length&&console.warn("Usage of the .t3js-toggle-new-content-element-wizard class is deprecated and will be removed in v12. Use the typo3-backend-new-content-element-wizard-button web component instead."),Array.from(document.querySelectorAll(c.newButton)).forEach(e=>{e.classList.remove(c.newButton.substring(1),"disabled");const t=document.createDocumentFragment();l.renderNodes(o.html`
        <typo3-backend-new-content-element-wizard-button
          title="${e.dataset.title||e.title||""}"
          url="${e instanceof HTMLAnchorElement?e.href:e.dataset.target||""}">
          <button type="button" class="${e.classList.toString()}">${s.unsafeHTML(e.innerHTML)}</button>
        </typo3-backend-new-content-element-wizard-button>
      `).forEach(e=>t.appendChild(e)),e.parentNode.replaceChild(t,e)})}}}));