(function (root) { 'use strict'; function createReinstallController({ document, serverName, hasPerm, fetchWithServerActionToken, showModalElement, animateClose, logger = console }) { const reinstallModal = document.getElementById('reinstallModal'); const reinstallModalClose = document.getElementById('reinstallModalClose'); const reinstallCancelBtn = document.getElementById('reinstallCancelBtn'); const reinstallConfirmBtn = document.getElementById('reinstallConfirmBtn'); const reinstallConfirmNo = document.getElementById('reinstallConfirmNo'); const reinstallConfirmYes = document.getElementById('reinstallConfirmYes'); const reinstallDoneBtn = document.getElementById('reinstallDoneBtn'); const reinstallErrorClose = document.getElementById('reinstallErrorClose'); const reinstallDefaultView = document.getElementById('reinstallDefaultView'); const reinstallConfirmView = document.getElementById('reinstallConfirmView'); const reinstallProgressView = document.getElementById('reinstallProgressView'); const reinstallSuccessView = document.getElementById('reinstallSuccessView'); const reinstallSuccessDesc = document.getElementById('reinstallSuccessDesc'); const reinstallSuccessText = reinstallSuccessDesc?.textContent; const reinstallErrorView = document.getElementById('reinstallErrorView'); const reinstallErrorTitle = document.getElementById('reinstallErrorTitle'); const reinstallErrorDesc = document.getElementById('reinstallErrorDesc'); const reinstallCredentialBlock = document.getElementById('reinstallCredentialBlock'); const reinstallCredentialLabel = document.getElementById('reinstallCredentialLabel'); const reinstallCredentialInput = document.getElementById('reinstallCredentialInput'); const reinstallCredentialError = document.getElementById('reinstallCredentialError'); const REINSTALL_STEPUP_CODES = ['password-required', '2fa-required', 'invalid-password', 'invalid-2fa']; let reinstallCredentialMode = ''; let reinstallInFlight = false; let reinstallResultView = ''; function reinstallShowView(viewId) { [reinstallDefaultView, reinstallConfirmView, reinstallProgressView, reinstallSuccessView, reinstallErrorView].forEach(view => { if (view) view.style.display = 'none'; }); const target = document.getElementById(viewId); if (target) target.style.display = ''; } function reinstallShowCredentialStep(message, revokeSecurity) { if (revokeSecurity && typeof revokeSecurity === 'object') { reinstallCredentialMode = (revokeSecurity.requiresTwoFactor || revokeSecurity.authMethod === '2fa') ? '2fa' : 'password'; } else if (!reinstallCredentialMode) { reinstallCredentialMode = 'password'; } if (reinstallCredentialLabel) { reinstallCredentialLabel.textContent = reinstallCredentialMode === '2fa' ? 'Enter a 6-digit code from your authenticator app to confirm' : 'Enter your current password to confirm'; } if (reinstallCredentialInput) { reinstallCredentialInput.type = reinstallCredentialMode === '2fa' ? 'text' : 'password'; reinstallCredentialInput.autocomplete = reinstallCredentialMode === '2fa' ? 'one-time-code' : 'current-password'; reinstallCredentialInput.placeholder = reinstallCredentialMode === '2fa' ? '123456' : 'Current password'; reinstallCredentialInput.maxLength = reinstallCredentialMode === '2fa' ? 6 : 128; } if (reinstallCredentialError) { reinstallCredentialError.textContent = message || ''; reinstallCredentialError.style.display = message ? 'block' : 'none'; } if (reinstallCredentialBlock) reinstallCredentialBlock.style.display = 'flex'; reinstallShowView('reinstallConfirmView'); if (reinstallCredentialInput) reinstallCredentialInput.focus(); } function reinstallResetCredentialStep() { if (reinstallCredentialBlock) reinstallCredentialBlock.style.display = 'none'; if (reinstallCredentialInput) reinstallCredentialInput.value = ''; if (reinstallCredentialError) { reinstallCredentialError.textContent = ''; reinstallCredentialError.style.display = 'none'; } reinstallCredentialMode = ''; } function showReinstallResult(viewId) { reinstallResetCredentialStep(); reinstallResultView = viewId; reinstallShowView(viewId); } function showReinstallError(message, unconfirmed = false) { if (reinstallErrorTitle) reinstallErrorTitle.textContent = unconfirmed ? 'Reinstall result unconfirmed' : 'Reinstall failed'; if (reinstallErrorDesc) reinstallErrorDesc.textContent = message; showReinstallResult('reinstallErrorView'); } async function attemptReinstall() { if (reinstallInFlight) return; reinstallInFlight = true; if (reinstallConfirmYes) reinstallConfirmYes.disabled = true; reinstallShowView('reinstallProgressView'); try { const body = {}; if (reinstallCredentialMode === '2fa') { body.twoFactorCode = (reinstallCredentialInput?.value || '').replace(/\D/g, ''); } else if (reinstallCredentialMode === 'password') { body.currentPassword = reinstallCredentialInput?.value || ''; } if (reinstallCredentialInput) reinstallCredentialInput.value = ''; const response = await fetchWithServerActionToken(`/api/servers/${encodeURIComponent(serverName)}/reinstall`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }, 'reinstallServer', { refreshBefore: true }); const data = await response.json().catch(() => null); if (response.status === 403 && REINSTALL_STEPUP_CODES.includes(data?.code)) { if (!reinstallModal?.classList.contains('show') || reinstallModal.classList.contains('closing')) return; const failed = data.code === 'invalid-password' || data.code === 'invalid-2fa'; const revokeSecurity = data.revokeSecurity || { authMethod: data.code.endsWith('2fa') || data.code === '2fa-required' ? '2fa' : 'password' }; reinstallShowCredentialStep(failed ? (data.error || 'Verification failed.') : '', revokeSecurity); return; } if (!response.ok || data?.ok !== true) { const unconfirmed = response.ok || data?.code === 'reinstall-result-unconfirmed' || response.status === 504 || (response.status >= 500 && (data?.ok !== false || !data?.error)); showReinstallError(unconfirmed ? 'The reinstall result could not be confirmed. It may still be running or may have completed. Check the server state before retrying.' : (data?.error || 'Reinstall failed. Check the server state before retrying.'), unconfirmed); return; } if (reinstallSuccessDesc) { let message = reinstallSuccessText; if (data.eulaRequired === true) { message = 'Your server files were replaced. Accept the Minecraft EULA in the console before starting the server.'; } else if (data.powerState === 'stopped') { message = 'Your server files were replaced, but the server is stopped.'; } reinstallSuccessDesc.textContent = message; } showReinstallResult('reinstallSuccessView'); } catch (error) { logger.error('[reinstall] Network error:', error); showReinstallError('Connection lost. The reinstall may still be running or may have completed. Check the server state before retrying.', true); } finally { reinstallInFlight = false; if (reinstallConfirmYes) reinstallConfirmYes.disabled = false; } } function openReinstallModal() { if (!reinstallModal || !hasPerm('server_reinstall')) return; // adpanel:reinstall-admission - navigation must not release a pending // destructive request; only its completion can admit another attempt. if (reinstallInFlight) { reinstallShowView('reinstallProgressView'); } else { reinstallResetCredentialStep(); reinstallShowView(reinstallResultView || 'reinstallDefaultView'); } showModalElement(reinstallModal); } function dismissReinstallView() { reinstallResultView = ''; reinstallResetCredentialStep(); } function closeReinstallModal() { if (!reinstallModal) return; dismissReinstallView(); animateClose(reinstallModal); } [reinstallModalClose, reinstallCancelBtn, reinstallErrorClose, reinstallDoneBtn].forEach(button => { if (button) button.addEventListener('click', closeReinstallModal); }); if (reinstallConfirmBtn) { reinstallConfirmBtn.addEventListener('click', () => { reinstallShowView('reinstallConfirmView'); }); } if (reinstallConfirmNo) { reinstallConfirmNo.addEventListener('click', () => { reinstallResetCredentialStep(); reinstallShowView('reinstallDefaultView'); }); } if (reinstallConfirmYes) { reinstallConfirmYes.addEventListener('click', () => { if (!hasPerm('server_reinstall')) { closeReinstallModal(); return; } return attemptReinstall(); }); } if (reinstallCredentialInput) { reinstallCredentialInput.addEventListener('keydown', (event) => { if (event.key === 'Enter') { event.preventDefault(); if (hasPerm('server_reinstall')) return attemptReinstall(); } }); } if (reinstallModal) { reinstallModal.addEventListener('adpanel:reinstall-dismiss', dismissReinstallView); reinstallModal.addEventListener('click', (event) => { if (event.target === reinstallModal) closeReinstallModal(); }); if (root.MutationObserver) { const observer = new root.MutationObserver(() => { if (!reinstallModal.classList.contains('show')) reinstallResetCredentialStep(); }); observer.observe(reinstallModal, { attributes: true, attributeFilter: ['class'] }); } } document.addEventListener('keydown', (event) => { if (event.key === 'Escape' && reinstallModal && reinstallModal.classList.contains('show')) { closeReinstallModal(); } }); return { open: openReinstallModal, close: closeReinstallModal }; } const api = { createReinstallController }; if (typeof module !== 'undefined' && module.exports) module.exports = api; else root.ADPanelReinstall = api; })(typeof window !== 'undefined' ? window : globalThis);