import webPush from 'web-push';
import { vapidKeysDb } from '../database/db.js';
let cachedKeys = null;
function ensureVapidKeys() {
    if (cachedKeys)
        return cachedKeys;
    const row = vapidKeysDb.getLatest();
    if (row) {
        cachedKeys = { publicKey: row.public_key, privateKey: row.private_key };
        return cachedKeys;
    }
    const keys = webPush.generateVAPIDKeys();
    vapidKeysDb.insert(keys.publicKey, keys.privateKey);
    cachedKeys = keys;
    return cachedKeys;
}
function getPublicKey() {
    return ensureVapidKeys().publicKey;
}
function configureWebPush() {
    const keys = ensureVapidKeys();
    webPush.setVapidDetails('mailto:noreply@pixcode.local', keys.publicKey, keys.privateKey);
    console.log('Web Push notifications configured');
}
export { ensureVapidKeys, getPublicKey, configureWebPush };
//# sourceMappingURL=vapid-keys.js.map