import { getTasksOfCategory } from 'src/pages/dashboard/onboarding/queries';
import { IconTypes } from 'src/types/icons';
import { OnboardingStatus } from 'src/types/onboarding';

export const isCurrentStep = (categories, index) => {
  let currentStep = false;
  if (
    index === 0 &&
    categories?.length >= 0 &&
    !categories[index]?.alreadyDone
  ) {
    currentStep = true;
  }
  if (
    index >= 0 &&
    categories?.length === index + 1 &&
    categories[index]?.alreadyDone
  ) {
    currentStep = true;
  }

  if (!categories[index]?.alreadyDone && categories[index - 1]?.alreadyDone) {
    currentStep = true;
  }
  return currentStep;
};

export const canOpenCategory = (categories, index) => {
  if (index === 0 && !categories[index]?.alreadyDone) {
    return true;
  }

  if (categories[index]?.alreadyDone) {
    return true;
  }

  if (!categories[index]?.alreadyDone && categories[index - 1]?.alreadyDone) {
    return true;
  }

  return false;
};

export const getTitleCategory = (categories, categoryId) =>
  categories.find(elem => elem.id === Number(categoryId))?.name;

export const getIconCategory = (categories, categoryId) =>
  categories.find(elem => elem.id === Number(categoryId))?.icon;

export const getTasksTodo = tasks =>
  tasks
    ?.filter(item => item.status === OnboardingStatus.TODO)
    .map(elem => elem.task);

export const getTasksDone = tasks =>
  tasks
    ?.filter(item => item.status === OnboardingStatus.DONE)
    .map(elem => elem.task);

export const checkCategoryAlreadyDone = categoryId => {
  const tasks = getTasksOfCategory(categoryId);
  return tasks.every(category => category.status === OnboardingStatus.DONE);
};

export const getWidthHeightOnCategoryImage = icon => {
  if (icon.type === IconTypes.IMAGE && !icon.custom) {
    return '100px';
  }
  if (icon.type === IconTypes.EMOJI) {
    return '100px';
  }
  return '150px';
};

export const getBorderRadiusOnCategoryImage = icon => {
  if (icon.type === IconTypes.IMAGE && !icon.custom) {
    return '0%';
  }
  return '50%';
};
