import type { MultiSelectOption } from '../components/MultiSelectDropdown'
import type { BusinessActivityCategory } from '../services/businessActivityCategoryService'

export function flattenBusinessActivityCategories(
  categories: BusinessActivityCategory[],
): MultiSelectOption<string>[] {
  const result: MultiSelectOption<string>[] = []

  function walk(parentId: string | 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
}
