/**
 * React hooks for the consent SDK.
 *
 * Phase 4: extracted from index.tsx to keep the main file under 500 LOC.
 */

import { useContext } from 'react';
import type { ConsentCategory } from '../types';
import type { ConsentManager } from '../core/ConsentManager';
import { ConsentContext, type ConsentContextValue } from './context';

/**
 * useConsent - Consent-Hook.
 * Overloads: call without args for the full context; pass a category to also get `allowed`.
 */
export function useConsent(): ConsentContextValue;
export function useConsent(
  category: ConsentCategory
): ConsentContextValue & { allowed: boolean };
export function useConsent(category?: ConsentCategory) {
  const context = useContext(ConsentContext);

  if (!context) {
    throw new Error('useConsent must be used within a ConsentProvider');
  }

  if (category) {
    return {
      ...context,
      allowed: context.hasConsent(category),
    };
  }

  return context;
}

/**
 * useConsentManager - Direkter Zugriff auf ConsentManager.
 */
export function useConsentManager(): ConsentManager | null {
  const context = useContext(ConsentContext);
  return context?.manager ?? null;
}
