export const WORKSPACE_ROLES = {
  superAdmin: 'Super Administrator',
  administrator: 'Administrator',
  directorGeneral: 'Director General',
  financeDirector: 'Finance Director',
  procurementLead: 'Procurement Lead',
  complianceAuditLead: 'Compliance and Audit Lead',
  projectDeliveryLead: 'Project Delivery Lead',
  public: 'Public',
} as const;

export const WORKSPACE_ROUTES = {
  [WORKSPACE_ROLES.superAdmin]: '/platform-command',
  [WORKSPACE_ROLES.administrator]: '/operations-command',
  [WORKSPACE_ROLES.directorGeneral]: '/executive-command',
  [WORKSPACE_ROLES.financeDirector]: '/financial-control',
  [WORKSPACE_ROLES.procurementLead]: '/procurement-desk',
  [WORKSPACE_ROLES.complianceAuditLead]: '/compliance-desk',
  [WORKSPACE_ROLES.projectDeliveryLead]: '/delivery-command',
} as const;

const ADMIN_DASHBOARD_ROLES = [WORKSPACE_ROLES.superAdmin, WORKSPACE_ROLES.administrator] as const;

export type WorkspaceMetricKey =
  | 'approvedBudget'
  | 'committedBudget'
  | 'disbursedBudget'
  | 'pendingApprovals'
  | 'contractsNearingExpiry'
  | 'vendorComplianceAlerts'
  | 'procurementPipeline'
  | 'openRiskAlerts'
  | 'averageProjectProgress'
  | 'highRiskProjects'
  | 'overduePayments'
  | 'activeProjects'
  | 'unreadNotifications';

export type WorkspaceDetailBlockKey =
  | 'focus'
  | 'summary'
  | 'watchlist'
  | 'approvalRisk'
  | 'operations'
  | 'delivery'
  | 'actions';

export type WorkspaceSectionKey =
  | 'approvalQueue'
  | 'riskPanel'
  | 'procurementQueue'
  | 'contractWatchlist'
  | 'recentNotifications'
  | 'provinceRollout'
  | 'topContracts'
  | 'quickActions';

export type WorkspaceQuickLinkIconKey =
  | 'organizations'
  | 'users'
  | 'approvals'
  | 'notifications'
  | 'projects'
  | 'contracts'
  | 'payments'
  | 'allocations'
  | 'requisitions'
  | 'tenders'
  | 'compliance'
  | 'audit'
  | 'milestones'
  | 'vendors';

export interface WorkspaceAction {
  href: string;
  label: string;
}

export interface WorkspaceQuickLink {
  href: string;
  label: string;
  description: string;
  icon: WorkspaceQuickLinkIconKey;
}

export interface WorkspaceSectionCopy {
  eyebrow: string;
  title: string;
  actionLabel?: string;
}

export interface WorkspaceConfig {
  sidebarLabel: string;
  pageTitle: string;
  eyebrow: string;
  heroTitle: string;
  heroDescription: string;
  primaryAction: WorkspaceAction;
  secondaryAction: WorkspaceAction;
  highlightedMetricKeys: WorkspaceMetricKey[];
  heroMetricKeys: WorkspaceMetricKey[];
  blockOrder: WorkspaceDetailBlockKey[];
  sectionCopy: Record<WorkspaceSectionKey, WorkspaceSectionCopy>;
  quickLinks: WorkspaceQuickLink[];
}

type WorkspaceConfigInput = Omit<WorkspaceConfig, 'sectionCopy' | 'heroMetricKeys' | 'blockOrder' | 'quickLinks'> & {
  heroMetricKeys?: WorkspaceMetricKey[];
  blockOrder?: WorkspaceDetailBlockKey[];
  sectionCopy?: Partial<Record<WorkspaceSectionKey, WorkspaceSectionCopy>>;
  quickLinks?: WorkspaceQuickLink[];
};

const defaultBlockOrder: WorkspaceDetailBlockKey[] = [
  'focus',
  'summary',
  'watchlist',
  'approvalRisk',
  'operations',
  'delivery',
  'actions',
];

