'use client';

import { ReactNode } from 'react';
import { useCapability } from "@/hooks/useCapability";
import { useInject } from '@/lib/di/hooks/useInject';
import { POLICY_SERVICE_TOKEN } from '@/lib/di/tokens';

type CapabilityGateProps = {
  capabilityKey: string;
  children: ReactNode;
  fallback?: ReactNode;
  comingSoon?: ReactNode;
};

export function CapabilityGate({
  capabilityKey,
  children,
  fallback = null,
  comingSoon = null,
}: CapabilityGateProps) {
  const policyService = useInject(POLICY_SERVICE_TOKEN);
  const { isLoading, isError, data: snapshot } = useCapability(capabilityKey);

  // Use PolicyService to centralize the evaluation logic
  const content = policyService.getCapabilityContent(
    snapshot || null,
    capabilityKey,
    isLoading,
    isError,
    children,
    fallback,
    comingSoon
  );

  return <>{content}</>;
}