{"version":3,"file":"onboardingStatus.mjs","names":[],"sources":["../../../../src/web/features/plugins/onboardingStatus.ts"],"sourcesContent":["import { t, type OnboardingContext, type OnboardingContribution } from '@agimon-ai/po-core/web';\nimport { browserTransport, fetchWithStepUp } from '@agimon-ai/po-web-security/browser';\n\nimport { listProviders } from '../../lib/authApi';\n\nexport type OnboardingCheck = Awaited<ReturnType<OnboardingContribution['status']>>;\n\nasync function isSignedIn(providerId: string): Promise<boolean> {\n  const result = await listProviders();\n  if ('error' in result) throw new Error(result.error);\n  return result.providers.find((provider) => provider.id === providerId)?.authenticated !== undefined;\n}\n\n/** What a plugin's onboarding may use: the host's own transports and sign-in state, never a hand-built URL. */\nexport function onboardingContext(pluginId: string): OnboardingContext {\n  return {\n    pluginId,\n    scope: { scope: 'host' },\n    request: (input, init) => browserTransport.fetch(input, init),\n    requestWithStepUp: fetchWithStepUp,\n    isSignedIn,\n  };\n}\n\n/** A plugin's status check gets this long before the frame says it couldn't check. */\nexport const ONBOARDING_STATUS_TIMEOUT_MS = 10_000;\n\n/**\n * Runs a plugin's `status()` with a deadline. A throw, a rejection, a timeout or an answer outside the\n * contract all read as `failed`: the host never guesses `ready`.\n */\nexport async function checkOnboarding(\n  onboarding: Pick<OnboardingContribution, 'status'>,\n  context: OnboardingContext,\n  timeoutMs = ONBOARDING_STATUS_TIMEOUT_MS,\n): Promise<OnboardingCheck> {\n  let timer: ReturnType<typeof setTimeout> | undefined;\n  const deadline = new Promise<OnboardingCheck>((resolve) => {\n    timer = setTimeout(\n      () =>\n        resolve({ state: 'failed', detail: t('webDashboard.plugins.onboarding.timeout', 'The check took too long.') }),\n      timeoutMs,\n    );\n  });\n  try {\n    const result = await Promise.race([Promise.resolve().then(() => onboarding.status(context)), deadline]);\n    if (result?.state === 'ready' || result?.state === 'needs-setup' || result?.state === 'failed') {\n      return typeof result.detail === 'string'\n        ? { state: result.state, detail: result.detail }\n        : { state: result.state };\n    }\n    return {\n      state: 'failed',\n      detail: t('webDashboard.plugins.onboarding.badAnswer', 'The plugin gave an answer Po does not understand.'),\n    };\n  } catch (error) {\n    return {\n      state: 'failed',\n      detail:\n        error instanceof Error\n          ? error.message\n          : t('webDashboard.plugins.onboarding.stopped', 'The check stopped part way.'),\n    };\n  } finally {\n    clearTimeout(timer);\n  }\n}\n"],"mappings":";;;;AAOA,eAAe,WAAW,YAAsC;CAC9D,MAAM,SAAS,MAAM,cAAc;CACnC,IAAI,WAAW,QAAQ,MAAM,IAAI,MAAM,OAAO,KAAK;CACnD,OAAO,OAAO,UAAU,MAAM,aAAa,SAAS,OAAO,UAAU,CAAC,EAAE,kBAAkB,KAAA;AAC5F;;AAGA,SAAgB,kBAAkB,UAAqC;CACrE,OAAO;EACL;EACA,OAAO,EAAE,OAAO,OAAO;EACvB,UAAU,OAAO,SAAS,iBAAiB,MAAM,OAAO,IAAI;EAC5D,mBAAmB;EACnB;CACF;AACF;;AAGA,MAAa,+BAA+B;;;;;AAM5C,eAAsB,gBACpB,YACA,SACA,YAAY,8BACc;CAC1B,IAAI;CACJ,MAAM,WAAW,IAAI,SAA0B,YAAY;EACzD,QAAQ,iBAEJ,QAAQ;GAAE,OAAO;GAAU,QAAQ,EAAE,2CAA2C,0BAA0B;EAAE,CAAC,GAC/G,SACF;CACF,CAAC;CACD,IAAI;EACF,MAAM,SAAS,MAAM,QAAQ,KAAK,CAAC,QAAQ,QAAQ,CAAC,CAAC,WAAW,WAAW,OAAO,OAAO,CAAC,GAAG,QAAQ,CAAC;EACtG,IAAI,QAAQ,UAAU,WAAW,QAAQ,UAAU,iBAAiB,QAAQ,UAAU,UACpF,OAAO,OAAO,OAAO,WAAW,WAC5B;GAAE,OAAO,OAAO;GAAO,QAAQ,OAAO;EAAO,IAC7C,EAAE,OAAO,OAAO,MAAM;EAE5B,OAAO;GACL,OAAO;GACP,QAAQ,EAAE,6CAA6C,mDAAmD;EAC5G;CACF,SAAS,OAAO;EACd,OAAO;GACL,OAAO;GACP,QACE,iBAAiB,QACb,MAAM,UACN,EAAE,2CAA2C,6BAA6B;EAClF;CACF,UAAU;EACR,aAAa,KAAK;CACpB;AACF"}