/*
 * 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/Enum/Severity","jquery","TYPO3/CMS/Backend/Modal","TYPO3/CMS/Backend/Utility","./Workspaces","TYPO3/CMS/Core/Event/ThrottleEvent"],(function(e,t,i,s,a,n,o,r){"use strict";var d;s=__importDefault(s),o=__importDefault(o),function(e){e.topbar="#typo3-topbar",e.workspacePanel=".workspace-panel",e.liveView="#live-view",e.stageSlider="#workspace-stage-slider",e.workspaceView="#workspace-view",e.sendToStageAction='[data-action="send-to-stage"]',e.discardAction='[data-action="discard"]',e.stageButtonsContainer=".t3js-stage-buttons",e.previewModeContainer=".t3js-preview-mode",e.activePreviewMode=".t3js-active-preview-mode",e.workspacePreview=".t3js-workspace-preview"}(d||(d={}));class l extends o.default{static getAvailableSpace(){return(0,s.default)(window).height()-(0,s.default)(d.topbar).outerHeight()}constructor(){super(),this.currentSlidePosition=100,this.elements={},this.updateSlidePosition=e=>{this.currentSlidePosition=parseInt(e.target.value,10),this.resizeViews()},this.renderDiscardWindow=()=>{const e=a.confirm(TYPO3.lang["window.discardAll.title"],TYPO3.lang["window.discardAll.message"],i.SeverityEnum.warning,[{text:TYPO3.lang.cancel,active:!0,btnClass:"btn-default",name:"cancel",trigger:()=>{e.modal("hide")}},{text:TYPO3.lang.ok,btnClass:"btn-warning",name:"ok"}]);e.on("button.clicked",t=>{"ok"===t.target.name&&this.sendRemoteRequest([this.generateRemoteActionsPayload("discardStagesFromPage",[TYPO3.settings.Workspaces.id]),this.generateRemoteActionsPayload("updateStageChangeButtons",[TYPO3.settings.Workspaces.id])],"#typo3-topbar").then(async t=>{e.modal("hide"),this.renderStageButtons((await t.resolve())[1].result),this.elements.$workspaceView.attr("src",this.elements.$workspaceView.attr("src"))})})},this.renderSendPageToStageWindow=e=>{const t=e.currentTarget,i=t.dataset.direction;let s;if("prev"===i)s="sendPageToPreviousStage";else{if("next"!==i)throw"Invalid direction "+i+" requested.";s="sendPageToNextStage"}this.sendRemoteRequest(this.generateRemoteActionsPayload(s,[TYPO3.settings.Workspaces.id]),"#typo3-topbar").then(async e=>{const i=await e.resolve(),s=this.renderSendToStageWindow(i);s.on("button.clicked",e=>{if("ok"===e.target.name){const a=n.convertFormToObject(e.currentTarget.querySelector("form"));a.affects=i[0].result.affects,a.stageId=t.dataset.stageId,this.sendRemoteRequest([this.generateRemoteActionsPayload("sentCollectionToStage",[a]),this.generateRemoteActionsPayload("updateStageChangeButtons",[TYPO3.settings.Workspaces.id])],"#typo3-topbar").then(async e=>{s.modal("hide"),this.renderStageButtons((await e.resolve())[1].result)})}})})},this.changePreviewMode=e=>{e.preventDefault();const t=(0,s.default)(e.currentTarget),i=this.elements.$activePreviewMode.data("activePreviewMode"),a=t.data("previewMode");this.elements.$activePreviewMode.text(t.text()).data("activePreviewMode",a),this.elements.$workspacePreview.parent().removeClass("preview-mode-"+i).addClass("preview-mode-"+a),"slider"===a?(this.elements.$stageSlider.parent().toggle(!0),this.resizeViews()):(this.elements.$stageSlider.parent().toggle(!1),"vbox"===a?this.elements.$liveView.height("100%"):this.elements.$liveView.height("50%"))},(0,s.default)(()=>{this.getElements(),this.resizeViews(),this.adjustPreviewModeSelectorWidth(),this.registerEvents()})}getElements(){this.elements.$liveView=(0,s.default)(d.liveView),this.elements.$workspacePanel=(0,s.default)(d.workspacePanel),this.elements.$stageSlider=(0,s.default)(d.stageSlider),this.elements.$workspaceView=(0,s.default)(d.workspaceView),this.elements.$stageButtonsContainer=(0,s.default)(d.stageButtonsContainer),this.elements.$previewModeContainer=(0,s.default)(d.previewModeContainer),this.elements.$activePreviewMode=(0,s.default)(d.activePreviewMode),this.elements.$workspacePreview=(0,s.default)(d.workspacePreview)}registerEvents(){new r("resize",()=>{this.resizeViews()},50).bindTo(window),(0,s.default)(document).on("click",d.discardAction,this.renderDiscardWindow).on("click",d.sendToStageAction,this.renderSendPageToStageWindow).on("click",".t3js-workspace-recipients-selectall",()=>{(0,s.default)(".t3js-workspace-recipient",window.top.document).not(":disabled").prop("checked",!0)}).on("click",".t3js-workspace-recipients-deselectall",()=>{(0,s.default)(".t3js-workspace-recipient",window.top.document).not(":disabled").prop("checked",!1)}),new r("input",this.updateSlidePosition,25).bindTo(document.querySelector(d.stageSlider)),this.elements.$previewModeContainer.find("[data-preview-mode]").on("click",this.changePreviewMode)}renderStageButtons(e){this.elements.$stageButtonsContainer.html(e)}resizeViews(){const e=l.getAvailableSpace(),t=-1*(this.currentSlidePosition-100),i=Math.round(Math.abs(e*t/100)),s=this.elements.$liveView.outerHeight()-this.elements.$liveView.height();this.elements.$workspacePreview.height(e),"slider"===this.elements.$activePreviewMode.data("activePreviewMode")&&this.elements.$liveView.height(i-s)}adjustPreviewModeSelectorWidth(){const e=this.elements.$previewModeContainer.find(".dropdown-menu");let t=0;e.addClass("show"),this.elements.$previewModeContainer.find("li > a > span").each((e,i)=>{const a=(0,s.default)(i).width();t<a&&(t=a)}),e.removeClass("show"),this.elements.$activePreviewMode.width(t)}}return new l}));