const defaultSectionCopy: Record<WorkspaceSectionKey, WorkspaceSectionCopy> = {
  approvalQueue: {
    eyebrow: 'Approval inbox',
    title: 'Pending institutional decisions',
    actionLabel: 'Open approvals',
  },
  riskPanel: {
    eyebrow: 'Risk and red-flag panel',
    title: 'Compliance, audit, and payment exposure',
  },
  procurementQueue: {
    eyebrow: 'Procurement workflow',
    title: 'Live procurement pipeline',
    actionLabel: 'All requisitions',
  },
  contractWatchlist: {
    eyebrow: 'Contract watchlist',
    title: 'Expiring commitments',
    actionLabel: 'Contract register',
  },
  recentNotifications: {
    eyebrow: 'Notifications',
    title: 'Recent operational signals',
    actionLabel: 'Notification center',
  },
  provinceRollout: {
    eyebrow: 'Geographic rollout',
    title: 'Projects by province',
    actionLabel: 'Projects register',
  },
  topContracts: {
    eyebrow: 'Top-value contracts',
    title: 'Largest commitments currently tracked',
    actionLabel: 'Vendor master',
  },
  quickActions: {
    eyebrow: 'Action launcher',
    title: 'Move from oversight to execution',
  },
};

const defaultQuickLinks: WorkspaceQuickLink[] = [
  {
    href: '/requisitions/requisitions-new',
    label: 'Create requisition',
    description: 'Start the procurement workflow.',
    icon: 'requisitions',
  },
  {
    href: '/approvals/approvals-list',
    label: 'Approval inbox',
    description: 'Review pending decisions and escalations.',
    icon: 'approvals',
  },
  {
    href: '/contracts/contracts-list',
    label: 'Contract register',
    description: 'Inspect value, milestones, and expiry status.',
    icon: 'contracts',
  },
  {
    href: '/audit_logs/audit_logs-list',
    label: 'Audit center',
    description: 'Trace actions, timestamps, and record history.',
    icon: 'audit',
  },
];

const createWorkspaceConfig = ({
  heroMetricKeys,
  blockOrder,
  sectionCopy,
  quickLinks,
  ...config
}: WorkspaceConfigInput): WorkspaceConfig => ({
  ...config,
  heroMetricKeys: heroMetricKeys || config.highlightedMetricKeys.slice(0, 4),
  blockOrder: blockOrder || defaultBlockOrder,
  sectionCopy: {
    ...defaultSectionCopy,
    ...sectionCopy,
  },
  quickLinks: quickLinks || defaultQuickLinks,
});

