import { createContext, useContext } from 'react'; import { RequestContext, createRequestContext, getActiveGlobalTenantId, setActiveGlobalTenantId, } from '../../services/contracts/context'; export interface TenantInfo { id: string; name: string; description?: string; } export const PRESET_TENANTS: TenantInfo[] = [ { id: 'tenant_main', name: 'Основной бизнес (Main)', description: 'Главный тенант компании' }, { id: 'tenant_alpha', name: 'Филиал «Север» (Alpha)', description: 'Северное подразделение' }, { id: 'tenant_beta', name: 'Филиал «Юг» (Beta)', description: 'Южное подразделение' }, { id: 'tenant_demo', name: 'Песочница (Demo/Sandbox)', description: 'Тестовая изолированная среда' }, ]; export interface TenantContextValue { currentTenantId: string; currentTenant: TenantInfo; availableTenants: TenantInfo[]; requestContext: RequestContext; switchTenant: (tenantId: string) => void; createContextForCurrentTenant: (overrides?: Partial) => RequestContext; } export const TenantContext = createContext(null); export function useTenant(): TenantContextValue { const ctx = useContext(TenantContext); if (!ctx) { const fallbackTenantId = getActiveGlobalTenantId(); const currentTenant = PRESET_TENANTS.find((t) => t.id === fallbackTenantId) || { id: fallbackTenantId, name: fallbackTenantId, }; return { currentTenantId: fallbackTenantId, currentTenant, availableTenants: PRESET_TENANTS, requestContext: createRequestContext({ tenantId: fallbackTenantId }), switchTenant: setActiveGlobalTenantId, createContextForCurrentTenant: (overrides) => createRequestContext({ tenantId: fallbackTenantId, ...overrides }), }; } return ctx; }