/** * Staff route only: tab matrix + checkbox state ↔ TAB_* permission names. * Add presentational pieces under ./components/ as the UI grows. */ export const STAFF_FEATURE_GROUPS = [ { labelKey: 'featureToday', read: 'TAB_TODAY_READ', edit: 'TAB_TODAY_EDIT' }, { labelKey: 'featureStaff', read: 'TAB_STAFF_READ', edit: 'TAB_STAFF_EDIT' }, { labelKey: 'featureOrganizations', read: 'TAB_ORGANIZATIONS_READ', edit: 'TAB_ORGANIZATIONS_EDIT' }, { labelKey: 'featurePatients', read: 'TAB_PATIENTS_READ', edit: 'TAB_PATIENTS_EDIT' }, { labelKey: 'featureAppointment', read: 'TAB_APPOINTMENTS_READ', edit: 'TAB_APPOINTMENTS_EDIT' }, { labelKey: 'featureTreatment', read: 'TAB_TREATMENT_READ', edit: 'TAB_TREATMENT_EDIT' }, { labelKey: 'featureBilling', read: 'TAB_BILLING_READ', edit: 'TAB_BILLING_EDIT' }, { labelKey: 'featureReports', read: 'TAB_REPORTS_READ', edit: 'TAB_REPORTS_EDIT' }, ] as const; export type FeaturePermState = Record; export type OrgType = 'CLINIC' | 'LAB' | null | undefined; type StaffFeaturesTranslate = (key: string) => string; export function resolveStaffFeatureLabel( group: (typeof STAFF_FEATURE_GROUPS)[number], organizationType: OrgType, t: StaffFeaturesTranslate, ): string { if (group.read === 'TAB_ORGANIZATIONS_READ') { return organizationType === 'LAB' ? t('featureClinics') : t('featureLabs'); } return t(group.labelKey); } export function emptyFeaturePermissionState(): FeaturePermState { const s: FeaturePermState = {}; for (const g of STAFF_FEATURE_GROUPS) { s[g.edit] = { read: false, edit: false }; } return s; } export function featureStateFromPermissionNames(names: string[]): FeaturePermState { const set = new Set(names); const s = emptyFeaturePermissionState(); for (const g of STAFF_FEATURE_GROUPS) { const hasEdit = set.has(g.edit); const hasRead = set.has(g.read) || hasEdit; s[g.edit] = { read: hasRead, edit: hasEdit }; } return s; } export function permissionNamesFromFeatureState(state: FeaturePermState): string[] { const out: string[] = []; for (const g of STAFF_FEATURE_GROUPS) { const cell = state[g.edit]; if (!cell) continue; if (cell.edit) out.push(g.edit); else if (cell.read) out.push(g.read); } return out; } export function featureStateHasTreatmentEdit(state: FeaturePermState): boolean { return Boolean(state.TAB_TREATMENT_EDIT?.edit); } /** Human-readable access for the team table — feature name, or "Feature (Read only)" */ export function formatAccessSummary( permissionNames: string[] | null | undefined, organizationType: OrgType, t: StaffFeaturesTranslate, ): string { if (!permissionNames?.length) return t('noTabAccess'); const set = new Set(permissionNames); const parts: string[] = []; for (const g of STAFF_FEATURE_GROUPS) { const hasEdit = set.has(g.edit); const hasRead = set.has(g.read) || hasEdit; if (!hasRead) continue; const label = resolveStaffFeatureLabel(g, organizationType, t); parts.push(hasEdit ? label : `${label} ${t('readOnlySuffix')}`); } return parts.length ? parts.join(' · ') : t('noTabAccess'); }