interface IdentityApi { createSecureUuid: () => string; } interface IdentityWindow extends Window { FotogenicAI?: { Shared?: { identity?: IdentityApi; }; }; } const UUID_V4_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i; const createSecureUuid = (): string => { const source = globalThis.crypto; if (!source) return ''; if (typeof source.randomUUID === 'function') { try { const uuid = source.randomUUID(); if (UUID_V4_PATTERN.test(uuid)) return uuid.toLowerCase(); } catch { // Continue to the secure byte-based source when the preferred API is unavailable at call time. } } if (typeof source.getRandomValues !== 'function') return ''; try { const bytes = source.getRandomValues(new Uint8Array(16)); const versionByte = bytes[6]; const variantByte = bytes[8]; if (versionByte === undefined || variantByte === undefined) return ''; bytes[6] = (versionByte & 0x0f) | 0x40; bytes[8] = (variantByte & 0x3f) | 0x80; const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join(''); return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; } catch { return ''; } }; const globalWindow = window as IdentityWindow; globalWindow.FotogenicAI = globalWindow.FotogenicAI ?? {}; globalWindow.FotogenicAI.Shared = globalWindow.FotogenicAI.Shared ?? {}; globalWindow.FotogenicAI.Shared.identity = Object.freeze({ createSecureUuid }); export type { IdentityApi }; export { createSecureUuid };