// Mapper de la vue monolithique src/views/Estimation.tsx.
// Le titre WP conserve la convention `*accent*` (la vue reconstruit le <span>
// de l'original via parseAccent).

import { arr } from "../mappers";
import type { BarreReassuranceBlock, ComposantReactBlock, HeroBlock, WpSection } from "../types";
import type { EstimationProps } from "@/views/Estimation";
import { byLayout, nthLayout, stripHtml, u } from "./shared";
import { mapHubspotFormConfig } from "./hubspot";

export function mapEstimationProps(sections: WpSection[]): EstimationProps {
  const hero = nthLayout<HeroBlock>(sections, "HeroLayout");
  const barre = nthLayout<BarreReassuranceBlock>(sections, "BarreReassuranceLayout");
  const formulaire = byLayout<ComposantReactBlock>(sections, "ComposantReactLayout").find(
    (c) => c.composant === "estimation",
  );

  const items = arr(barre?.items)
    .map((item) => stripHtml(item?.texte))
    .filter((t): t is string => Boolean(t));

  return {
    titre: u(hero?.titre),
    intro: stripHtml(hero?.lead),
    hubspotForm: mapHubspotFormConfig(formulaire?.formulaireHubspot, formulaire?.hubspotFormId),
    reassuranceItems: items.length ? items : undefined,
  };
}
