{
  "version": 3,
  "sources": ["../src/createPresenceStateDerivation.ts"],
  "sourcesContent": ["import { Signal, computed } from '@tldraw/state'\nimport { TLStore } from './TLStore'\nimport { CameraRecordType } from './records/TLCamera'\nimport { TLINSTANCE_ID } from './records/TLInstance'\nimport { InstancePageStateRecordType } from './records/TLPageState'\nimport { TLPOINTER_ID } from './records/TLPointer'\nimport { InstancePresenceRecordType, TLInstancePresence } from './records/TLPresence'\n\n/**\n * The information about a user which is used for multiplayer features.\n * @public\n */\nexport interface TLPresenceUserInfo {\n\t/**\n\t * id - A unique identifier for the user. This should be the same across all devices and sessions.\n\t */\n\tid: string\n\t/**\n\t * The user's display name.\n\t */\n\tname?: string | null\n\t/**\n\t * The user's color. If not given, a random color will be assigned.\n\t */\n\tcolor?: string | null\n}\n\n/**\n * Creates a derivation that represents the current presence state of the current user.\n *\n * This function returns a derivation factory that, when given a store, creates a computed signal\n * containing the user's current presence state. The presence state includes information like cursor\n * position, selected shapes, camera position, and user metadata that gets synchronized in\n * multiplayer scenarios.\n *\n * @param $user - A reactive signal containing the user information\n * @param instanceId - Optional custom instance ID. If not provided, one will be generated based on the store ID\n * @returns A function that takes a store and returns a computed signal of the user's presence state\n *\n * @example\n * ```ts\n * import { createPresenceStateDerivation } from '@tldraw/tlschema'\n * import { atom } from '@tldraw/state'\n *\n * const userSignal = atom('user', { id: 'user-123', name: 'Alice', color: '#ff0000' })\n * const presenceDerivation = createPresenceStateDerivation(userSignal)\n *\n * // Use with a store to get reactive presence state\n * const presenceState = presenceDerivation(store)\n * console.log(presenceState.get()) // Current user presence or null\n * ```\n *\n * @public\n */\nexport function createPresenceStateDerivation(\n\t$user: Signal<TLPresenceUserInfo>,\n\tinstanceId?: TLInstancePresence['id']\n) {\n\treturn (store: TLStore): Signal<TLInstancePresence | null> => {\n\t\treturn computed('instancePresence', () => {\n\t\t\tconst user = $user.get()\n\t\t\tif (!user) return null\n\n\t\t\tconst state = getDefaultUserPresence(store, user)\n\t\t\tif (!state) return null\n\n\t\t\treturn InstancePresenceRecordType.create({\n\t\t\t\t...state,\n\t\t\t\tid: instanceId ?? InstancePresenceRecordType.createId(store.id),\n\t\t\t})\n\t\t})\n\t}\n}\n\n/**\n * The shape of data used to create a presence record.\n *\n * This type represents all the properties needed to construct a TLInstancePresence record.\n * It includes user information, cursor state, camera position, selected shapes, and other\n * presence-related data that gets synchronized across multiplayer clients.\n *\n * @public\n */\nexport type TLPresenceStateInfo = Parameters<(typeof InstancePresenceRecordType)['create']>[0]\n\n/**\n * Creates default presence state information for a user based on the current store state.\n *\n * This function extracts the current state from various store records (instance, page state,\n * camera, pointer) and combines them with user information to create a complete presence\n * state object. This is commonly used as a starting point for custom presence implementations.\n *\n * @param store - The tldraw store containing the current editor state\n * @param user - The user information to include in the presence state\n * @returns The default presence state info, or null if required store records are missing\n *\n * @example\n * ```ts\n * import { getDefaultUserPresence } from '@tldraw/tlschema'\n *\n * const user = { id: 'user-123', name: 'Alice', color: '#ff0000' }\n * const presenceInfo = getDefaultUserPresence(store, user)\n *\n * if (presenceInfo) {\n *   console.log('Current cursor:', presenceInfo.cursor)\n *   console.log('Selected shapes:', presenceInfo.selectedShapeIds)\n *   console.log('Camera position:', presenceInfo.camera)\n * }\n * ```\n *\n * @example\n * ```ts\n * // Common pattern: customize default presence\n * const customPresence = {\n *   ...getDefaultUserPresence(store, user),\n *   // Remove camera for privacy\n *   camera: undefined,\n *   // Add custom metadata\n *   customField: 'my-data'\n * }\n * ```\n *\n * @public\n */\nexport function getDefaultUserPresence(store: TLStore, user: TLPresenceUserInfo) {\n\tconst instance = store.get(TLINSTANCE_ID)\n\tconst pageState = store.get(InstancePageStateRecordType.createId(instance?.currentPageId))\n\tconst camera = store.get(CameraRecordType.createId(instance?.currentPageId))\n\tconst pointer = store.get(TLPOINTER_ID)\n\tif (!pageState || !instance || !camera || !pointer) {\n\t\treturn null\n\t}\n\n\treturn {\n\t\tselectedShapeIds: pageState.selectedShapeIds,\n\t\tbrush: instance.brush,\n\t\tscribbles: instance.scribbles,\n\t\tuserId: user.id,\n\t\tuserName: user.name ?? '',\n\t\tfollowingUserId: instance.followingUserId,\n\t\tcamera: {\n\t\t\tx: camera.x,\n\t\t\ty: camera.y,\n\t\t\tz: camera.z,\n\t\t},\n\t\tcolor: user.color ?? '#FF0000',\n\t\tcurrentPageId: instance.currentPageId,\n\t\tcursor: {\n\t\t\tx: pointer.x,\n\t\t\ty: pointer.y,\n\t\t\trotation: instance.cursor.rotation,\n\t\t\ttype: instance.cursor.type,\n\t\t},\n\t\tlastActivityTimestamp: pointer.lastActivityTimestamp,\n\t\tscreenBounds: instance.screenBounds,\n\t\tchatMessage: instance.chatMessage,\n\t\tmeta: {},\n\t} satisfies TLPresenceStateInfo\n}\n"],
  "mappings": "AAAA,SAAiB,gBAAgB;AAEjC,SAAS,wBAAwB;AACjC,SAAS,qBAAqB;AAC9B,SAAS,mCAAmC;AAC5C,SAAS,oBAAoB;AAC7B,SAAS,kCAAsD;AAgDxD,SAAS,8BACf,OACA,YACC;AACD,SAAO,CAAC,UAAsD;AAC7D,WAAO,SAAS,oBAAoB,MAAM;AACzC,YAAM,OAAO,MAAM,IAAI;AACvB,UAAI,CAAC,KAAM,QAAO;AAElB,YAAM,QAAQ,uBAAuB,OAAO,IAAI;AAChD,UAAI,CAAC,MAAO,QAAO;AAEnB,aAAO,2BAA2B,OAAO;AAAA,QACxC,GAAG;AAAA,QACH,IAAI,cAAc,2BAA2B,SAAS,MAAM,EAAE;AAAA,MAC/D,CAAC;AAAA,IACF,CAAC;AAAA,EACF;AACD;AAoDO,SAAS,uBAAuB,OAAgB,MAA0B;AAChF,QAAM,WAAW,MAAM,IAAI,aAAa;AACxC,QAAM,YAAY,MAAM,IAAI,4BAA4B,SAAS,UAAU,aAAa,CAAC;AACzF,QAAM,SAAS,MAAM,IAAI,iBAAiB,SAAS,UAAU,aAAa,CAAC;AAC3E,QAAM,UAAU,MAAM,IAAI,YAAY;AACtC,MAAI,CAAC,aAAa,CAAC,YAA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