"use client";

import type { InternetPlanCatalogItem } from "@customer-portal/domain/catalog";

type ParsedPlanName = {
  baseName: string;
  detail: string | null;
};

/**
 * Splits a plan name into a primary label and secondary detail.
 * Many SKUs include the housing type in parentheses, e.g.:
 *   "Internet Gold Plan (Home 1G)"
 * This helper lets the UI present those parts separately so the card layout
 * can avoid awkward line breaks while still surfacing the extra context.
 */
export function parsePlanName(
  plan: Pick<InternetPlanCatalogItem, "name" | "internetOfferingType">
): ParsedPlanName {
  const rawName = (plan.name ?? "").trim();
  if (!rawName) {
    return { baseName: "Internet Plan", detail: plan.internetOfferingType ?? null };
  }

  const match = rawName.match(/^(.*?)(?:\s*\((.+)\))?$/);
  const baseName = match?.[1]?.trim() || rawName;
  const bracketDetail = match?.[2]?.trim() || "";

  const offeringDetail = (plan.internetOfferingType ?? "").trim();
  const detail = bracketDetail || offeringDetail || null;

  return { baseName, detail };
}

export type { ParsedPlanName };
