/*
 * 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{html as u,LitElement as y}from"lit";import{state as M,customElement as F}from"lit/decorators.js";import m,{Sizes as f,Styles as p}from"@typo3/backend/modal.js";import{SeverityEnum as v}from"@typo3/backend/enum/severity.js";import c from"@typo3/core/ajax/ajax-request.js";import b from"@typo3/backend/notification.js";import"@typo3/backend/element/progress-bar-element.js";import o from"~labels/core.core";var L=function(r,e,t,n){var i=arguments.length,s=i<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")s=Reflect.decorate(r,e,t,n);else for(var l=r.length-1;l>=0;l--)(a=r[l])&&(s=(i<3?a(s):i>3?a(e,t,s):a(e,t))||s);return i>3&&s&&Object.defineProperty(e,t,s),s},h;(function(r){r.loginrefresh="t3js-modal-loginrefresh",r.lockedModal="t3js-modal-backendlocked",r.loginFormModal="t3js-modal-backendloginform"})(h||(h={}));class P{constructor(){this.intervalTime=60,this.intervalId=null,this.backendIsLocked=!1,this.timeoutModal=null,this.backendLockedModal=null,this.loginForm=null,this.requestTokenUrl="",this.loginFramesetUrl="",this.logoutUrl="",this.submitForm=async(e,t)=>{e.preventDefault();const i=await(await new c(this.requestTokenUrl).post({})).resolve("application/json");if(!i.headerName||!i.requestToken)return;const s=t.querySelector("input[name=p_field]"),a=t.querySelector("input[name=userident]"),l=s.value;if(l===""&&a.value===""){b.error(o.get("mess.refresh_login_failed"),o.get("mess.refresh_login_emptyPassword")),s.focus();return}l&&(a.value=l,s.value="");const k={login_status:"login"};for(const[_,T]of new FormData(t))k[_]=T.toString();const w=new Headers;w.set(i.headerName,i.requestToken),(await(await new c(t.getAttribute("action")).post(k,{headers:w})).resolve()).login.success?this.hideLoginForm():(b.error(o.get("mess.refresh_login_failed"),o.get("mess.refresh_login_failed_message")),s.focus())},this.checkActiveSession=async()=>{try{const t=await(await new c(TYPO3.settings.ajaxUrls.login_timedout).get()).resolve();t.login.locked?this.backendIsLocked||(this.backendIsLocked=!0,this.showBackendLockedModal()):this.backendIsLocked&&(this.backendIsLocked=!1,this.hideBackendLockedModal()),this.backendIsLocked||(t.login.timed_out||t.login.will_time_out)&&(t.login.timed_out?this.showLoginForm():this.showTimeoutModal())}catch{this.backendIsLocked=!0,this.showBackendLockedModal()}}}initialize(e){typeof e=="object"&&this.applyOptions(e),this.startTask()}startTask(){if(this.intervalId!==null)return;const e=this.intervalTime*1e3;this.intervalId=setInterval(this.checkActiveSession,e)}stopTask(){clearInterval(this.intervalId),this.intervalId=null}setIntervalTime(e){this.intervalTime=Math.min(e,86400)}setLogoutUrl(e){this.logoutUrl=e}setLoginFramesetUrl(e){this.loginFramesetUrl=e}showTimeoutModal(){this.timeoutModal=this.createTimeoutModal(),this.timeoutModal.addEventListener("typo3-modal-hidden",()=>this.timeoutModal=null),this.timeoutModal.addEventListener("show-login-form",()=>{this.timeoutModal.hideModal(),this.showLoginForm()})}hideTimeoutModal(){this.timeoutModal?.hideModal()}showBackendLockedModal(){this.backendLockedModal||(this.backendLockedModal=this.createBackendLockedModal(),this.backendLockedModal.addEventListener("typo3-modal-hidden",()=>this.backendLockedModal=null))}hideBackendLockedModal(){this.backendLockedModal?.hideModal()}showLoginForm(){this.loginForm||new c(TYPO3.settings.ajaxUrls.logout).get().then(()=>{TYPO3.configuration.showRefreshLoginPopup?this.showLoginPopup():(this.loginForm=this.createLoginFormModal(),this.loginForm.addEventListener("typo3-modal-hidden",()=>this.loginForm=null))})}showLoginPopup(){const e=window.open(this.loginFramesetUrl,"relogin_"+Math.random().toString(16).slice(2),"height=450,width=700,status=0,menubar=0,location=1");e&&e.focus()}hideLoginForm(){this.loginForm?.hideModal()}createBackendLockedModal(){return m.advanced({additionalCssClasses:[h.lockedModal],title:o.get("mess.please_wait"),severity:v.notice,style:p.light,size:f.small,staticBackdrop:!0,hideCloseButton:!0,content:u`<p>${o.get("mess.be_locked")}</p>`})}createTimeoutModal(){const e=m.advanced({additionalCssClasses:[h.loginrefresh],title:o.get("mess.login_about_to_expire_title"),severity:v.notice,style:p.light,size:f.small,staticBackdrop:!0,hideCloseButton:!0,buttons:[{text:o.get("mess.refresh_login_logout_button"),active:!1,btnClass:"btn-default",name:"logout",trigger:()=>top.location.href=this.logoutUrl},{text:o.get("mess.refresh_login_refresh_button"),active:!0,btnClass:"btn-primary",name:"refreshSession",trigger:async(t,n)=>{const s=await(await new c(TYPO3.settings.ajaxUrls.login_refresh).get()).resolve();n.hideModal(),s.refresh.success||n.dispatchEvent(new Event("show-login-form"))}}],content:u`<p>${o.get("mess.login_about_to_expire")}</p><typo3-login-refresh-progress-bar @progress-bar-overdue=${()=>e.dispatchEvent(new Event("show-login-form"))}></typo3-login-refresh-progress-bar>`});return e.addEventListener("typo3-modal-hidden",()=>{this.startTask()}),e.addEventListener("typo3-modal-shown",()=>{this.stopTask()}),e}createLoginFormModal(){const e=o.get("mess.refresh_login_title",[TYPO3.configuration.username]),t=m.advanced({additionalCssClasses:[h.loginFormModal],title:e,severity:v.notice,style:p.light,size:f.small,staticBackdrop:!0,hideCloseButton:!0,buttons:[{text:o.get("mess.refresh_exit_button"),active:!1,btnClass:"btn-default",name:"logout",trigger:()=>top.location.href=this.logoutUrl},{text:o.get("mess.refresh_login_button"),active:!1,btnClass:"btn-primary",name:"refreshSession",trigger:async(n,i)=>{i.querySelector("form").requestSubmit();const a=await(await new c(TYPO3.settings.ajaxUrls.login_refresh).get()).resolve();i.hideModal(),a.refresh.success||i.dispatchEvent(new Event("show-login-form"))}}],content:u`<p>${o.get("mess.login_expired")}</p><form id=beLoginRefresh method=POST action=${TYPO3.settings.ajaxUrls.login} @submit=${n=>this.submitForm(n,n.currentTarget)}><div><input type=text name=username class=d-none autocomplete=username .value=${TYPO3.configuration.username}> <input type=hidden name=userident id=t3-loginrefresh-userident></div><div class=form-group><input type=password name=p_field autofocus class=form-control autocomplete=current-password placeholder=${o.get("mess.refresh_login_password")}></div></form>`});return t.addEventListener("typo3-modal-hidden",()=>{this.startTask()}),t.addEventListener("typo3-modal-shown",()=>{this.stopTask()}),t}applyOptions(e){e.intervalTime!==void 0&&this.setIntervalTime(e.intervalTime),e.loginFramesetUrl!==void 0&&this.setLoginFramesetUrl(e.loginFramesetUrl),e.logoutUrl!==void 0&&this.setLogoutUrl(e.logoutUrl),e.requestTokenUrl!==void 0&&(this.requestTokenUrl=e.requestTokenUrl)}}let g=class extends y{constructor(){super(...arguments),this.current=0,this.max=100,this.advanceProgressBar=()=>{this.current++,this.current>=this.max&&this.dispatchEvent(new Event("progress-bar-overdue"))}}connectedCallback(){super.connectedCallback(),this.intervalId&&clearInterval(this.intervalId),this.intervalId=setInterval(this.advanceProgressBar,300)}disconnectedCallback(){super.disconnectedCallback(),this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null)}createRenderRoot(){return this}render(){return u`<typo3-backend-progress-bar value=${100-this.current} max=100></typo3-backend-progress-bar>`}};L([M()],g.prototype,"current",void 0),g=L([F("typo3-login-refresh-progress-bar")],g);let d;try{window.opener&&window.opener.TYPO3&&window.opener.TYPO3.LoginRefresh&&(d=window.opener.TYPO3.LoginRefresh),parent&&parent.window.TYPO3&&parent.window.TYPO3.LoginRefresh&&(d=parent.window.TYPO3.LoginRefresh),top&&top.TYPO3&&top.TYPO3.LoginRefresh&&(d=top.TYPO3.LoginRefresh)}catch{}d||(d=new P,typeof TYPO3<"u"&&(TYPO3.LoginRefresh=d));var O=d;export{g as SelfFillingProgressBarElement,O as default};
