{"version":3,"file":"homeApps.cjs","names":["splitHostId","qualifyHostId"],"sources":["../../../src/web/lib/homeApps.ts"],"sourcesContent":["import { qualifyHostId, splitHostId } from './hostIdentity';\n\ninterface HomeHost {\n  id: string;\n  person?: { issuer: string; subject: string };\n  connected?: boolean;\n}\n\ntype FillScope = { combinesComputers?: unknown; dataScope?: unknown } | undefined;\n\n/**\n * One card per computer, except for a plugin whose page joins every computer (its fill sets\n * `data.combinesComputers`, as the calendar does): that one shows once per person. Unlinked\n * computers join the phone's one account, or share a card of their own when it holds several.\n * A fill with `data.dataScope: 'global'` reads a shared backend, so it also shows once per person,\n * from that person's first connected computer, and is marked `global` so it reads only that one.\n */\nexport function homeAppEntries<T extends { pluginId: string; id: string; data?: unknown }>(\n  hosts: readonly HomeHost[],\n  read: (hostId: string) => readonly T[],\n) {\n  const people = new Set(hosts.flatMap((host) => (host.person ? [JSON.stringify(host.person)] : [])));\n  const unlinked = people.size === 1 ? [...people][0]! : 'unlinked';\n  const fills = hosts.map((host) => ({ host, fills: read(host.id) }));\n  const keyOf = (host: HomeHost, fill: T) => {\n    const scope = fill.data as FillScope;\n    const perPerson =\n      scope?.dataScope === 'global' || (scope?.dataScope !== 'host' && scope?.combinesComputers === true);\n    const owner = perPerson ? (host.person ? JSON.stringify(host.person) : unlinked) : host.id;\n    return JSON.stringify([owner, splitHostId(fill.pluginId)?.localId ?? fill.pluginId, fill.id]);\n  };\n  // A global fill is served by the first connected computer that has it, else the first one.\n  const globalHost = new Map<string, string>();\n  for (const connectedPass of [true, false])\n    for (const { host, fills: hostFills } of fills)\n      if ((host.connected !== false) === connectedPass)\n        for (const fill of hostFills)\n          if ((fill.data as FillScope)?.dataScope === 'global') {\n            const key = keyOf(host, fill);\n            if (!globalHost.has(key)) globalHost.set(key, host.id);\n          }\n  const seen = new Set<string>();\n  return fills.flatMap(({ host, fills: hostFills }) =>\n    hostFills.flatMap((fill) => {\n      const key = keyOf(host, fill);\n      const chosen = globalHost.get(key);\n      if (seen.has(key) || (chosen !== undefined && chosen !== host.id)) return [];\n      seen.add(key);\n      return [{ hostId: host.id, fill, key, global: chosen !== undefined }];\n    }),\n  );\n}\n\n/** Page ids retain the supplying host, independent of focus and connection order. */\nexport function homePageRoute(hostId: string, pageId: string) {\n  return {\n    pageId: qualifyHostId(hostId, pageId),\n    title: pageId.charAt(0).toUpperCase() + pageId.slice(1).replace(/-/g, ' '),\n  };\n}\n"],"mappings":";;;;;;;;;AAiBA,SAAgB,eACd,OACA,MACA;CACA,MAAM,SAAS,IAAI,IAAI,MAAM,SAAS,SAAU,KAAK,SAAS,CAAC,KAAK,UAAU,KAAK,MAAM,CAAC,IAAI,CAAC,CAAE,CAAC;CAClG,MAAM,WAAW,OAAO,SAAS,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,KAAM;CACvD,MAAM,QAAQ,MAAM,KAAK,UAAU;EAAE;EAAM,OAAO,KAAK,KAAK,EAAE;CAAE,EAAE;CAClE,MAAM,SAAS,MAAgB,SAAY;EACzC,MAAM,QAAQ,KAAK;EAGnB,MAAM,QADJ,OAAO,cAAc,YAAa,OAAO,cAAc,UAAU,OAAO,sBAAsB,OACrE,KAAK,SAAS,KAAK,UAAU,KAAK,MAAM,IAAI,WAAY,KAAK;EACxF,OAAO,KAAK,UAAU;GAAC;GAAOA,qBAAAA,YAAY,KAAK,QAAQ,CAAC,EAAE,WAAW,KAAK;GAAU,KAAK;EAAE,CAAC;CAC9F;CAEA,MAAM,6BAAa,IAAI,IAAoB;CAC3C,KAAK,MAAM,iBAAiB,CAAC,MAAM,KAAK,GACtC,KAAK,MAAM,EAAE,MAAM,OAAO,eAAe,OACvC,IAAK,KAAK,cAAc,UAAW,eAC5B;OAAA,MAAM,QAAQ,WACjB,IAAK,KAAK,MAAoB,cAAc,UAAU;GACpD,MAAM,MAAM,MAAM,MAAM,IAAI;GAC5B,IAAI,CAAC,WAAW,IAAI,GAAG,GAAG,WAAW,IAAI,KAAK,KAAK,EAAE;EACvD;;CACR,MAA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