import type { BusinessCategory } from '../types/category'
import type { MultiSelectOption } from '../components/MultiSelectDropdown'

export function flattenBusinessCategories(
  categories: BusinessCategory[],
): MultiSelectOption<number>[] {
  const result: MultiSelectOption<number>[] = []

  function walk(parentId: number | null, depth: number) {
    const children = categories
      .filter((category) => category.parentId === parentId)
      .sort((a, b) => a.sortOrder - b.sortOrder || a.name.localeCompare(b.name))

    for (const child of children) {
      result.push({ value: child.id, label: child.name, depth })
      walk(child.id, depth + 1)
    }
  }

  walk(null, 0)
  return result
}
