"use client";

import { useRepositories } from "@/di/repository-provider";
import { useApiQuery } from "@/presentation/hooks/use-api-query";
import type { HelpArticleBlockLibrary } from "@/domain/entities/help-article";

/**
 * Layout blocks offered by the article editor, tinted with the tone of the selected category.
 *
 * Refetched whenever the category changes: a step badge in an "Anomalies" article is rose where the
 * same badge is blue under "Véhicules", and the palette lives on the backend so seeded and
 * hand-written articles cannot drift apart.
 */
export function useHelpArticleBlocks(categoryId?: string | number) {
  const { helpArticleRepository } = useRepositories();

  const { data, loading } = useApiQuery<HelpArticleBlockLibrary>(
    () => helpArticleRepository.getBlocks(categoryId ? { help_category_id: categoryId } : undefined),
    { deps: [categoryId] },
  );

  return {
    blocks: data?.blocks ?? [],
    templates: data?.templates ?? [],
    tone: data?.tone,
    loading,
  };
}
