import { CategoryStatus } from '../../constants';
import { type CategoryItem } from '../../types';

import {
  type ManageCategoryChildItem,
  type ManageCategoryFormValues,
} from './form';

export const newCategoryItem = () => ({
  name: '',
  status: CategoryStatus.DRAFT,
  amountOfCourses: 0,
  children: [],
});

const formatManageCategoryChildDefaultValues = (child: CategoryItem) => {
  return {
    originalId: child.id,
    name: child.name,
    status: child.status,
    amountOfCourses: child.amountOfCourses,
    children: child.children,
  };
};

export const formatManageCategoryDefaultValues = (category: CategoryItem) => {
  return {
    children:
      category.children?.map(formatManageCategoryChildDefaultValues) ?? [],
  };
};

export const formatManageCategoryChildParams = (
  child: ManageCategoryChildItem,
) => {
  return {
    id: child.originalId,
    name: child.name,
  };
};

export const formatManageCategoryParams = (
  category: CategoryItem,
  values: ManageCategoryFormValues,
) => {
  return {
    categoryId: category.id,
    name: category.name,
    children: values.children.map(formatManageCategoryChildParams),
  };
};