const workspaceConfigs: Record<string, WorkspaceConfig> = {
  [WORKSPACE_ROLES.superAdmin]: createWorkspaceConfig({
    sidebarLabel: 'Platform Command',
    pageTitle: 'Platform Command Workspace',
    eyebrow: 'FDSU ERP · Tenant governance workspace',
    heroTitle: 'Govern tenants, access policy, institutional activity, and platform-level risk from one command surface.',
    heroDescription:
      'This workspace is for platform stewardship: organization oversight, role and permission control, auditability, and cross-tenant signals while preserving drill-down access into the underlying ERP records used by operating teams.',
    primaryAction: { href: '/organizations/organizations-list', label: 'Review tenant registry' },
    secondaryAction: { href: '/users/users-list', label: 'Open access control' },
    highlightedMetricKeys: ['activeProjects', 'pendingApprovals', 'openRiskAlerts', 'contractsNearingExpiry', 'unreadNotifications', 'vendorComplianceAlerts'],
    heroMetricKeys: ['activeProjects', 'pendingApprovals', 'openRiskAlerts', 'unreadNotifications'],
    blockOrder: ['focus', 'watchlist', 'summary', 'approvalRisk', 'operations', 'delivery', 'actions'],
    sectionCopy: {
      approvalQueue: {
        eyebrow: 'Governance queue',
        title: 'Escalations and approvals requiring governance attention',
        actionLabel: 'Open governance queue',
      },
      riskPanel: {
        eyebrow: 'Cross-cutting control flags',
        title: 'Platform-wide exceptions, permission exposure, and institutional control risk',
      },
      recentNotifications: {
        eyebrow: 'Cross-tenant activity',
        title: 'Recent workflow and control signals across organizations',
        actionLabel: 'Open activity feed',
      },
      quickActions: {
        eyebrow: 'Platform actions',
        title: 'Jump directly into tenant oversight and permission-control work',
      },
    },
    quickLinks: [
      {
        href: '/organizations/organizations-list',
        label: 'Organization registry',
        description: 'Review tenant setup, coverage, and stewardship ownership.',
        icon: 'organizations',
      },
      {
        href: '/users/users-list',
        label: 'User access',
        description: 'Manage accounts, authority assignments, and platform ownership.',
        icon: 'users',
      },
      {
        href: '/roles/roles-list',
        label: 'Role catalog',
        description: 'Govern platform-wide role definitions and responsibility boundaries.',
        icon: 'users',
      },
      {
        href: '/permissions/permissions-list',
        label: 'Permission matrix',
        description: 'Inspect and tighten permission coverage across the platform.',
        icon: 'audit',
      },
    ],
  }),
  [WORKSPACE_ROLES.administrator]: createWorkspaceConfig({
    sidebarLabel: 'Operations Command',
    pageTitle: 'Operations Command Workspace',
    eyebrow: 'FDSU ERP · Workflow operations workspace',
    heroTitle: 'Run workflow operations, user support, record readiness, and day-to-day administrative follow-through.',
    heroDescription:
      'This workspace is for organization-level operations administration: grouped workflow controls, notices, master-data upkeep, and fast drill-down into procurement, delivery, finance, and records without forcing teams to scan a single dense sidebar.',
    primaryAction: { href: '/approval_workflows/approval_workflows-list', label: 'Open workflow hub' },
    secondaryAction: { href: '/notifications/notifications-list', label: 'Open notice center' },
    highlightedMetricKeys: ['pendingApprovals', 'unreadNotifications', 'contractsNearingExpiry', 'openRiskAlerts', 'activeProjects', 'procurementPipeline'],
    heroMetricKeys: ['pendingApprovals', 'unreadNotifications', 'procurementPipeline', 'contractsNearingExpiry'],
    blockOrder: ['focus', 'summary', 'approvalRisk', 'watchlist', 'operations', 'delivery', 'actions'],
    sectionCopy: {
      approvalQueue: {
        eyebrow: 'Operations queue',
        title: 'Approvals waiting on routing, delegation, or admin follow-through',
        actionLabel: 'Open workflow queue',
      },
      procurementQueue: {
        eyebrow: 'Operational throughput',
        title: 'Requisitions that may require workflow or setup unblock',
        actionLabel: 'Open requisitions',
      },
      recentNotifications: {
        eyebrow: 'Operational notices',
        title: 'Recent admin-facing signals requiring follow-through',
        actionLabel: 'Open notice center',
      },
      quickActions: {
        eyebrow: 'Administrative actions',
        title: 'Open grouped control areas without scanning the full ERP register',
      },
    },
    quickLinks: [
      {
        href: '/approval_workflows/approval_workflows-list',
        label: 'Workflow hub',
        description: 'Keep approval routes, steps, and queues ready for daily use.',
        icon: 'approvals',
      },
      {
        href: '/notifications/notifications-list',
        label: 'Notice center',
        description: 'Triage notifications, documents, and control signals in one stop.',
        icon: 'notifications',
      },
      {
        href: '/users/users-list',
        label: 'Admin records',
        description: 'Jump into users, departments, provinces, and support clean-up.',
        icon: 'users',
      },
      {
        href: '/projects/projects-list',
        label: 'Delivery register',
        description: 'Move from grouped navigation into active projects and contract follow-through.',
        icon: 'projects',
      },
    ],
  }),
  [WORKSPACE_ROLES.directorGeneral]: createWorkspaceConfig({
    sidebarLabel: 'Executive Command',
    pageTitle: 'Director General Workspace',
    eyebrow: 'FDSU ERP · Director General workspace',
    heroTitle: 'Track strategic budget use, delivery momentum, risk concentration, and contract exposure across the institution.',
    heroDescription:
      'This workspace emphasizes executive oversight over budgets, projects, approvals, and risk hot spots while staying connected to the underlying contracts, requisitions, and implementation records.',
    primaryAction: { href: '/projects/projects-list', label: 'Review projects' },
    secondaryAction: { href: '/contracts/contracts-list', label: 'Review contracts' },
    highlightedMetricKeys: ['approvedBudget', 'committedBudget', 'disbursedBudget', 'pendingApprovals', 'highRiskProjects', 'contractsNearingExpiry'],
    heroMetricKeys: ['approvedBudget', 'disbursedBudget', 'pendingApprovals', 'highRiskProjects'],
    blockOrder: ['summary', 'focus', 'watchlist', 'delivery', 'approvalRisk', 'operations', 'actions'],
    sectionCopy: {
      riskPanel: {
        eyebrow: 'Executive risk watch',
        title: 'Strategic risks, bottlenecks, and control exposure',
      },
      provinceRollout: {
        eyebrow: 'Delivery coverage',
        title: 'Provincial rollout and execution pace',
        actionLabel: 'Open projects',
      },
      topContracts: {
        eyebrow: 'Contract exposure',
        title: 'Largest commitments shaping institutional delivery',
        actionLabel: 'Vendor and contract view',
      },
      quickActions: {
        eyebrow: 'Executive actions',
        title: 'Jump into the records most relevant for strategic follow-up',
      },
    },
    quickLinks: [
      {
        href: '/projects/projects-list',
        label: 'Project portfolio',
        description: 'Review delivery status and ownership.',
        icon: 'projects',
      },
      {
        href: '/contracts/contracts-list',
        label: 'Contract exposure',
        description: 'Inspect major commitments and expiry dates.',
        icon: 'contracts',
      },
      {
        href: '/approvals/approvals-list',
        label: 'Decision queue',
        description: 'Track items waiting for institutional action.',
        icon: 'approvals',
      },
      {
        href: '/notifications/notifications-list',
        label: 'Executive signals',
        description: 'Review the latest alerts and escalations.',
        icon: 'notifications',
      },
    ],
  }),
  [WORKSPACE_ROLES.financeDirector]: createWorkspaceConfig({
    sidebarLabel: 'Financial Control',
    pageTitle: 'Finance Director Workspace',
    eyebrow: 'FDSU ERP · Finance Director workspace',
    heroTitle: 'Monitor allocations, commitments, invoices, payments, and approval pressure with finance-first operational context.',
    heroDescription:
      'This workspace focuses on fiscal control across allocations, commitments, disbursements, overdue payment requests, and approvals while remaining linked to the procurement and project records driving spend.',
    primaryAction: { href: '/payment_requests/payment_requests-list', label: 'Review payment requests' },
    secondaryAction: { href: '/allocations/allocations-list', label: 'Review allocations' },
    highlightedMetricKeys: ['approvedBudget', 'committedBudget', 'disbursedBudget', 'overduePayments', 'pendingApprovals', 'unreadNotifications'],
    heroMetricKeys: ['approvedBudget', 'committedBudget', 'disbursedBudget', 'overduePayments'],
    blockOrder: ['summary', 'watchlist', 'focus', 'approvalRisk', 'delivery', 'operations', 'actions'],
    sectionCopy: {
      approvalQueue: {
        eyebrow: 'Finance queue',
        title: 'Approvals affecting commitments and disbursements',
        actionLabel: 'Open finance approvals',
      },
      riskPanel: {
        eyebrow: 'Fiscal control panel',
        title: 'Budget pressure, overdue payments, and control exceptions',
      },
      topContracts: {
        eyebrow: 'High-value commitments',
        title: 'Largest contracts influencing current financial exposure',
        actionLabel: 'Open vendor master',
      },
      quickActions: {
        eyebrow: 'Finance actions',
        title: 'Move from budget control into payment and allocation follow-up',
      },
    },
    quickLinks: [
      {
        href: '/payment_requests/payment_requests-list',
        label: 'Payment requests',
        description: 'Review overdue and pending disbursements.',
        icon: 'payments',
      },
      {
        href: '/allocations/allocations-list',
        label: 'Allocations',
        description: 'Check available headroom and funding coverage.',
        icon: 'allocations',
      },
      {
        href: '/contracts/contracts-list',
        label: 'Commitment register',
        description: 'Inspect the contracts driving spend.',
        icon: 'contracts',
      },
      {
        href: '/approvals/approvals-list',
        label: 'Approval pressure',
        description: 'Track bottlenecks holding back finance action.',
        icon: 'approvals',
      },
    ],
  }),
  [WORKSPACE_ROLES.procurementLead]: createWorkspaceConfig({
    sidebarLabel: 'Procurement Desk',
    pageTitle: 'Procurement Lead Workspace',
    eyebrow: 'FDSU ERP · Procurement Lead workspace',
    heroTitle: 'Stay on top of requisitions, tender flow, contract deadlines, and approval backlog across the procurement chain.',
    heroDescription:
      'This workspace keeps procurement leadership focused on live demand, tender progression, expiring contracts, and workflow bottlenecks while preserving drill-down into vendors, awards, and originating requisitions.',
    primaryAction: { href: '/requisitions/requisitions-list', label: 'Review requisitions' },
    secondaryAction: { href: '/tenders/tenders-list', label: 'Open tender pipeline' },
    highlightedMetricKeys: ['procurementPipeline', 'pendingApprovals', 'contractsNearingExpiry', 'vendorComplianceAlerts', 'openRiskAlerts', 'activeProjects'],
    heroMetricKeys: ['procurementPipeline', 'pendingApprovals', 'contractsNearingExpiry', 'vendorComplianceAlerts'],
    blockOrder: ['focus', 'summary', 'operations', 'watchlist', 'approvalRisk', 'delivery', 'actions'],
    sectionCopy: {
      approvalQueue: {
        eyebrow: 'Procurement approvals',
        title: 'Decisions blocking sourcing and award progress',
        actionLabel: 'Open approval backlog',
      },
      procurementQueue: {
        eyebrow: 'Sourcing pipeline',
        title: 'Requisitions and tenders requiring procurement action',
        actionLabel: 'Open sourcing queue',
      },
      contractWatchlist: {
        eyebrow: 'Contract renewals',
        title: 'Supplier commitments nearing expiry or follow-up',
        actionLabel: 'Open contracts',
      },
      quickActions: {
        eyebrow: 'Procurement actions',
        title: 'Jump into the next sourcing and contract-management tasks',
      },
    },
    quickLinks: [
      {
        href: '/requisitions/requisitions-list',
        label: 'Requisition queue',
        description: 'Review demand and readiness to source.',
        icon: 'requisitions',
      },
      {
        href: '/tenders/tenders-list',
        label: 'Tender pipeline',
        description: 'Follow active tenders and consultations.',
        icon: 'tenders',
      },
      {
        href: '/contracts/contracts-list',
        label: 'Contract deadlines',
        description: 'Track expiring commitments and supplier action.',
        icon: 'contracts',
      },
      {
        href: '/vendors/vendors-list',
        label: 'Vendor register',
        description: 'Inspect supplier readiness and history.',
        icon: 'vendors',
      },
    ],
  }),
  [WORKSPACE_ROLES.complianceAuditLead]: createWorkspaceConfig({
    sidebarLabel: 'Compliance Desk',
    pageTitle: 'Compliance & Audit Workspace',
    eyebrow: 'FDSU ERP · Compliance & Audit workspace',
    heroTitle: 'Surface red flags, document gaps, control exceptions, and approval exposure before they become institutional failures.',
    heroDescription:
      'This workspace is optimized for compliance and audit follow-up: open alerts, expiring obligations, evidence gaps, approvals awaiting control review, and links into the exact underlying ERP records.',
    primaryAction: { href: '/compliance_alerts/compliance_alerts-list', label: 'Review compliance alerts' },
    secondaryAction: { href: '/audit_logs/audit_logs-list', label: 'Open audit logs' },
    highlightedMetricKeys: ['openRiskAlerts', 'vendorComplianceAlerts', 'contractsNearingExpiry', 'pendingApprovals', 'overduePayments', 'unreadNotifications'],
    heroMetricKeys: ['openRiskAlerts', 'vendorComplianceAlerts', 'contractsNearingExpiry', 'pendingApprovals'],
    blockOrder: ['watchlist', 'focus', 'summary', 'approvalRisk', 'operations', 'delivery', 'actions'],
    sectionCopy: {
      approvalQueue: {
        eyebrow: 'Control review queue',
        title: 'Approvals and cases requiring audit follow-through',
        actionLabel: 'Open control queue',
      },
      riskPanel: {
        eyebrow: 'Compliance watch',
        title: 'Exceptions, overdue items, and evidence gaps',
      },
      recentNotifications: {
        eyebrow: 'Alert traffic',
        title: 'Recent system signals and policy-relevant activity',
        actionLabel: 'Open alert feed',
      },
      quickActions: {
        eyebrow: 'Audit actions',
        title: 'Go straight to alerts, logs, and records needing review',
      },
    },
    quickLinks: [
      {
        href: '/compliance_alerts/compliance_alerts-list',
        label: 'Compliance alerts',
        description: 'Review open red flags and follow-up status.',
        icon: 'compliance',
      },
      {
        href: '/audit_logs/audit_logs-list',
        label: 'Audit logs',
        description: 'Trace evidence across users and records.',
        icon: 'audit',
      },
      {
        href: '/approvals/approvals-list',
        label: 'Control queue',
        description: 'Inspect approvals needing governance review.',
        icon: 'approvals',
      },
      {
        href: '/contracts/contracts-list',
        label: 'Contract obligations',
        description: 'Check commitments nearing expiry or breach.',
        icon: 'contracts',
      },
    ],
  }),
  [WORKSPACE_ROLES.projectDeliveryLead]: createWorkspaceConfig({
    sidebarLabel: 'Delivery Command',
    pageTitle: 'Project Delivery Workspace',
    eyebrow: 'FDSU ERP · Project Delivery workspace',
    heroTitle: 'Keep execution moving by watching project progress, blocked approvals, payment lag, and field-level delivery risk.',
    heroDescription:
      'This workspace centers on implementation performance: active projects, milestone progress, approvals affecting execution, and risks that can delay delivery across provinces and programs.',
    primaryAction: { href: '/projects/projects-list', label: 'Review projects' },
    secondaryAction: { href: '/project_milestones/project_milestones-list', label: 'Review milestones' },
    highlightedMetricKeys: ['activeProjects', 'averageProjectProgress', 'highRiskProjects', 'pendingApprovals', 'overduePayments', 'unreadNotifications'],
    heroMetricKeys: ['activeProjects', 'averageProjectProgress', 'highRiskProjects', 'pendingApprovals'],
    blockOrder: ['summary', 'focus', 'delivery', 'approvalRisk', 'watchlist', 'operations', 'actions'],
    sectionCopy: {
      approvalQueue: {
        eyebrow: 'Execution blockers',
        title: 'Approvals holding back project delivery',
        actionLabel: 'Open delivery blockers',
      },
      provinceRollout: {
        eyebrow: 'Delivery heatmap',
        title: 'Progress by province and execution footprint',
        actionLabel: 'Open project register',
      },
      topContracts: {
        eyebrow: 'Major delivery contracts',
        title: 'Largest commitments underpinning project execution',
        actionLabel: 'Open suppliers',
      },
      quickActions: {
        eyebrow: 'Delivery actions',
        title: 'Move into the projects, milestones, and blockers that need attention',
      },
    },
    quickLinks: [
      {
        href: '/projects/projects-list',
        label: 'Project portfolio',
        description: 'Review implementation status and ownership.',
        icon: 'projects',
      },
      {
        href: '/project_milestones/project_milestones-list',
        label: 'Milestones',
        description: 'Track critical delivery checkpoints.',
        icon: 'milestones',
      },
      {
        href: '/approvals/approvals-list',
        label: 'Delivery blockers',
        description: 'See approvals slowing field execution.',
        icon: 'approvals',
      },
      {
        href: '/contracts/contracts-list',
        label: 'Supporting contracts',
        description: 'Inspect contracts affecting delivery scope.',
        icon: 'contracts',
      },
    ],
  }),
};

