{"version":3,"file":"accountStore.mjs","names":[],"sources":["../../../src/web/stores/accountStore.ts"],"sourcesContent":["import { Store } from '@tanstack/store';\n\n/** The signed-in state a host's sign-in bridge reports. Credentials never reach the page. */\nexport interface PoAccountStatus {\n  signedIn: boolean;\n  email?: string;\n}\n\n/**\n * Sign in with Po, as the host offers it: Po Desktop's bridge today. The browser does the signing\n * in; `signIn` opens it and answers the code its page should show.\n */\nexport interface PoAccountBridge {\n  status(): Promise<PoAccountStatus>;\n  signIn(): Promise<{ code: string; url: string }>;\n  openSignIn(): Promise<void>;\n  waitForSignIn(): Promise<PoAccountStatus>;\n  cancelSignIn(): Promise<void>;\n  signOut(): Promise<PoAccountStatus>;\n}\n\nexport interface AccountState {\n  /** Undefined where this Po cannot sign in to Po, so no account entry or sign-in step shows. */\n  bridge?: PoAccountBridge;\n  /** Opens the host's account page, where signing in shows its code and a signed-in person signs out. */\n  openAccount?: () => void;\n  /** False only while a bound bridge has not answered its first status; with no bridge there is nothing to wait for. */\n  checked: boolean;\n  status: PoAccountStatus;\n}\n\nconst SIGNED_OUT: PoAccountStatus = { signedIn: false };\n\nexport const accountStore = new Store<AccountState>({ checked: true, status: SIGNED_OUT });\n\n/** Reads the bridge's status again, after a sign-in or sign-out somewhere else. */\nexport async function refreshAccount(): Promise<void> {\n  const { bridge } = accountStore.state;\n  if (bridge === undefined) return;\n  try {\n    const status = await bridge.status();\n    if (accountStore.state.bridge === bridge) accountStore.setState((state) => ({ ...state, checked: true, status }));\n  } catch (error) {\n    console.warn('Account status unavailable.', error);\n    if (accountStore.state.bridge === bridge) accountStore.setState((state) => ({ ...state, checked: true }));\n  }\n}\n\n/** The host binds its bridge once at startup; the returned function unbinds it. */\nexport function bindAccount(bridge: PoAccountBridge | undefined, openAccount?: () => void): () => void {\n  accountStore.setState(() => ({ bridge, openAccount, checked: bridge === undefined, status: SIGNED_OUT }));\n  void refreshAccount();\n  return () => {\n    if (accountStore.state.bridge === bridge) accountStore.setState(() => ({ checked: true, status: SIGNED_OUT }));\n  };\n}\n\nexport async function signOutAccount(): Promise<void> {\n  const { bridge } = accountStore.state;\n  if (bridge === undefined) return;\n  const status = await bridge.signOut();\n  accountStore.setState((state) => ({ ...state, status }));\n}\n"],"mappings":";;AA+BA,MAAM,aAA8B,EAAE,UAAU,MAAM;AAEtD,MAAa,eAAe,IAAI,MAAoB;CAAE,SAAS;CAAM,QAAQ;AAAW,CAAC;;AAGzF,eAAsB,iBAAgC;CACpD,MAAM,EAAE,WAAW,aAAa;CAChC,IAAI,WAAW,KAAA,GAAW;CAC1B,IAAI;EACF,MAAM,SAAS,MAAM,OAAO,OAAO;EACnC,IAAI,aAAa,MAAM,WAAW,QAAQ,aAAa,UAAU,WAAW;GAAE,GAAG;GAAO,SAAS;GAAM;EAAO,EAAE;CAClH,SAAS,OAAO;EACd,QAAQ,KAAK,+BAA+B,KAAK;EACjD,IAAI,aAAa,MAAM,WAAW,QAAQ,aAAa,UAAU,WAAW;GAAE,GAAG;GAAO,SAAS;EAAK,EAAE;CAC1G;AACF;;AAGA,SAAgB,YAAY,QAAqC,aAAsC;CACrG,aAAa,gBAAgB;EAAE;EAAQ;EAAa,SAAS,WAAW,KAAA;EAAW,QAAQ;CAAW,EAAE;CACxG,eAAoB;CACpB,aAAa;EACX,IAAI,aAAa,MAAM,WAAW,QAAQ,aAAa,gBAAgB;GAAE,SAAS;GAAM,QAAQ;EAAW,EAAE;CAC/G;AACF;AAEA,eAAsB,iBAAgC;CACpD,MAAM,EAAE,WAAW,aAAa;CAChC,IAAI,WAAW,KAAA,GAAW;CAC1B,MAAM,SAAS,MAAM,OAAO,QAAQ;CACpC,aAAa,UAAU,WAAW;EAAE,GAAG;EAAO;CAAO,EAAE;AACzD"}