import { Link } from "react-router";
import { ChevronRight } from "lucide-react";
import { usePlatformLanguage } from "../i18n/LanguageContext";

export default function BreadcrumbBar({ pathname }: {pathname: string;}) {
  const parts = pathname.split("/").filter(Boolean);
  const labels = parts.length ? parts : ["campaigns"];
  const { translateText } = usePlatformLanguage();

  return (
    <div className="breadcrumb-bar">
      <nav className="breadcrumbs" aria-label={translateText("i18n:govoplan-core.breadcrumb.c766e665")}>
        {labels.map((part, index) => {
          const href = `/${labels.slice(0, index + 1).join("/")}`;
          return (
            <span className="crumb" key={`${part}-${index}`}>
              <Link className="crumb-link" to={href}>{translateText(labelFor(part, labels, index))}</Link>
              {index < labels.length - 1 && <ChevronRight size={16} aria-hidden="true" />}
            </span>);

        })}
      </nav>
    </div>);

}

const campaignRouteLabels: Record<string, string> = {
  data: "i18n:govoplan-core.sender_recipients.922c6d24",
  campaign: "i18n:govoplan-core.sender_recipients.922c6d24",
  settings: "i18n:govoplan-core.campaign_settings.efffec26",
  "global-settings": "i18n:govoplan-core.campaign_settings.efffec26",
  fields: "i18n:govoplan-core.fields.e8b68527",
  recipients: "i18n:govoplan-core.sender_recipients.922c6d24",
  "recipient-data": "i18n:govoplan-core.recipient_data.c2baaf10",
  template: "i18n:govoplan-core.template.3ec1ae06",
  files: "i18n:govoplan-core.attachments.6771ade6",
  attachments: "i18n:govoplan-core.attachments.6771ade6",
  mail: "i18n:govoplan-core.server_settings.28af3cc7",
  "mail-settings": "i18n:govoplan-core.server_settings.28af3cc7",
  "server-settings": "i18n:govoplan-core.server_settings.28af3cc7",
  review: "i18n:govoplan-core.review_send.1627617d",
  send: "i18n:govoplan-core.review_send.1627617d",
  report: "i18n:govoplan-core.report.ee45c303",
  reports: "i18n:govoplan-core.report.ee45c303",
  audit: "i18n:govoplan-core.audit_log.3cfc5f1c",
  json: "i18n:govoplan-core.json.031a4e76",
  wizard: "i18n:govoplan-core.wizard.127a661b",
  create: "i18n:govoplan-core.create.6e157c5d"
};

const topLevelRouteLabels: Record<string, string> = {
  campaigns: "i18n:govoplan-core.campaigns.01a23a28",
  dashboard: "i18n:govoplan-core.dashboard.d87f47b4",
  templates: "i18n:govoplan-core.templates.f25b700e",
  files: "i18n:govoplan-core.files.6ce6c512",
  "address-book": "i18n:govoplan-core.address_book.f6327f59",
  reports: "i18n:govoplan-core.reports.88bc3fe3",
  settings: "i18n:govoplan-core.settings.c7f73bb5",
  admin: "i18n:govoplan-core.admin.4e7afebc"
};

function labelFor(value: string, parts: string[], index: number): string {
  if (parts[0] === "campaigns" && index === 1) return "i18n:govoplan-core.campaign.69390e16";
  if (parts[0] === "campaigns" && index >= 2) {
    const mapped = campaignRouteLabels[value];
    if (mapped) return mapped;
  }

  const mapped = topLevelRouteLabels[value];
  if (mapped) return mapped;
  if (value.length > 18) return "i18n:govoplan-core.campaign.69390e16";
  return value.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}
