import type { Product } from "@/types/content";

const PRODUCT_ORDER = [
  "interactive-flat-panel",
  "commercial-display",
  "led-videotron",
  "projector",
  "pro-sound",
  "professional-sound",
  "server",
  "network",
  "laptop-pc",
  "pc-laptop",
  "software"
];

export function sortProductsByShowroomOrder<T extends Pick<Product, "slug" | "title">>(items: T[]) {
  return [...items].sort((a, b) => {
    const aIndex = PRODUCT_ORDER.indexOf(a.slug);
    const bIndex = PRODUCT_ORDER.indexOf(b.slug);
    const normalizedA = aIndex === -1 ? Number.MAX_SAFE_INTEGER : aIndex;
    const normalizedB = bIndex === -1 ? Number.MAX_SAFE_INTEGER : bIndex;

    if (normalizedA !== normalizedB) return normalizedA - normalizedB;
    return a.title.en.localeCompare(b.title.en);
  });
}

export function getProductInitial(title: string, index: number) {
  const mapped: Record<string, string> = {
    "Interactive Flat Panel": "IFP",
    "Commercial Display": "CD",
    "LED Videotron": "LED",
    Projector: "PJ",
    "Professional Sound": "PA",
    "Pro Sound": "PA",
    Server: "SV",
    Network: "NET",
    "PC & Laptop": "PC",
    "Laptop & PC": "PC",
    Software: "SW"
  };

  return mapped[title] || String(index + 1).padStart(2, "0");
}
