import type { Employee } from '~/services/dto/employee'

export const compareEmployeesInSite = (employeeA: Employee, employeeB: Employee) => {
  const orderA = employeeA.displayOrder ?? 0
  const orderB = employeeB.displayOrder ?? 0
  if (orderA !== orderB) return orderA - orderB

  const lastNameA = employeeA.lastName ?? ''
  const lastNameB = employeeB.lastName ?? ''
  if (lastNameA !== lastNameB) return lastNameA.localeCompare(lastNameB, 'fr')

  const firstNameA = employeeA.firstName ?? ''
  const firstNameB = employeeB.firstName ?? ''
  return firstNameA.localeCompare(firstNameB, 'fr')
}

export const compareEmployeesBySiteAndOrder = (employeeA: Employee, employeeB: Employee) => {
  const siteOrderA = employeeA.site?.displayOrder ?? 0
  const siteOrderB = employeeB.site?.displayOrder ?? 0
  if (siteOrderA !== siteOrderB) return siteOrderA - siteOrderB

  const siteNameA = employeeA.site?.name ?? ''
  const siteNameB = employeeB.site?.name ?? ''
  if (siteNameA !== siteNameB) return siteNameA.localeCompare(siteNameB, 'fr')

  return compareEmployeesInSite(employeeA, employeeB)
}

export const sortEmployeesBySiteAndOrder = (employees: Employee[]) => {
  return [...employees].sort(compareEmployeesBySiteAndOrder)
}
