{"version":3,"file":"firstRunProfile.cjs","names":[],"sources":["../../../src/web/lib/firstRunProfile.ts"],"sourcesContent":["import type { StarterPo } from './starterPos';\n\n/** Fetch-shaped and step-up aware: the browser's `fetchWithStepUp` or the phone's sealed transport. */\ntype Request = (input: string, init?: RequestInit) => Promise<Response>;\n\nconst PROFILE_ROUTE = '/api/plugins/profile/profiles';\nconst ME_ROUTE = '/api/plugins/profile/me';\nconst JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n/**\n * Creates the Po as a coordinator profile with its panda and personality, or keeps the one an earlier\n * first run made. Answers whether the Po is ready.\n */\nexport async function createStarterProfile(\n  po: StarterPo,\n  request: Request,\n  avatarPngBase64?: string,\n): Promise<boolean> {\n  let response: Response;\n  try {\n    response = await request(PROFILE_ROUTE, {\n      method: 'POST',\n      headers: JSON_HEADERS,\n      body: JSON.stringify({\n        alias: po.alias,\n        displayName: po.name,\n        preset: po.persona,\n        ...(avatarPngBase64 === undefined ? {} : { avatarPngBase64 }),\n      }),\n    });\n  } catch {\n    return false;\n  }\n  if (response.ok) return true;\n  const body: unknown = await response.json().catch(() => undefined);\n  const message = typeof body === 'object' && body !== null && 'error' in body ? String(body.error) : '';\n  // Someone already made a profile with this name, so the Po works with that one as it is.\n  return response.status === 409 && message.includes('taken');\n}\n\ninterface PersonProfile {\n  id: string;\n  revision: number;\n  name: string;\n  role: string;\n  objective: string;\n  aboutMe: string;\n  source?: 'global';\n}\n\nfunction isPersonProfile(body: unknown): body is PersonProfile {\n  if (typeof body !== 'object' || body === null) return false;\n  const profile = body as Record<string, unknown>;\n  return (\n    typeof profile.id === 'string' &&\n    typeof profile.revision === 'number' &&\n    ['name', 'role', 'objective', 'aboutMe'].every((key) => typeof profile[key] === 'string')\n  );\n}\n\n/**\n * Keeps the name and interests on the person's profile, where every Po reads them. Fields left empty\n * keep what the profile already says. Answers whether they were kept (or had nowhere to go).\n */\nexport async function saveStarterAboutYou(name: string, interests: string, request: Request): Promise<boolean> {\n  if (name.trim() === '' && interests.trim() === '') return true;\n  try {\n    const read = await request(ME_ROUTE);\n    // A host without the profile plugin, or one that keeps the person's profile from this caller (a phone\n    // paired without an account), has nowhere to keep them; the first session still uses them.\n    if (read.status === 404 || read.status === 403) return true;\n    const profile: unknown = await read.json().catch(() => undefined);\n    if (!read.ok || !isPersonProfile(profile)) return false;\n    // Global settings own the text of a synced profile, so it is changed there, not here.\n    if (profile.source === 'global') return true;\n    const saved = await request(ME_ROUTE, {\n      method: 'PUT',\n      headers: JSON_HEADERS,\n      body: JSON.stringify({\n        name: name.trim() || profile.name,\n        role: profile.role,\n        objective: profile.objective,\n        aboutMe: interests.trim() || profile.aboutMe,\n        expectedRevision: profile.revision,\n        expectedProfileId: profile.id,\n      }),\n    });\n    return saved.ok;\n  } catch {\n    return false;\n  }\n}\n"],"mappings":";AAKA,MAAM,gBAAgB;AACtB,MAAM,WAAW;AACjB,MAAM,eAAe,EAAE,gBAAgB,mBAAmB;;;;;AAM1D,eAAsB,qBACpB,IACA,SACA,iBACkB;CAClB,IAAI;CACJ,IAAI;EACF,WAAW,MAAM,QAAQ,eAAe;GACtC,QAAQ;GACR,SAAS;GACT,MAAM,KAAK,UAAU;IACnB,OAAO,GAAG;IACV,aAAa,GAAG;IAChB,QAAQ,GAAG;IACX,GAAI,oBAAoB,KAAA,IAAY,CAAC,IAAI,EAAE,gBAAgB;GAC7D,CAAC;EACH,CAAC;CACH,QAAQ;EACN,OAAO;CACT;CACA,IAAI,SAAS,IAAI,OAAO;CACxB,MAAM,OAAgB,MAAM,SAAS,KAAK,CAAC,CAAC,YAAY,KAAA,CAAS;CACjE,MAAM,UAAU,OAAO,SAAS,YAAY,SAAS,QAAQ,WAAW,OAAO,OAAO,KAAK,KAAK,IAAI;CAEpG,OAAO,SAAS,WAAW,OAAO,QAAQ,SAAS,OAAO;AAC5D;AAYA,SAAS,gBAAgB,MAAsC;CAC7D,IAAI,OAAO,SAAS,YAAY,SAAS,MAAM,OAAO;CACtD,MAAM,UAAU;C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