import{t as e}from"./jquery.module-z0Ig-8vR.js";import{k as t}from"./utils-BakkG8Vx.js";e();var n=class{step;setupType=``;syncPollIntervalId=null;rootNode;setupTypeForm;syncFromServerForm;setupTypeNextButton;setupTypeInputs;syncServerHostInput;syncProxyInput;passwordInput;sections;constructor(e,t){this.rootNode=e,this.step=t?`sync-in-progress`:`setup-type`,this.setupTypeForm=s(`setup-type-form`,HTMLFormElement),this.syncFromServerForm=s(`sync-from-server-form`,HTMLFormElement),this.setupTypeNextButton=s(`setup-type-next`,HTMLButtonElement),this.setupTypeInputs=Array.from(document.querySelectorAll(`input[name='setup-type']`)),this.syncServerHostInput=s(`sync-server-host`,HTMLInputElement),this.syncProxyInput=s(`sync-proxy`,HTMLInputElement),this.passwordInput=s(`password`,HTMLInputElement),this.sections={"setup-type":s(`setup-type-section`,HTMLElement),"new-document-in-progress":s(`new-document-in-progress-section`,HTMLElement),"sync-from-desktop":s(`sync-from-desktop-section`,HTMLElement),"sync-from-server":s(`sync-from-server-section`,HTMLElement),"sync-in-progress":s(`sync-in-progress-section`,HTMLElement)}}init(){this.setupTypeForm.addEventListener(`submit`,e=>{e.preventDefault(),this.selectSetupType()}),this.syncFromServerForm.addEventListener(`submit`,e=>{e.preventDefault(),this.finish()});for(let e of this.setupTypeInputs)e.addEventListener(`change`,()=>{this.setupType=e.value,this.render()});for(let e of document.querySelectorAll(`[data-action='back']`))e.addEventListener(`click`,()=>{this.back()});let e=`${location.protocol}//${location.host}`;$(`#current-host`).html(e),this.step===`sync-in-progress`&&this.startSyncPolling(),this.render(),this.rootNode.style.display=``}async selectSetupType(){if(this.setupType===`new-document`){this.setStep(`new-document-in-progress`),await $.post(`api/setup/new-document`),window.location.replace(`./setup`);return}this.setupType&&this.setStep(this.setupType)}back(){this.setStep(`setup-type`),this.setupType=``;for(let e of this.setupTypeInputs)e.checked=!1;this.render()}async finish(){let e=this.syncServerHostInput.value.trim().replace(/\/+$/,``),t=this.syncProxyInput.value.trim(),n=this.passwordInput.value;if(!e){i(`Trilium server address can't be empty`);return}if(!n){i(`Password can't be empty`);return}let r=await $.post(`api/setup/sync-from-server`,{syncServerHost:e,syncProxy:t,password:n});r.result===`success`?(a(),this.setStep(`sync-in-progress`),this.startSyncPolling()):i(`Sync setup failed: ${r.error}`)}setStep(e){this.step=e,this.render()}render(){for(let[e,t]of Object.entries(this.sections))t.style.display=e===this.step?``:`none`;this.setupTypeNextButton.disabled=!this.setupType}getSelectedSetupType(){return this.setupTypeInputs.find(e=>e.checked)?.value??``}startSyncPolling(){this.syncPollIntervalId===null&&(this.syncPollIntervalId=window.setInterval(r,1e3))}};async function r(){let{outstandingPullCount:e,initialized:n}=await $.get(`api/sync/stats`);if(n)if(t.isElectron()){let e=t.dynamicRequire(`@electron/remote`);e.app.relaunch(),e.app.exit(0)}else t.reloadFrontendApp();else $(`#outstanding-syncs`).html(e)}function i(e){$(`#alert`).text(e),$(`#alert`).show()}function a(){$(`#alert`).hide()}function o(){let e=document.getElementById(`syncInProgress`);return!e||!(e instanceof HTMLMetaElement)?!1:!!parseInt(e.content)}function s(e,t){let n=document.getElementById(e);if(!n||!(n instanceof t))throw Error(`Expected element #${e}`);return n}addEventListener(`DOMContentLoaded`,e=>{let t=document.getElementById(`setup-dialog`);!t||!(t instanceof HTMLElement)||new n(t,o()).init()});