(function (global) { const storage = global.BeaStorageService; const PROFILES_KEY = "profiles"; const ACTIVE_PROFILE_KEY = "active-profile-id"; function createProfileId() { const randomPart = global.crypto && typeof global.crypto.randomUUID === "function" ? global.crypto.randomUUID().replace(/-/g, "").slice(0, 10).toUpperCase() : Math.random().toString(36).slice(2, 12).toUpperCase(); return `BRC-${new Date().toISOString().slice(0, 10).replace(/-/g, "")}-${randomPart}`; } function getProfiles() { return storage.read(PROFILES_KEY, []); } function getActiveProfile() { const activeId = storage.read(ACTIVE_PROFILE_KEY, ""); return getProfiles().find((profile) => profile.profileId === activeId) || null; } function login(profileInput) { const profiles = getProfiles(); const normalizedEmail = String(profileInput.email || "").trim().toLowerCase(); const existingProfile = profiles.find((profile) => profile.email.toLowerCase() === normalizedEmail); const timestamp = new Date().toISOString(); const profile = { ...(existingProfile || {}), profileId: existingProfile ? existingProfile.profileId : createProfileId(), displayName: String(profileInput.displayName || "").trim(), email: normalizedEmail, organization: String(profileInput.organization || "").trim(), role: String(profileInput.role || "").trim(), region: String(profileInput.region || "").trim(), purposeOfUse: `Role-scoped access: ${String(profileInput.role || existingProfile?.role || "Care Operations").trim()}`, createdAt: existingProfile ? existingProfile.createdAt : timestamp, lastLoginAt: timestamp }; const nextProfiles = existingProfile ? profiles.map((candidate) => candidate.profileId === profile.profileId ? profile : candidate) : [profile, ...profiles]; storage.write(PROFILES_KEY, nextProfiles); storage.write(ACTIVE_PROFILE_KEY, profile.profileId); return profile; } function update(profileId, profileInput) { const profiles = getProfiles(); const existingProfile = profiles.find((profile) => profile.profileId === profileId); if (!existingProfile) { return null; } const profile = { ...existingProfile, displayName: String(profileInput.displayName || "").trim(), email: String(profileInput.email || "").trim().toLowerCase(), organization: String(profileInput.organization || "").trim(), role: String(profileInput.role || "").trim(), region: String(profileInput.region || "").trim(), jobTitle: String(profileInput.jobTitle || "").trim(), phone: String(profileInput.phone || "").trim(), preferredContact: String(profileInput.preferredContact || "").trim(), timezone: String(profileInput.timezone || "").trim(), language: String(profileInput.language || "").trim(), purposeOfUse: `Role-scoped access: ${String(profileInput.role || existingProfile.role || "Care Operations").trim()}`, profileNotes: String(profileInput.profileNotes || "").trim(), updatedAt: new Date().toISOString() }; storage.write(PROFILES_KEY, profiles.map((candidate) => candidate.profileId === profileId ? profile : candidate)); return profile; } function activate(profileId) { const profile = getProfiles().find((candidate) => candidate.profileId === profileId); if (!profile) { return null; } storage.write(ACTIVE_PROFILE_KEY, profile.profileId); return profile; } function logout() { storage.remove(ACTIVE_PROFILE_KEY); } function getInitials(profile) { return String(profile && profile.displayName ? profile.displayName : "BC") .split(/\s+/) .filter(Boolean) .slice(0, 2) .map((part) => part.charAt(0).toUpperCase()) .join("") || "BC"; } global.BeaProfileManager = Object.freeze({ getProfiles, getActiveProfile, login, update, activate, logout, getInitials }); })(window);