import CollaborateSection from "@/components/CollaborateSection/CollaborateSection";

const parseLogoKeys = (raw: unknown): string[] => {
  if (Array.isArray(raw)) {
    return raw.map(String).map((key) => key.trim()).filter(Boolean);
  }

  if (typeof raw === "string" && raw.trim()) {
    return raw
      .split(/[,;\n]+/)
      .map((key) => key.trim())
      .filter(Boolean);
  }

  return [];
};

/** ACF multi-select field (`logos`) on the logo marquee block. */
export const collectLogoKeys = (props: {
  [rest: string]: unknown;
}): string[] | undefined => {
  const raw = props.logos ?? props.logo_keys ?? props.logoKeys;

  if (raw === undefined || raw === null) {
    return undefined;
  }

  return parseLogoKeys(raw);
};

const LogoMarquee = (props: { [rest: string]: unknown }) => (
  <CollaborateSection
    anchor={props.anchor as string | undefined}
    title={props.title as string | undefined}
    ctaLabel={(props.cta_label ?? props.ctaLabel) as string | undefined}
    ctaHref={(props.cta_href ?? props.ctaHref) as string | undefined}
    titleSize="sm"
    logoKeys={collectLogoKeys(props)}
  />
);

export default LogoMarquee;
