/*
 * 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","TYPO3/CMS/Backend/BroadcastMessage","TYPO3/CMS/Core/Ajax/AjaxRequest","./Enum/Severity","bootstrap","jquery","TYPO3/CMS/Backend/BroadcastService","./Icons","./Modal","./Notification"],(function(e,t,a,s,n,o,i,r,d,l,c){"use strict";var h;i=__importDefault(i),function(e){e.hide=".t3js-record-hide",e.delete=".t3js-record-delete",e.icon=".t3js-icon"}(h||(h={}));class u{static refreshPageTree(){top.document.dispatchEvent(new CustomEvent("typo3:pagetree:refresh"))}static call(e){return new s(TYPO3.settings.ajaxUrls.record_process).withQueryArguments(e).get().then(async e=>await e.resolve())}constructor(){(0,i.default)(()=>{this.initialize()})}process(e,t){return u.call(e).then(e=>{if(e.hasErrors&&this.handleErrors(e),t){const s=Object.assign(Object.assign({},t),{hasErrors:e.hasErrors}),n=new a.BroadcastMessage("datahandler","process",s);r.post(n);const o=new CustomEvent("typo3:datahandler:process",{detail:{payload:s}});document.dispatchEvent(o)}return e})}initialize(){(0,i.default)(document).on("click",h.hide,e=>{e.preventDefault();const t=(0,i.default)(e.currentTarget),a=t.find(h.icon),s=t.closest("tr[data-uid]"),n=t.data("params");this._showSpinnerIcon(a),this.process(n).then(e=>{e.hasErrors||this.toggleRow(s)})}),(0,i.default)(document).on("click",h.delete,e=>{e.preventDefault();const t=(0,i.default)(e.currentTarget);t.tooltip("hide");l.confirm(t.data("title"),t.data("message"),n.SeverityEnum.warning,[{text:t.data("button-close-text")||TYPO3.lang["button.cancel"]||"Cancel",active:!0,btnClass:"btn-default",name:"cancel"},{text:t.data("button-ok-text")||TYPO3.lang["button.delete"]||"Delete",btnClass:"btn-warning",name:"delete"}]).on("button.clicked",e=>{"cancel"===e.target.getAttribute("name")?l.dismiss():"delete"===e.target.getAttribute("name")&&(l.dismiss(),this.deleteRecord(t))})})}toggleRow(e){const t=e.find(h.hide),a=t.closest("table[data-table]").data("table"),s=t.data("params");let n,r,l;"hidden"===t.data("state")?(r="visible",n=s.replace("=0","=1"),l="actions-edit-hide"):(r="hidden",n=s.replace("=1","=0"),l="actions-edit-unhide"),t.data("state",r).data("params",n),t.one("hidden.bs.tooltip",()=>{const e=t.data("toggleTitle");o.Tooltip.getInstance(t.get(0)).setContent(e)}),t.tooltip("hide");const c=t.find(h.icon);d.getIcon(l,d.sizes.small).then(e=>{c.replaceWith(e)});const p=e.find(".col-icon "+h.icon);"hidden"===r?d.getIcon("miscellaneous-placeholder",d.sizes.small,"overlay-hidden").then(e=>{p.append((0,i.default)(e).find(".icon-overlay"))}):p.find(".icon-overlay").remove(),e.fadeTo("fast",.4,()=>{e.fadeTo("fast",1)}),"pages"===a&&u.refreshPageTree()}deleteRecord(e){const t=e.data("params");let a=e.find(h.icon);this._showSpinnerIcon(a);const s=e.closest("table[data-table]"),n=s.data("table");let o=e.closest("tr[data-uid]");const i=o.data("uid"),r={component:"datahandler",action:"delete",table:n,uid:i};this.process(t,r).then(t=>{if(d.getIcon("actions-edit-delete",d.sizes.small).then(t=>{a=e.find(h.icon),a.replaceWith(t)}),!t.hasErrors){const t=e.closest(".panel"),a=t.find(".panel-heading"),r=s.find("[data-l10nparent="+i+"]").closest("tr[data-uid]");if(o=o.add(r),o.fadeTo("slow",.4,()=>{o.slideUp("slow",()=>{o.remove(),0===s.find("tbody tr").length&&t.slideUp("slow")})}),"0"===e.data("l10parent")||""===e.data("l10parent")){const e=Number(a.find(".t3js-table-total-items").html());a.find(".t3js-table-total-items").text(e-1)}"pages"===n&&u.refreshPageTree()}})}handleErrors(e){i.default.each(e.messages,(e,t)=>{c.error(t.title,t.message)})}_showSpinnerIcon(e){d.getIcon("spinner-circle-dark",d.sizes.small).then(t=>{e.replaceWith(t)})}}return new u}));