export type HelpContext = {
  id: string;
  title: string;
  route: string;
};

const campaignSectionContexts: Record<string, Omit<HelpContext, "route">> = {
  data: { id: "campaign.settings", title: "i18n:govoplan-core.campaign_settings.efffec26" },
  campaign: { id: "campaign.settings", title: "i18n:govoplan-core.campaign_settings.efffec26" },
  fields: { id: "campaign.fields", title: "i18n:govoplan-core.campaign_fields.969e7d80" },
  template: { id: "campaign.template", title: "i18n:govoplan-core.template.3ec1ae06" },
  files: { id: "campaign.attachments", title: "i18n:govoplan-core.attachments.6771ade6" },
  attachments: { id: "campaign.attachments", title: "i18n:govoplan-core.attachments.6771ade6" },
  recipients: { id: "campaign.recipients", title: "i18n:govoplan-core.sender_recipients.922c6d24" },
  "recipient-data": { id: "campaign.recipient-data", title: "i18n:govoplan-core.recipient_data.c2baaf10" },
  "mail-settings": { id: "campaign.server-settings", title: "i18n:govoplan-core.server_settings.28af3cc7" },
  "server-settings": { id: "campaign.server-settings", title: "i18n:govoplan-core.server_settings.28af3cc7" },
  mail: { id: "campaign.server-settings", title: "i18n:govoplan-core.server_settings.28af3cc7" },
  "global-settings": { id: "campaign.global-settings", title: "i18n:govoplan-core.policies.8d611849" },
  settings: { id: "campaign.global-settings", title: "i18n:govoplan-core.policies.8d611849" },
  review: { id: "campaign.review-send", title: "i18n:govoplan-core.review_send.1627617d" },
  send: { id: "campaign.review-send", title: "i18n:govoplan-core.review_send.1627617d" },
  report: { id: "campaign.report", title: "i18n:govoplan-core.report.ee45c303" },
  reports: { id: "campaign.report", title: "i18n:govoplan-core.report.ee45c303" },
  audit: { id: "campaign.audit", title: "i18n:govoplan-core.audit_log.3cfc5f1c" },
  json: { id: "campaign.json", title: "i18n:govoplan-core.json.031a4e76" }
};

const topLevelContexts: Record<string, Omit<HelpContext, "route">> = {
  dashboard: { id: "app.dashboard", title: "i18n:govoplan-core.dashboard.d87f47b4" },
  campaigns: { id: "campaigns.list", title: "i18n:govoplan-core.campaigns.01a23a28" },
  templates: { id: "templates.list", title: "i18n:govoplan-core.templates.f25b700e" },
  files: { id: "files.list", title: "i18n:govoplan-core.files.6ce6c512" },
  mail: { id: "mail.list", title: "i18n:govoplan-mail.mail.92379cbb" },
  "address-book": { id: "address-book.list", title: "i18n:govoplan-core.address_book.f6327f59" },
  reports: { id: "reports.list", title: "i18n:govoplan-core.reports.88bc3fe3" },
  settings: { id: "app.settings", title: "i18n:govoplan-core.settings.c7f73bb5" },
  admin: { id: "app.admin", title: "i18n:govoplan-core.admin.4e7afebc" }
};

export function helpContextForPathname(pathname: string, search = ""): HelpContext {
  const route = pathname || "/";
  const segments = route.split("/").filter(Boolean);

  if (segments[0] === "settings") {
    const section = new URLSearchParams(search).get("section") || "";
    if (section === "mail-profiles") {
      return { id: "mail.profiles", title: "i18n:govoplan-core.mail_profiles.8a8018b7", route: `${route}${search}` };
    }
  }

  if (segments[0] === "campaigns" && segments[1]) {
    if (!segments[2]) return { id: "campaign.overview", title: "i18n:govoplan-core.campaign_overview.43c3d159", route };
    if (segments[2] === "wizard") {
      const step = segments[3] || "create";
      return { id: `campaign.wizard.${step}`, title: `${capitalize(step)} wizard`, route };
    }
    const context = campaignSectionContexts[segments[2]];
    if (context) return { ...context, route };
    return { id: "campaign.workspace", title: "i18n:govoplan-core.campaign_workspace.c345580f", route };
  }

  const context = topLevelContexts[segments[0] || "campaigns"];
  if (context) return { ...context, route };
  return { id: "app.general", title: "i18n:govoplan-core.application.b291beb8", route };
}

export function helpQueryForContext(context: HelpContext): string {
  return `context=${encodeURIComponent(context.id)}`;
}

function capitalize(value: string): string {
  return value.replace(/-/g, " ").replace(/\b\w/g, (letter) => letter.toUpperCase());
}
