// Customer branding — the single source for the console's identity surfaces.
//
// This console is deployed per customer, so the name and logo that appear on
// the sign-in screen, in the workspace switcher and on the browser tab come
// from `PARCLE_CONFIG.brand` (mapped to window.PARCLE_BRAND by index.html)
// instead of being hardcoded across files. Delete the `brand` block from
// config.js and every surface falls back to Parcle's own identity.
//
// Scope is deliberately the identity layer only: name, mark, tab title,
// attribution. Colors and fonts are NOT branded here — the console keeps its
// own design tokens (styles.css).

const PARCLE_BRAND_FALLBACK = Object.freeze({
  configured: false,
  name: 'Parcle',
  domain: 'parcle.ai',
  mark: 'parcle',
  poweredBy: null,
});

// Normalized brand for render code. Always returns a complete object, so call
// sites never need null checks.
function parcleBrand() {
  const b = (typeof window !== 'undefined' && window.PARCLE_BRAND) || null;
  if (!b || typeof b !== 'object') return PARCLE_BRAND_FALLBACK;
  return {
    // True only when this deploy actually names a customer — `name` is what
    // switches branding on, so a stray empty `brand: {}` doesn't rename the
    // workspace after the vendor. Surfaces that name the *workspace* (rail,
    // breadcrumb, onboarding) key off this so an unbranded deploy keeps the
    // labels it had before branding existed.
    configured: !!b.name,
    name:   b.name   || PARCLE_BRAND_FALLBACK.name,
    domain: b.domain || PARCLE_BRAND_FALLBACK.domain,
    mark:   b.mark   || PARCLE_BRAND_FALLBACK.mark,
    // Attribution line shown under the customer's name. `true` renders the
    // default wording, a string overrides it, null/absent hides it entirely
    // (which is what Parcle's own deploy wants — no "powered by itself").
    poweredBy: b.poweredBy === true ? 'Powered by Parcle'
             : (typeof b.poweredBy === 'string' && b.poweredBy ? b.poweredBy : null),
  };
}

// Name for "the workspace this console is showing". A branded deploy names it
// after the customer; an unbranded one gets `fallback`, which is whatever that
// surface said before branding existed — so deleting the `brand` block from
// config.js restores the previous UI verbatim instead of relabelling every
// workspace "Parcle".
function parcleWorkspaceName(fallback) {
  const b = parcleBrand();
  return b.configured ? b.name : fallback;
}

// URL-safe form of the workspace name, for the places that need a host or path
// segment rather than a label (the setup guide's workspace URL, the demo
// connector drawer's tenant host). Keys off `configured` for the same reason
// parcleWorkspaceName does: unbranded, the vendor's own name must not stand in
// for the customer's tenant — `parcle.dev/parcle` would read as Parcle being
// the customer.
function parcleWorkspaceSlug() {
  const b = parcleBrand();
  if (!b.configured) return 'workspace';
  return b.name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'workspace';
}

