import { useLicenseContext } from "../contexts/LicenseContext";
import { isEntitled, type FeatureKey } from "../shared/entitlements";

/**
 * Synchronous feature-gating hook.
 *
 * Returns `{ allowed, ready }` (NOT a bare boolean) so consumers can suppress
 * the lock/upsell while the license is still loading or errored (`ready` is
 * false) instead of flashing "locked" to a paying user. `allowed` is fail-closed
 * in Free and during boot (edition defaults to "free"), so it is safe to read
 * even before `ready`.
 */
export function useEntitlement(feature: FeatureKey): { allowed: boolean; ready: boolean } {
  const { status, edition, features } = useLicenseContext();
  return {
    allowed: isEntitled(feature, edition, features),
    ready: status === "ready",
  };
}
