import {
  type CategoriesRecord,
  type CategoryFilter,
  type CourseListItem,
  CourseStatus,
  NoCategory,
} from 'src/pages/dashboard/Learning/Courses/types';
import { type Category } from 'src/pages/dashboard/Learning/Settings/Categories/types';

export const getToggleStatus = (status: CourseStatus) =>
  status === CourseStatus.ACTIVE ? CourseStatus.INACTIVE : CourseStatus.ACTIVE;

export const getCategoryFiltersParams = (
  categoryIds: string[],
): CategoryFilter => {
  const includeNoCategory =
    categoryIds[categoryIds.length - 1] === NoCategory.ID;

  const cleanCategoryIds = includeNoCategory
    ? categoryIds.slice(0, categoryIds.length - 1)
    : categoryIds;

  return {
    categoryIds: cleanCategoryIds.length ? cleanCategoryIds.join(',') : '',
    includeNoCategory,
  };
};

export const throwNoCourseSelected = () => {
  throw new Error('Course not selected');
};

export const buildCategoryFilterValues = (
  categories: Category[],
  categoryFilterIds: string[],
  cleanFields: boolean = false,
) => {
  return categories.reduce((acc, category) => {
    acc[category.id] = cleanFields
      ? false
      : categoryFilterIds.includes(category.id.toString());
    return acc;
  }, {} as CategoriesRecord);
};

export const getCourseStatusType = (course: CourseListItem) => {
  switch (course.status) {
    case CourseStatus.ACTIVE:
      return 'success';
    case CourseStatus.INACTIVE:
      return 'disabled';
    default:
      return 'neutral';
  }
};
