'use client'

import { createContext, useContext } from 'react'

/**
 * Whether the signed-in person may see Billing and the buy-route / buy-permit
 * controls (admin and moderator only — `src/lib/domain/purchasing-access.ts`).
 * The app shell sets it once from the session; every purchase control reads
 * it. The default is `false`, so a control drawn outside the shell stays hidden.
 */
const PurchasingAccessContext = createContext(false)

export function PurchasingAccessProvider({ allowed, children }: { allowed: boolean; children?: React.ReactNode }) {
  return <PurchasingAccessContext.Provider value={allowed}>{children}</PurchasingAccessContext.Provider>
}

export function usePurchasingAllowed(): boolean {
  return useContext(PurchasingAccessContext)
}

/** Draw the children only for a person who may purchase. */
export function PurchasingOnly({ children }: { children?: React.ReactNode }) {
  return usePurchasingAllowed() ? <>{children}</> : null
}
