{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/awareness/utils.ts"],"sourcesContent":["import type { CollaboratorInfo } from './types';\n\n/**\n * Get the browser name from the user agent.\n * @return The browser name.\n */\nfunction getBrowserName(): string {\n\tconst userAgent = window.navigator.userAgent;\n\tlet browserName = 'Unknown';\n\n\tif ( userAgent.includes( 'Firefox' ) ) {\n\t\tbrowserName = 'Firefox';\n\t} else if ( userAgent.includes( 'Edg' ) ) {\n\t\tbrowserName = 'Microsoft Edge';\n\t} else if (\n\t\tuserAgent.includes( 'Chrome' ) &&\n\t\t! userAgent.includes( 'Edg' )\n\t) {\n\t\tbrowserName = 'Chrome';\n\t} else if (\n\t\tuserAgent.includes( 'Safari' ) &&\n\t\t! userAgent.includes( 'Chrome' )\n\t) {\n\t\tbrowserName = 'Safari';\n\t} else if (\n\t\tuserAgent.includes( 'MSIE' ) ||\n\t\tuserAgent.includes( 'Trident' )\n\t) {\n\t\tbrowserName = 'Internet Explorer';\n\t} else if ( userAgent.includes( 'Opera' ) || userAgent.includes( 'OPR' ) ) {\n\t\tbrowserName = 'Opera';\n\t}\n\n\treturn browserName;\n}\n\nexport function areMapsEqual< Key, Value >(\n\tmap1: Map< Key, Value >,\n\tmap2: Map< Key, Value >,\n\tcomparatorFn: ( value1: Value, value2: Value ) => boolean\n): boolean {\n\tif ( map1.size !== map2.size ) {\n\t\treturn false;\n\t}\n\n\tfor ( const [ key, value1 ] of map1.entries() ) {\n\t\tif ( ! map2.has( key ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif ( ! comparatorFn( value1, map2.get( key )! ) ) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\n/**\n * Check if two collaborator infos are equal.\n *\n * @param collaboratorInfo1 - The first collaborator info.\n * @param collaboratorInfo2 - The second collaborator info.\n * @return True if the collaborator infos are equal, false otherwise.\n */\nexport function areCollaboratorInfosEqual(\n\tcollaboratorInfo1?: CollaboratorInfo,\n\tcollaboratorInfo2?: CollaboratorInfo\n): boolean {\n\tif ( ! collaboratorInfo1 || ! collaboratorInfo2 ) {\n\t\treturn collaboratorInfo1 === collaboratorInfo2;\n\t}\n\n\tif (\n\t\tObject.keys( collaboratorInfo1 ).length !==\n\t\tObject.keys( collaboratorInfo2 ).length\n\t) {\n\t\treturn false;\n\t}\n\n\treturn Object.entries( collaboratorInfo1 ).every( ( [ key, value ] ) => {\n\t\t// Update this function with any non-primitive fields added to CollaboratorInfo.\n\t\treturn value === collaboratorInfo2[ key as keyof CollaboratorInfo ];\n\t} );\n}\n\nfunction hasValidAvatarUrls(\n\tvalue: unknown\n): value is NonNullable< CollaboratorInfo[ 'avatar_urls' ] > {\n\tif (\n\t\t'object' !== typeof value ||\n\t\tnull === value ||\n\t\tArray.isArray( value )\n\t) {\n\t\treturn false;\n\t}\n\n\treturn [ '24', '48', '96' ].every(\n\t\t( size ) => ! ( size in value ) || 'string' === typeof value[ size ]\n\t);\n}\n\n/**\n * Check that awareness information contains the fields required to present a\n * collaborator. Awareness is supplied by peers, so its runtime shape cannot be\n * guaranteed by the local TypeScript type.\n *\n * @param value - The collaborator information to check.\n * @return Whether the collaborator can be presented safely.\n */\nexport function isCollaboratorInfo(\n\tvalue: unknown\n): value is CollaboratorInfo {\n\tif ( 'object' !== typeof value || null === value ) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\t'id' in value &&\n\t\t'name' in value &&\n\t\t'slug' in value &&\n\t\t'browserType' in value &&\n\t\t'enteredAt' in value &&\n\t\t( null === value.id ||\n\t\t\t( 'number' === typeof value.id &&\n\t\t\t\tNumber.isInteger( value.id ) &&\n\t\t\t\tvalue.id > 0 ) ) &&\n\t\t'string' === typeof value.name &&\n\t\t'' !== value.name.trim() &&\n\t\t'string' === typeof value.slug &&\n\t\t( ! ( 'avatar_urls' in value ) ||\n\t\t\thasValidAvatarUrls( value.avatar_urls ) ) &&\n\t\t'string' === typeof value.browserType &&\n\t\t'number' === typeof value.enteredAt &&\n\t\tNumber.isFinite( value.enteredAt )\n\t);\n}\n\n/**\n * Generate a collaborator info object from a current collaborator.\n *\n * @param currentCollaborator - The current collaborator, when available.\n * @param clientId            - The Yjs client ID used for fallback identity.\n * @return The collaborator info object.\n */\nexport function generateCollaboratorInfo(\n\tcurrentCollaborator: unknown,\n\tclientId: number\n): CollaboratorInfo {\n\tconst presentationInfo = {\n\t\tbrowserType: getBrowserName(),\n\t\tenteredAt: Date.now(),\n\t};\n\n\tif ( 'object' === typeof currentCollaborator && currentCollaborator ) {\n\t\tconst user = currentCollaborator;\n\t\tif ( 'id' in user && 'name' in user ) {\n\t\t\tconst collaboratorInfo = {\n\t\t\t\t...presentationInfo,\n\t\t\t\t...( 'avatar_urls' in user &&\n\t\t\t\thasValidAvatarUrls( user.avatar_urls )\n\t\t\t\t\t? { avatar_urls: user.avatar_urls }\n\t\t\t\t\t: {} ),\n\t\t\t\tid: user.id,\n\t\t\t\tname: user.name,\n\t\t\t\tslug:\n\t\t\t\t\t'slug' in user && 'string' === typeof user.slug\n\t\t\t\t\t\t? user.slug\n\t\t\t\t\t\t: '',\n\t\t\t};\n\n\t\t\tif (\n\t\t\t\tisCollaboratorInfo( collaboratorInfo ) &&\n\t\t\t\tnull !== collaboratorInfo.id\n\t\t\t) {\n\t\t\t\treturn collaboratorInfo;\n\t\t\t}\n\t\t}\n\t}\n\n\t// The Yjs client ID remains available on the surrounding awareness state for\n\t// session-specific UI identity and also makes the fallback slug deterministic.\n\treturn {\n\t\t...presentationInfo,\n\t\tid: null,\n\t\t// Keep shared awareness data language-neutral. The editor localizes this\n\t\t// fallback name for the viewer when it is displayed.\n\t\tname: 'Anonymous User',\n\t\tslug: `anonymous-${ clientId }`,\n\t};\n}\n\nexport function getRecordValue< RecordType, Key extends keyof RecordType >(\n\tobj: unknown,\n\tkey: Key\n): RecordType[ Key ] | null {\n\tif ( 'object' === typeof obj && null !== obj && key in obj ) {\n\t\treturn ( obj as RecordType )[ key ];\n\t}\n\n\treturn null;\n}\n\nexport function getTypedKeys< T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