// Mark registry. `chip: true` means the glyph is a thin stroke drawing meant
// to sit inside a filled tile; `chip: false` means the artwork is already a
// solid shape and must sit on the surface directly. `ink` lets a brand pin its
// own mark color when the call site's default would misrepresent it.
const BRAND_MARKS = {
  // Parcle: arc + dot, drawn in the tile's foreground color.
  parcle: {
    chip: true,
    render: ({ size, fg }) => (
      <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
           stroke={fg} strokeWidth="2.5">
        <path d="M4 12a8 8 0 0116 0"/><circle cx="12" cy="12" r="3" fill={fg}/>
      </svg>
    ),
  },
  // Mithra: a solid "M" letterform. It carries no tile of its own, so it must
  // not be dropped into a filled one — a same-colored tile would swallow the
  // glyph. Mithra publishes the mark white-on-dark; ink is pinned to
  // --text-primary so it inverts with the app's theme instead. That is the
  // console's OWN token, not Mithra's palette — near enough to their ironwood /
  // pearl to read right, but branding stops at the identity layer and does not
  // reach styles.css. (The favicon can't read app tokens, so that file hardcodes
  // their real #271606 / #F7F8EC.) The artwork is not square (2400x2200); the
  // default preserveAspectRatio letterboxes it in the square slot rather than
  // stretching it.
  mithra: {
    chip: false,
    ink: 'var(--text-primary)',
    render: ({ size, fg }) => (
      <svg width={size} height={size} viewBox="0 0 2400 2200" fill="none">
        <path fill={fg}
          d="M15.67,2166.82c16.47-70.06,29.86-155.09,40.21-255.11,10.29-99.96,15.45-245.83,15.45-437.55v-763.78c0-158.72-2.6-291.68-7.73-398.9-5.16-107.17-13.92-205.11-26.28-293.77h414.37c26.77,80.4,68.03,152.58,123.69,216.46,55.66,63.92,141.17,148.42,256.65,253.56l98.95,86.59c152.54,136.06,274.68,255.1,366.44,357.15,91.7,102.05,167.46,223.71,227.28,364.88,59.76,141.23,88.61,304.59,86.59,490.12h-256.66c0-179.35-31.45-337.54-94.3-474.66-62.91-137.07-141.23-256.13-235.01-357.15-93.83-100.98-236.55-230.86-428.28-389.63-80.39-65.95-146.39-134.51-197.9-205.63-51.55-71.12-86.59-140.7-105.14-208.73h-21.64l3.09,61.84c12.36,129.88,18.54,295.84,18.54,497.85v763.78c0,301.01,18.56,530.85,55.66,689.57l3.09,18.55H12.58l3.09-15.46ZM552.17,891.28c46.39-60.79,105.62-132.44,177.81-214.92,100.98-115.42,178.82-215.39,233.46-299.94,54.6-84.51,93.26-182.44,115.95-293.76h253.56c24.74,111.32,64.42,208.72,119.06,292.22,54.6,83.49,132.43,183.99,233.46,301.49,74.21,86.59,133.98,159.25,179.35,218.01,45.31,58.75,86.59,124.22,123.69,196.36l-194.81,114.41c-33.01-70.06-71.66-135-115.97-194.81-44.35-59.77-99.48-127.79-165.44-204.09-61.84-70.06-111.31-130.36-148.43-180.9-37.1-50.49-69.09-108.71-95.86-174.71-26.81-65.96-43.29-141.18-49.47-225.74h-21.65c-6.18,84.55-22.71,159.78-49.48,225.74-26.81,66-58.26,124.22-94.3,174.71-36.1,50.54-85.03,110.84-146.88,180.9-68.04,76.29-123.7,144.32-166.99,204.09-43.29,59.82-82.47,124.75-117.51,194.81l-197.9-114.41c39.13-72.14,81.94-138.63,128.32-199.45ZM2050.37,2163.73c35.03-158.72,52.57-388.56,52.57-689.57v-763.78c0-202.02,6.18-367.98,18.54-497.85l3.09-61.84h-18.54c-16.53,61.84-52.08,129.39-106.68,202.54-54.64,73.2-120.12,143.79-196.36,211.82-193.8,171.14-336.58,305.65-428.28,403.54-91.75,97.93-169.59,214.42-233.46,349.42-63.91,135.05-95.86,291.2-95.86,468.48h-256.65c-2.08-185.53,26.77-348.89,86.59-490.12,59.76-141.18,135.52-262.84,227.27-364.88,91.72-102.05,213.85-221.09,366.44-357.15l98.95-86.59c115.42-105.14,201-189.64,256.66-253.56,55.65-63.88,96.86-136.06,123.69-216.46h414.35c-12.36,88.66-21.65,186.6-27.83,293.77-6.18,107.21-9.27,240.18-9.27,398.9v763.78c0,191.71,5.65,337.59,17.01,437.55,11.3,100.02,25.22,185.05,41.75,255.11l3.09,15.46h-340.14l3.09-18.55Z"/>
      </svg>
    ),
  },
};

const _parcleWarnedMarks = {};

// The brand's logo at `size` px.
//   ink — the mark's color (tile fill for chip marks, artwork fill otherwise)
//   on  — the color drawn on top of the tile; unused by non-chip marks
// A brand-declared `ink` wins, so a solid mark keeps its own color wherever it
// is placed.
function BrandMark({ size = 24, radius = 6, ink = 'var(--text-primary)', on = 'var(--bg)', style }) {
  const key = parcleBrand().mark;
  // A typo'd `mark` would otherwise silently ship the wrong logo — warn once so
  // it surfaces in the console instead of on the customer's screen.
  if (!BRAND_MARKS[key] && !_parcleWarnedMarks[key]) {
    _parcleWarnedMarks[key] = true;
    console.warn(`[brand] unknown mark "${key}" — falling back to the Parcle mark. Known marks: ${Object.keys(BRAND_MARKS).join(', ')}`);
  }
  const mark = BRAND_MARKS[key] || BRAND_MARKS.parcle;
  const color = mark.ink || ink;
  if (!mark.chip) {
    return (
      <div style={{
        width: size, height: size, flexShrink: 0,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        ...style,
      }}>
        {mark.render({ size, fg: color })}
      </div>
    );
  }
  return (
    <div style={{
      width: size, height: size, borderRadius: radius,
      background: color, color: on,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      flexShrink: 0,
      ...style,
    }}>
      {mark.render({ size: Math.round(size / 2), fg: on })}
    </div>
  );
}

Object.assign(window, { parcleBrand, parcleWorkspaceName, parcleWorkspaceSlug, BrandMark, BRAND_MARKS });
