"use strict";async function getUserPreferences(e){let s=await RedisCache_1.redisCache.getUserPreferences(e);if(!s){const r=await db_1.models.user.findByPk(e,{attributes:["settings"]});if(!r)throw new errors_1.UserNotFoundError(e);const t=r.settings||{};s={email:void 0===t.email||t.email,sms:void 0!==t.sms&&t.sms,push:void 0!==t.push&&t.push,types:t.types,pushTokens:t.pushTokens};await RedisCache_1.redisCache.setUserPreferences(e,s)}return s}async function filterChannelsByPreferences(e,s){const r=await getUserPreferences(e);return s.filter(e=>{switch(e){case"EMAIL":return!0===r.email;case"SMS":return!0===r.sms;case"PUSH":return!0===r.push;case"IN_APP":case"WEBSOCKET":return!0;default:return!1}})}async function isNotificationTypeEnabled(e,s){const r=await getUserPreferences(e);if(!r.types)return!0;const t=s.toLowerCase();return!1!==r.types[t]}async function filterChannelsByPreferencesAndType(e,s,r){return await isNotificationTypeEnabled(e,r)?await filterChannelsByPreferences(e,s):[]}async function updateUserPreferences(e,s){const r=await db_1.models.user.findByPk(e);if(!r)throw new errors_1.UserNotFoundError(e);const t={...r.settings||{},...s};await db_1.models.user.update({settings:t},{where:{id:e}});await RedisCache_1.redisCache.clearUserPreferences(e)}async function getUserPushTokens(e){const s=await getUserPreferences(e);return s.pushTokens&&Array.isArray(s.pushTokens)?s.pushTokens.filter(e=>e&&e.token).map(e=>e.token):[]}async function addPushToken(e,s,r){const t=(await getUserPreferences(e)).pushTokens||[],n=t.findIndex(e=>e.token===s);n>=0?t[n].lastActive=(new Date).toISOString():t.push({token:s,deviceType:r,lastActive:(new Date).toISOString()});await updateUserPreferences(e,{pushTokens:t})}async function removePushToken(e,s){const r=await getUserPreferences(e);if(!r.pushTokens)return;const t=r.pushTokens.filter(e=>e.token!==s);await updateUserPreferences(e,{pushTokens:t})}async function getUserContactInfo(e,s){const r=await db_1.models.user.findByPk(e,{attributes:["email","phone","settings"]});if(!r)throw new errors_1.UserNotFoundError(e);switch(s){case"EMAIL":return r.email||null;case"SMS":return r.phone||null;case"PUSH":const s=await getUserPushTokens(e);return s.length>0?s[0]:null;default:return null}}async function hasValidContactMethod(e,s){const r=await getUserContactInfo(e,s);return null!==r&&r.length>0}Object.defineProperty(exports,"__esModule",{value:!0});exports.getUserPreferences=getUserPreferences;exports.filterChannelsByPreferences=filterChannelsByPreferences;exports.isNotificationTypeEnabled=isNotificationTypeEnabled;exports.filterChannelsByPreferencesAndType=filterChannelsByPreferencesAndType;exports.updateUserPreferences=updateUserPreferences;exports.getUserPushTokens=getUserPushTokens;exports.addPushToken=addPushToken;exports.removePushToken=removePushToken;exports.getUserContactInfo=getUserContactInfo;exports.hasValidContactMethod=hasValidContactMethod;const db_1=require("@b/db"),RedisCache_1=require("../cache/RedisCache"),errors_1=require("../errors");