export type PaginationProfileKey =
  | 'master-data'
  | 'transaction'
  | 'report'
  | 'audit'
  | 'mobile'

export interface PaginationProfile {
  defaultPerPage: number
  pageSizes: number[]
}

export const PAGINATION_PROFILES: Record<PaginationProfileKey, PaginationProfile> = {
  'master-data': {
    defaultPerPage: 25,
    pageSizes: [25, 50, 100],
  },
  transaction: {
    defaultPerPage: 25,
    pageSizes: [25, 50, 100],
  },
  report: {
    defaultPerPage: 50,
    pageSizes: [50, 100, 250],
  },
  audit: {
    defaultPerPage: 50,
    pageSizes: [50, 100, 250],
  },
  mobile: {
    defaultPerPage: 10,
    pageSizes: [10, 25, 50],
  },
}

const PROFILE_MATCHERS: Array<{ profile: PaginationProfileKey; pattern: RegExp }> = [
  { profile: 'mobile', pattern: /(^|\/)mobile(\/|$)/i },
  { profile: 'audit', pattern: /(^|\/)audit(\/|$)/i },
  { profile: 'report', pattern: /(^|\/)report(s)?(\/|$)/i },
  {
    profile: 'transaction',
    pattern: /(^|\/)(transaction|transaksi|sales-order|sales-orders|journal|budget|fund-request)(\/|$)/i,
  },
  { profile: 'master-data', pattern: /(^|\/)master-data(\/|$)/i },
]

export function detectPaginationProfile(pathname?: string): PaginationProfileKey {
  const normalizedPathname = pathname?.toLowerCase() ?? ''

  for (const matcher of PROFILE_MATCHERS) {
    if (matcher.pattern.test(normalizedPathname)) {
      return matcher.profile
    }
  }

  return 'master-data'
}

export function getPaginationProfile(pathname?: string): PaginationProfile {
  const profile = detectPaginationProfile(pathname)
  return PAGINATION_PROFILES[profile]
}
