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CAAiB,QAAQ,KAAK,CAAA;AAAA,IAClC,CAAC,CAAA;AAAA,EACL,CAAA;AACA,EAAA,MAAA,EAAO;AACP,EAAA,MAAM,MAAA,GAAS,SAAS,GAAA,CAAI,CAAC,OAAO,MAAA,CAAO,UAAA,CAAW,KAAA,EAAO,EAAE,CAAC,CAAA;AAChE,EAAA,MAAM,WACJ,OAAO,gBAAA,KAAqB,cACxB,IAAA,GACA,IAAI,iBAAiB,MAAM;AACzB,IAAA,MAAA,EAAO;AACP,IAAA,KAAA,EAAM;AAAA,EACR,CAAC,CAAA;AACP,EAAA,QAAA,EAAU,QAAQ,QAAA,CAAS,IAAA,EAAM,EAAE,SAAA,EAAW,MAAM,CAAA;AACpD,EAAA,OAAO,MAAM;AACX,IAAA,IAAA,GAAO,IAAA;AACP,IAAA,MAAA,CAAO,QAAQ,CAAC,CAAA,KAAM,EAAE,mBAAA,CAAoB,MAAA,EAAQ,KAAK,CAAC,CAAA;AAC1D,IAAA,MAAA,CAAO,QAAQ,CAAC,CAAA,KAAM,MAAA,CAAO,YAAA,CAAa,CAAC,CAAC,CAAA;AAC5C,IAAA,QAAA,EAAU,UAAA,EAAW;AAAA,EACvB,CAAA;AACF;AAGO,SAAS,YAAA,GAAqB;AACnC,EAAAA,eAAAA,CAAU,MAAM,iBAAA,EAAkB,EAAG,EAAE,CAAA;AACvC,EAAA,OAAO,IAAA;AACT","file":"brand-favicon.cjs","sourcesContent":["// Brand assets — logo URLs read at RUNTIME from the Brand token sheet.\n//\n// Brand guideline \"Logo standard: use the uploaded Brand assets as-is\": apps\n// (and the shell) never bundle their own copy of a logo; they read the URLs\n// from the Brand `assets` group. `/api/tokens?format=css` emits each set asset\n// as `--asset-<kebab>: url(\"…\")` on `:root`, and every app already loads that\n// sheet at runtime (see ../runtime-tokens.ts), so the rail logo is resolved\n// from the same live sheet as every other chrome value — a Brand upload swaps\n// the logo in every app without a deploy, and there is nothing to fall back to\n// that could drift.\n\n'use client'\n\nimport { useEffect, useState } from 'react'\n\n/** Brand `assets` keys the chrome renders, by their `--asset-<kebab>` name. */\nexport type BrandAssetKind = 'logo-horizontal' | 'logo-square' | 'favicon' | 'apple-icon' | 'verified-icon'\n\n/** `url(\"https://…\")` / `url(https://…)` → the bare https URL, else null. */\nexport function parseCssUrl(value: string | null | undefined): string | null {\n  if (!value) return null\n  const m = /^url\\(\\s*(['\"]?)(.*?)\\1\\s*\\)$/i.exec(value.trim())\n  const url = m?.[2]?.trim()\n  return url && /^https:\\/\\//i.test(url) ? url : null\n}\n\n/** One Brand asset URL from the live token sheet, or null while it is unset. */\nexport function readBrandAsset(kind: BrandAssetKind): string | null {\n  if (typeof document === 'undefined') return null\n  try {\n    return parseCssUrl(\n      window.getComputedStyle(document.documentElement).getPropertyValue(`--asset-${kind}`),\n    )\n  } catch {\n    return null\n  }\n}\n\nexport type BrandAssets = {\n  /** `assets.logoHorizontal` — the all-white lockup at the top of the rail. */\n  logoHorizontal: string | null\n  /** `assets.logoSquare` — the all-white vrm tile on the collapsed rail. */\n  logoSquare: string | null\n}\n\nconst EMPTY: BrandAssets = { logoHorizontal: null, logoSquare: null }\n\n// The token sheet is a <link> the app appends at startup, so it may still be\n// in flight on first paint. Re-read when it loads, and a few times after, to\n// cover a link that is inserted after this effect ran.\nconst RETRY_MS = [0, 250, 1000, 3000]\n\n/**\n * The Brand logo URLs, resolved after mount (null on the server and until the\n * runtime token sheet has loaded). Callers reserve the logo's box and draw\n * nothing while a value is null — never a stand-in image.\n */\nexport function useBrandAssets(): BrandAssets {\n  const [assets, setAssets] = useState<BrandAssets>(EMPTY)\n  useEffect(() => {\n    let done = false\n    const read = () => {\n      if (done) return\n      const next = {\n        logoHorizontal: readBrandAsset('logo-horizontal'),\n        logoSquare: readBrandAsset('logo-square'),\n      }\n      setAssets((prev) =>\n        prev.logoHorizontal === next.logoHorizontal && prev.logoSquare === next.logoSquare\n          ? prev\n          : next,\n      )\n    }\n    const links = Array.from(\n      document.querySelectorAll<HTMLLinkElement>('link[rel=\"stylesheet\"][href*=\"api/tokens\"]'),\n    )\n    links.forEach((l) => l.addEventListener('load', read))\n    const timers = RETRY_MS.map((ms) => window.setTimeout(read, ms))\n    read()\n    return () => {\n      done = true\n      links.forEach((l) => l.removeEventListener('load', read))\n      timers.forEach((t) => window.clearTimeout(t))\n    }\n  }, [])\n  return assets\n}\n\n/**\n * One Brand asset URL, resolved after mount the same way as the rail logos\n * (null on the server and until the runtime token sheet has loaded). Used by\n * the VRM Verified badge for `assets.verifiedIcon` (`--asset-verified-icon`).\n */\nexport function useBrandAsset(kind: BrandAssetKind): string | null {\n  const [url, setUrl] = useState<string | null>(null)\n  useEffect(() => {\n    let done = false\n    const read = () => {\n      if (done) return\n      const next = readBrandAsset(kind)\n      setUrl((prev) => (prev === next ? prev : next))\n    }\n    const links = Array.from(\n      document.querySelectorAll<HTMLLinkElement>('link[rel=\"stylesheet\"][href*=\"api/tokens\"]'),\n    )\n    links.forEach((l) => l.addEventListener('load', read))\n    const timers = RETRY_MS.map((ms) => window.setTimeout(read, ms))\n    read()\n    return () => {\n      done = true\n      links.forEach((l) => l.removeEventListener('load', read))\n      timers.forEach((t) => window.clearTimeout(t))\n    }\n  }, [kind])\n  return url\n}\n","// Browser-tab icon — Brand `assets.favicon` read at RUNTIME from the Brand\n// token sheet, the same way the rail logo is (./brandAssets).\n//\n// `politogyAppIcons()` (../app-icons) resolves the icon on the SERVER from\n// specs.politogyvrm.com/api/icons/<app>. In production that request comes from\n// a Worker, not a browser, and Cloudflare's bot check answers it with a\n// challenge page, so the metadata comes back with no icon and the tab shows the\n// browser default (or an app's leftover Next.js `favicon.ico` triangle). The\n// token sheet, by contrast, is loaded by the BROWSER in every app, and emits\n// `--asset-favicon` / `--asset-apple-icon` from Brand `assets.*`. Reading those\n// gives every app the Brand's VRM tile with no static file in any repo (Global\n// Rules → App Icons) and no extra request.\n//\n// The link carries a fixed id and is kept the LAST icon link in <head>, so a\n// stale icon from metadata or an old static file never wins over the Brand's.\n\n'use client'\n\nimport { useEffect } from 'react'\n\nimport { readBrandAsset } from './brandAssets'\n\nexport const BRAND_FAVICON_LINK_ID = 'politogy-brand-favicon'\nexport const BRAND_APPLE_ICON_LINK_ID = 'politogy-brand-apple-icon'\n\nfunction upsertLink(id: string, rel: string, href: string): void {\n  let link = document.getElementById(id) as HTMLLinkElement | null\n  if (!link) {\n    link = document.createElement('link')\n    link.id = id\n    link.rel = rel\n  }\n  if (link.getAttribute('href') !== href) link.href = href\n  // Last icon link in <head> wins. Re-appending an already-last node is a no-op\n  // for the browser, so only move it when something was added after it.\n  const icons = document.head.querySelectorAll(`link[rel~=\"${rel.split(' ')[0]}\"]`)\n  if (icons[icons.length - 1] !== link) document.head.appendChild(link)\n}\n\n/**\n * Point the tab icon (and the iOS home-screen icon) at the Brand's uploaded\n * assets. Returns true once the favicon was set; false while the token sheet\n * has not defined `--asset-favicon` yet. Never draws a stand-in.\n */\nexport function applyBrandFavicon(): boolean {\n  if (typeof document === 'undefined') return false\n  const favicon = readBrandAsset('favicon')\n  if (!favicon) return false\n  upsertLink(BRAND_FAVICON_LINK_ID, 'icon', favicon)\n  const apple = readBrandAsset('apple-icon')\n  if (apple) upsertLink(BRAND_APPLE_ICON_LINK_ID, 'apple-touch-icon', apple)\n  return true\n}\n\n// The token sheet is a <link> the app appends at startup, so it may still be in\n// flight on first paint. Re-read when it loads and a few times after.\nconst RETRY_MS = [0, 250, 1000, 3000]\n\n/**\n * Apply the Brand favicon now and keep it applied: on token sheet load, on a\n * few retries, and whenever <head> changes (a client navigation can re-insert\n * metadata icon links after ours). Returns a cleanup function.\n */\nexport function watchBrandFavicon(): () => void {\n  if (typeof document === 'undefined') return () => {}\n  let done = false\n  const apply = () => {\n    if (!done) applyBrandFavicon()\n  }\n  // A token sheet inserted after mount is picked up by the observer below.\n  const sheets = new Set<HTMLLinkElement>()\n  const listen = () => {\n    document\n      .querySelectorAll<HTMLLinkElement>('link[rel=\"stylesheet\"][href*=\"api/tokens\"]')\n      .forEach((l) => {\n        if (sheets.has(l)) return\n        sheets.add(l)\n        l.addEventListener('load', apply)\n      })\n  }\n  listen()\n  const timers = RETRY_MS.map((ms) => window.setTimeout(apply, ms))\n  const observer =\n    typeof MutationObserver === 'undefined'\n      ? null\n      : new MutationObserver(() => {\n          listen()\n          apply()\n        })\n  observer?.observe(document.head, { childList: true })\n  return () => {\n    done = true\n    sheets.forEach((l) => l.removeEventListener('load', apply))\n    timers.forEach((t) => window.clearTimeout(t))\n    observer?.disconnect()\n  }\n}\n\n/** Renders nothing; sets the Brand favicon after mount. PolitogyAppFrame renders it. */\nexport function BrandFavicon(): null {\n  useEffect(() => watchBrandFavicon(), [])\n  return null\n}\n"]}