/*
 * 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{BroadcastMessage as y}from"@typo3/backend/broadcast-message.js";import I from"@typo3/core/ajax/ajax-request.js";import E from"@typo3/core/document-service.js";import w from"@typo3/backend/broadcast-service.js";import s from"@typo3/backend/icons.js";import S from"@typo3/backend/notification.js";import m from"@typo3/core/event/regular-event.js";import{sudoModeInterceptor as V}from"@typo3/backend/security/sudo-mode-interceptor.js";var o;(function(l){l.hide='button[data-datahandler-action="visibility"]',l.icon=".t3js-icon"})(o||(o={}));class i{constructor(){E.ready().then(()=>{this.initialize()})}static refreshPageTree(){top.document.dispatchEvent(new CustomEvent("typo3:pagetree:refresh"))}static call(e){return new I(TYPO3.settings.ajaxUrls.record_process).addMiddleware(V).withQueryArguments(e).get().then(async a=>await a.resolve())}process(e,a){return i.call(e).then(r=>{if(r.hasErrors&&this.handleErrors(r),a){const t={...a,hasErrors:r.hasErrors},n=new y("datahandler","process",t);w.post(n);const d=new CustomEvent("typo3:datahandler:process",{detail:{payload:t}});document.dispatchEvent(d)}return r})}initialize(){new m("click",(e,a)=>{e.preventDefault(),this.handleVisibilityToggle(a)}).delegateTo(document,o.hide)}handleVisibilityToggle(e){const a=e.closest("tr[data-uid]"),h=e.querySelector(o.icon);this._showSpinnerIcon(h);const r=e.dataset.datahandlerStatus==="visible",t={table:e.dataset.datahandlerTable,uid:e.dataset.datahandlerUid,field:e.dataset.datahandlerField,visible:r,overlayIcon:r?e.dataset.datahandlerRecordHiddenOverlayIcon??"overlay-hidden":e.dataset.datahandlerRecordVisibleOverlayIcon??null},n={data:{[t.table]:{[t.uid]:{[t.field]:t.visible?e.dataset.datahandlerHiddenValue:e.dataset.datahandlerVisibleValue}}}};this.process(n).then(d=>{if(!d.hasErrors){t.visible=!t.visible,e.setAttribute("data-datahandler-status",t.visible?"visible":"hidden");const v=t.visible?e.dataset.datahandlerVisibleLabel:e.dataset.datahandlerHiddenLabel;e.setAttribute("title",v);const g=t.visible?e.dataset.datahandlerVisibleIcon:e.dataset.datahandlerHiddenIcon,b=e.querySelector(o.icon);s.getIcon(g,s.sizes.small).then(c=>{b.replaceWith(document.createRange().createContextualFragment(c))});const u=a.querySelector(".col-icon "+o.icon);u.querySelector(".icon-overlay")?.remove(),s.getIcon("miscellaneous-placeholder",s.sizes.small,t.overlayIcon).then(c=>{const f=document.createRange().createContextualFragment(c);u.append(f.querySelector(".icon-overlay"))});const p=new m("animationend",()=>{a.classList.remove("record-pulse"),p.release()});p.bindTo(a),a.classList.add("record-pulse"),t.table==="pages"&&i.refreshPageTree()}})}handleErrors(e){for(const a of e.messages)S.error(a.title,a.message)}_showSpinnerIcon(e){s.getIcon("spinner-circle",s.sizes.small).then(a=>{e.replaceWith(document.createRange().createContextualFragment(a))})}}var T=new i;export{T as default};