export function getWorkspaceConfig(roleName?: string | null): WorkspaceConfig {
  if (!roleName) {
    return workspaceConfigs[WORKSPACE_ROLES.projectDeliveryLead];
  }

  return workspaceConfigs[roleName] || workspaceConfigs[WORKSPACE_ROLES.projectDeliveryLead];
}

export function getWorkspaceRoute(roleName?: string | null) {
  if (!roleName) {
    return '/executive-summary';
  }

  return WORKSPACE_ROUTES[roleName] || '/executive-summary';
}

export function normalizeInternalRedirectTarget(target?: string | string[] | null) {
  const redirectTarget = Array.isArray(target) ? target[0] : target;

  if (!redirectTarget || typeof redirectTarget !== 'string') {
    return null;
  }

  if (!redirectTarget.startsWith('/') || redirectTarget.startsWith('//')) {
    return null;
  }

  if (redirectTarget.startsWith('/login')) {
    return null;
  }

  return redirectTarget;
}

export function getPostLoginRoute(roleName?: string | null, redirectTarget?: string | string[] | null) {
  return normalizeInternalRedirectTarget(redirectTarget) || getWorkspaceRoute(roleName);
}

export function getLoginRoute(redirectTarget?: string | string[] | null) {
  const safeRedirectTarget = normalizeInternalRedirectTarget(redirectTarget);

  if (!safeRedirectTarget) {
    return '/login';
  }

  return `/login?redirect=${encodeURIComponent(safeRedirectTarget)}`;
}

export function isDashboardRole(roleName?: string | null) {
  if (!roleName) {
    return false;
  }

  return ADMIN_DASHBOARD_ROLES.includes(roleName as (typeof ADMIN_DASHBOARD_ROLES)[number]);
}

export function itemVisibleForRole(itemRoles?: string[], roleName?: string | null) {
  if (!itemRoles?.length) {
    return true;
  }

  if (!roleName) {
    return false;
  }

  return itemRoles.includes(roleName);
}
