/**
 * Shared hook for loading filter options used across dashboard widgets.
 * Loads only the requested lookup sets and exposes them as filter options.
 */
"use client";

import { useMemo } from "react";
import { useReferenceData, type ReferenceDataSelection } from "~/hooks/useReferenceData.js";

export interface FilterOption {
  value: string;
  label: string;
}

export function useWidgetFilterOptions(selection: ReferenceDataSelection = {}) {
  const { clients: clientRows, countries: countryRows, roles: roleRows, chapters: chapterRows } =
    useReferenceData(selection);

  const clients = useMemo<FilterOption[]>(() => {
    return clientRows.map((client) => ({ value: client.id, label: client.name }));
  }, [clientRows]);

  const countries = useMemo<FilterOption[]>(() => {
    return countryRows.map((country) => ({ value: country.id, label: country.name }));
  }, [countryRows]);

  const roles = useMemo<FilterOption[]>(() => {
    return roleRows.map((role) => ({ value: role.id, label: role.name }));
  }, [roleRows]);

  const chapters = useMemo<FilterOption[]>(() => {
    return chapterRows.map((chapter) => ({ value: chapter, label: chapter }));
  }, [chapterRows]);

  return { clients, countries, roles, chapters };
}
