import {
isAcceptableNewAccessPin,
isForbiddenAccessPin,
normalizePinDigits
} from '../lib/ellyAccessPin.js';
document.addEventListener('DOMContentLoaded', () => {
const openBtn = /** @type {HTMLButtonElement|null} */ (document.getElementById('openOptions'));
const closeBtn = document.getElementById('closeTab');
const saveBtn = document.getElementById('saveAccessPin');
const errEl = document.getElementById('welcomeAccessPinError');
const doneEl = document.getElementById('welcomeAccessPinDone');
/**
* Enables Open Settings when Access PIN is configured.
* @param {boolean} configured
* @returns {void}
*/
function setOpenSettingsEnabled(configured) {
if (!openBtn) {
return;
}
openBtn.disabled = !configured;
openBtn.title = configured ? 'Open Elly settings' : 'Set your Access PIN first';
if (doneEl) {
doneEl.hidden = !configured;
}
}
/**
* Refreshes Access PIN configured state from the service worker.
* @returns {Promise<void>}
*/
async function refreshConfiguredState() {
try {
const status = await chrome.runtime.sendMessage({ action: 'getAppStatus' });
setOpenSettingsEnabled(!!(status?.configured ?? status?.pinSet));
} catch {
setOpenSettingsEnabled(false);
}
}
refreshConfiguredState().catch(() => setOpenSettingsEnabled(false));
saveBtn?.addEventListener('click', async () => {
const a = normalizePinDigits(document.getElementById('welcomeAccessPin')?.value);
const b = normalizePinDigits(document.getElementById('welcomeAccessPinConfirm')?.value);
if (errEl) {
errEl.hidden = true;
errEl.textContent = '';
}
if (!isAcceptableNewAccessPin(a)) {
if (errEl) {
errEl.textContent = isForbiddenAccessPin(a)
? 'That PIN is not allowed. Choose a different 8-digit PIN.'
: 'Enter an 8-digit Access PIN.';
errEl.hidden = false;
}
return;
}
if (a !== b) {
if (errEl) {
errEl.textContent = 'PINs do not match.';
errEl.hidden = false;
}
return;
}
try {
const response = await chrome.runtime.sendMessage({
action: 'setNewPIN',
newPin: a
});
if (!response?.success) {
if (errEl) {
errEl.textContent = response?.error || 'Could not save Access PIN';
errEl.hidden = false;
}
return;
}
const pinA = /** @type {HTMLInputElement|null} */ (document.getElementById('welcomeAccessPin'));
const pinB = /** @type {HTMLInputElement|null} */ (document.getElementById('welcomeAccessPinConfirm'));
if (pinA) pinA.value = '';
if (pinB) pinB.value = '';
setOpenSettingsEnabled(true);
} catch (error) {
if (errEl) {
errEl.textContent = 'Error saving Access PIN. Please try again.';
errEl.hidden = false;
}
console.error(error);
}
});
if (openBtn) {
openBtn.addEventListener('click', async () => {
try {
const status = await chrome.runtime.sendMessage({ action: 'getAppStatus' });
if (!(status?.configured ?? status?.pinSet)) {
if (errEl) {
errEl.textContent = 'Set your Access PIN before opening Settings.';
errEl.hidden = false;
}
setOpenSettingsEnabled(false);
return;
}
} catch {
/* proceed with open attempt */
}
if (chrome.runtime?.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
chrome.tabs.create({ url: chrome.runtime.getURL('options/options.html') });
}
});
}
if (closeBtn) {
closeBtn.addEventListener('click', () => {
window.close();
});
}
});