import { useMemo } from 'react';

export interface UseFiltersLayoutProps {
  totalFilters?: number;
  inline?: number;
  maxInlineFilters?: number;
  maxInlineLabels?: number;
  customInlineElements?: number;
  filters?: number;
  useNewFilters?: true;
  forceShowSidePanel?: boolean;
}

export function useFiltersLayout({
  totalFilters = 0,
  filters = 0,
  inline,
  maxInlineFilters = 3,
  maxInlineLabels = 2,
  customInlineElements = 0,
  useNewFilters,
  forceShowSidePanel = false,
}: UseFiltersLayoutProps) {
  return useMemo(() => {
    const totalInline =
      inline != null
        ? inline
        : totalFilters <= maxInlineFilters
        ? totalFilters
        : 0;

    let showFiltersPanelButton =
      forceShowSidePanel || totalFilters - totalInline > 0;
    let totalInlineFilters = forceShowSidePanel
      ? 0
      : Math.max(totalInline - customInlineElements, 0);

    const totalInlineButtons = totalInline + (showFiltersPanelButton ? 1 : 0);
    const showLabels = totalInlineButtons <= maxInlineLabels;

    if (useNewFilters) {
      showFiltersPanelButton =
        forceShowSidePanel || customInlineElements > 0 || filters > 2;
      totalInlineFilters = showFiltersPanelButton ? 0 : totalInlineFilters;
    }

    return {
      showFiltersPanelButton,
      showLabels,
      totalInlineFilters,
    };
  }, [
    totalFilters,
    inline,
    maxInlineFilters,
    maxInlineLabels,
    filters,
    useNewFilters,
  ]);
}
