{"version":3,"sources":["../src/hooks/useProfileLabels.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'react';\nimport { useAppStore } from '../store/appStore';\nimport { PROFILE_ROLES } from '../types/chat';\nimport {\n  DEFAULT_PROFILE_LABELS,\n  getProfileLabel as resolveProfileLabel,\n  type ProfileLabelsMap,\n  type ProfileLabelsFeatureFlag,\n} from '../types/profileLabels';\nimport type { BaseApiClient } from '../types/api';\n\nexport interface UseProfileLabelsConfig {\n  apiClient: Pick<BaseApiClient, 'get'>;\n}\n\nexport interface UseProfileLabelsReturn {\n  /** Full resolved label map (custom labels merged over the defaults). */\n  labels: Record<PROFILE_ROLES, string>;\n  /**\n   * Only the institution's explicit overrides (the raw feature flag `version`),\n   * with no defaults merged in. Use this when a consumer has its own fallback\n   * label and only wants to replace it when the institution customized it.\n   */\n  customLabels: ProfileLabelsMap;\n  /** Resolve a single role's display label using the resolved map. */\n  getProfileLabel: (role: PROFILE_ROLES | string) => string;\n  loading: boolean;\n}\n\n/**\n * Resolve the institution's custom profile nomenclatura (PROFILE_LABELS feature\n * flag) and expose a label map + resolver. Falls back to DEFAULT_PROFILE_LABELS\n * when the institution has no custom labels, so apps render correctly even\n * without the flag.\n *\n * Mirrors `useSupportFeatureFlag`: `institutionId` comes from `useAppStore` and\n * the HTTP client is injected so each app passes its authenticated instance.\n *\n * @example\n * ```tsx\n * const { getProfileLabel } = useProfileLabels({ apiClient });\n * <span>{getProfileLabel(PROFILE_ROLES.STUDENT)}</span> // \"Estudante\"\n * ```\n */\nexport const useProfileLabels = (\n  config: UseProfileLabelsConfig\n): UseProfileLabelsReturn => {\n  const [customLabels, setCustomLabels] = useState<ProfileLabelsMap>({});\n  const [loading, setLoading] = useState(true);\n  const { institutionId } = useAppStore();\n\n  useEffect(() => {\n    // Clear the previous institution's labels so we never render stale\n    // nomenclatura while the next fetch is in flight (or when it goes null).\n    setCustomLabels({});\n\n    if (!institutionId) {\n      setLoading(false);\n      return;\n    }\n\n    // Guard against out-of-order responses: a slower earlier request must not\n    // overwrite the labels of a newer institutionId.\n    let active = true;\n    setLoading(true);\n\n    const fetchProfileLabels = async () => {\n      try {\n        const { data: response } = await config.apiClient.get<{\n          data: { featureFlags: ProfileLabelsFeatureFlag };\n        }>(`/featureFlags/institution/${institutionId}/page/PROFILE_LABELS`);\n\n        if (!active) return;\n        const version = response?.data?.featureFlags?.version;\n        setCustomLabels(version ?? {});\n      } catch {\n        if (active) setCustomLabels({});\n      } finally {\n        if (active) setLoading(false);\n      }\n    };\n\n    fetchProfileLabels();\n\n    return () => {\n      active = false;\n    };\n  }, [institutionId, config.apiClient]);\n\n  const labels = useMemo(\n    () => ({ ...DEFAULT_PROFILE_LABELS, ...customLabels }),\n    [customLabels]\n  );\n\n  const getProfileLabel = useMemo(\n    () => (role: PROFILE_ROLES | string) => resolveProfileLabel(role, labels),\n    [labels]\n  );\n\n  return { labels, customLabels, getProfileLabel, loading };\n};\n"],"mappings":";;;;;;;;;AAAA,SAAS,WAAW,SAAS,gBAAgB;AA4CtC,IAAM,mBAAmB,CAC9B,WAC2B;AAC3B,QAAM,CAAC,cAAc,eAAe,IAAI,SAA2B,CAAC,CAAC;AACrE,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,IAAI;AAC3C,QAAM,EAAE,cAAc,IAAI,YAAY;AAEtC,YAAU,MAAM;AAGd,oBAAgB,CAAC,CAAC;AAElB,QAAI,CAAC,eAAe;AAClB,iBAAW,KAAK;AAChB;AAAA,IACF;AAIA,QAAI,SAAS;AACb,eAAW,IAAI;AAEf,UAAM,qBAAqB,YAAY;AACrC,UAAI;AACF,cAAM,EAAE,MAAM,SAAS,IAAI,MAAM,OAAO,UAAU,IAE/C,6BAA6B,aAAa,sBAAsB;AAEnE,YAAI,CAAC,OAAQ;AACb,cAAM,UAAU,UAAU,MAAM,cAAc;AAC9C,wBAAgB,WAAW,CAAC,CAAC;AAAA,MAC/B,QAAQ;AACN,YAAI,OAAQ,iBAAgB,CAAC,CAAC;AAAA,MAChC,UAAE;AACA,YAAI,OAAQ,YAAW,KAAK;AAAA,MAC9B;AAAA,IACF;AAEA,uBAAmB;AAEnB,WAAO,MAAM;AACX,eAAS;AAAA,IACX;AAAA,EACF,GAAG,CAAC,eAAe,OAAO,SAAS,CAAC;AAEpC,QAAM,SAAS;AAAA,IACb,OAAO,EAAE,GAAG,wBAAwB,GAAG,aAAa;AAAA,IACpD,CAAC,YAAY;AAAA,EACf;AAEA,QAAMA,mBAAkB;AAAA,IACtB,MAAM,CAAC,SAAiC,gBAAoB,MAAM,MAAM;AAAA,IACxE,CAAC,MAAM;AAAA,EACT;AAEA,SAAO,EAAE,QAAQ,cAAc,iBAAAA,kBAAiB,QAAQ;AAC1D;","names":["getProfileLabel"]}