"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PWA_NOTIFICATION_TOKEN_STORAGE_KEY = exports.NOTIFICATION_DEVICE_ID_STORAGE_KEY = void 0;
exports.buildScopedPwaNotificationTargetId = buildScopedPwaNotificationTargetId;
exports.getSavedNotificationDeviceId = getSavedNotificationDeviceId;
exports.getSavedPwaNotificationToken = getSavedPwaNotificationToken;
exports.setSavedPwaNotificationToken = setSavedPwaNotificationToken;
exports.clearNotificationIdentity = clearNotificationIdentity;
exports.getAutomationNotificationPreferenceTarget = getAutomationNotificationPreferenceTarget;
var clientIdentity_1 = require("./clientIdentity");
exports.NOTIFICATION_DEVICE_ID_STORAGE_KEY = 'work-server.notification.device-id';
exports.PWA_NOTIFICATION_TOKEN_STORAGE_KEY = 'work-server.notification.pwa-token';
function buildScopedPwaNotificationTargetId(token, clientId) {
    return [token.trim(), clientId.trim()].filter(Boolean).join('::client::');
}
function getSavedNotificationDeviceId() {
    if (typeof window === 'undefined') {
        return '';
    }
    var clientId = (0, clientIdentity_1.getOrCreateClientId)();
    if (clientId) {
        window.localStorage.setItem(exports.NOTIFICATION_DEVICE_ID_STORAGE_KEY, clientId);
        return clientId;
    }
    var saved = window.localStorage.getItem(exports.NOTIFICATION_DEVICE_ID_STORAGE_KEY);
    if (saved) {
        return saved;
    }
    var generated = "web-".concat(Date.now());
    window.localStorage.setItem(exports.NOTIFICATION_DEVICE_ID_STORAGE_KEY, generated);
    return generated;
}
function getSavedPwaNotificationToken() {
    var _a, _b;
    if (typeof window === 'undefined') {
        return '';
    }
    return (_b = (_a = window.localStorage.getItem(exports.PWA_NOTIFICATION_TOKEN_STORAGE_KEY)) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
}
function setSavedPwaNotificationToken(token) {
    var _a;
    if (typeof window === 'undefined') {
        return;
    }
    var normalizedToken = (_a = token === null || token === void 0 ? void 0 : token.trim()) !== null && _a !== void 0 ? _a : '';
    if (normalizedToken) {
        window.localStorage.setItem(exports.PWA_NOTIFICATION_TOKEN_STORAGE_KEY, normalizedToken);
    }
    else {
        window.localStorage.removeItem(exports.PWA_NOTIFICATION_TOKEN_STORAGE_KEY);
    }
}
function clearNotificationIdentity() {
    if (typeof window === 'undefined') {
        return;
    }
    window.localStorage.removeItem(exports.NOTIFICATION_DEVICE_ID_STORAGE_KEY);
    window.localStorage.removeItem(exports.PWA_NOTIFICATION_TOKEN_STORAGE_KEY);
    (0, clientIdentity_1.clearClientId)();
}
function getAutomationNotificationPreferenceTarget() {
    var pwaToken = getSavedPwaNotificationToken();
    var clientId = getSavedNotificationDeviceId();
    if (pwaToken && clientId) {
        return {
            targetKind: 'ios-token-client',
            targetId: buildScopedPwaNotificationTargetId(pwaToken, clientId),
        };
    }
    if (pwaToken) {
        return {
            targetKind: 'ios-token',
            targetId: pwaToken,
        };
    }
    if (!clientId) {
        return null;
    }
    return {
        targetKind: 'client',
        targetId: clientId,
    };
}
