/* Shared chrome: brand registry, site header for the top-nav variant. Exposes window.EH. */
(function () {
  const S = window.EH_SVG || {};
  const Svg = ({ name, className, style }) => (
    <span className={className} style={style} aria-hidden="true" dangerouslySetInnerHTML={{ __html: S[name] || '' }} />
  );

  /* The two Castle Connolly directory sites, castleconnolly.com and everydayhealth.care, from one source. Tokens live in CSS under html[data-brand]; this holds names, logos, nav and copy only. */
  const BRANDS = {
    cc: {
      id: 'cc',
      name: 'Castle Connolly',
      shortName: 'Castle Connolly',
      site: 'castleconnolly.com',
      siteUrl: 'https://www.castleconnolly.com/',
      logo: '/assets/cc-logo.svg',
      logoAlt: 'Castle Connolly Top Doctors',
      tagline: 'Search Top Providers. Find Trusted Care.',
      sub: 'Ask in plain language or browse the directory. Every answer is grounded in Castle Connolly\u2019s verified Top Doctor data.',
      headerLink: { label: 'For Doctors', href: 'https://www.castleconnolly.com/for-doctors', inert: true },
      nav: [
        { label: 'Doctors', href: 'https://www.castleconnolly.com/doctors' },
        { label: 'Dentists', href: 'https://www.castleconnolly.com/dentists' },
        { label: 'Hospitals', href: 'https://www.castleconnolly.com/hospitals' },
        { label: 'Centers of Excellence', href: 'https://www.castleconnolly.com/centers-of-excellence', inert: true },
        { label: 'Topics', href: 'https://www.castleconnolly.com/topics' },
        { label: 'About Us', href: 'https://www.castleconnolly.com/about-us' },
      ],
      sourceLabel: 'Castle Connolly',
    },
    ehcare: {
      id: 'ehcare',
      name: 'Everyday Health-CARE',
      shortName: 'Everyday Health-CARE',
      site: 'everydayhealth.care',
      siteUrl: 'https://www.everydayhealth.care/',
      logo: '/assets/ehcare-logo.svg',
      logoAlt: 'Everyday Health-CARE',
      tagline: 'Access the care you need, and the care you want',
      sub: 'Ask in plain language or browse the directory. Every answer is grounded in verified provider data.',
      headerLink: { label: 'For Providers', href: 'https://www.everydayhealth.care/', inert: true },
      nav: [
        { label: 'Specialists', href: 'https://www.everydayhealth.care/' },
        { label: 'Top Doctors', href: 'https://www.everydayhealth.care/' },
        { label: 'Conditions', href: 'https://www.everydayhealth.care/' },
        { label: 'Procedures', href: 'https://www.everydayhealth.care/' },
        { label: 'Locations', href: 'https://www.everydayhealth.care/' },
        { label: 'Insurances', href: 'https://www.everydayhealth.care/' },
      ],
      sourceLabel: 'Everyday Health-CARE',
    },
  };
  const brandOf = () => (new URLSearchParams(location.search).get('brand') === 'ehcare' ? 'ehcare' : 'cc');
  const applyBrand = (id) => { document.documentElement.setAttribute('data-brand', id); document.title = BRANDS[id].name + ' — Search'; };

  const Logo = ({ brand, className }) => (
    <img className={className} src={brand.logo} alt={brand.logoAlt} draggable="false" />
  );

  /* Top-nav variant: castleconnolly.com's header shape. One white row: logo, uppercase nav with chevrons, For Doctors, account, green search square flush right. */
  function Header({ brand, onSearch, homeHref, right }) {
    const b = brand || BRANDS.cc;
    return (
      <header className="cc-header">
        <div className="cc-header__bar">
          <a className="cc-header__logo" href={homeHref || '/'} title={b.name}><Logo brand={b} /></a>
          <nav className="cc-header__nav" aria-label="Primary">
            <ul className="cc-header__nav-items">
              {b.nav.map((n) => (
                <li key={n.label}>{n.inert
                  ? <span className="cc-header__nav-link is-inert" aria-disabled="true"><span>{n.label}</span><Svg name="chevron" /></span>
                  : <a className="cc-header__nav-link" href={n.href} target="_blank" rel="noreferrer"><span>{n.label}</span><Svg name="chevron" /></a>}</li>
              ))}
              <li>{b.headerLink.inert
                ? <span className="cc-header__nav-link is-inert" aria-disabled="true"><span>{b.headerLink.label}</span></span>
                : <a className="cc-header__nav-link" href={b.headerLink.href} target="_blank" rel="noreferrer"><span>{b.headerLink.label}</span></a>}</li>
            </ul>
          </nav>
          <div className="cc-header__right">
            {right}
            <button className="cc-header__search" type="button" onClick={onSearch} aria-label="Search"><Svg name="search" /></button>
          </div>
        </div>
      </header>
    );
  }

  window.EH = { Svg, Header, Logo, BRANDS, brandOf, applyBrand };
})();
