import { useMemo } from "react";

export default function useAppTaskDerived({
  tasks,
  selectedTaskId,
  taskForm,
  competitorText
}) {
  const competitorGroups = useMemo(() => {
    return competitorText
      .split("\\n")
      .map((line) => line.trim())
      .filter((line) => line.length > 0);
  }, [competitorText]);

  const hasSelectedTask = selectedTaskId != null;
  const selectedTask = tasks.find((task) => task.id === selectedTaskId) || null;
  const selectedTaskName = selectedTask ? (selectedTask.name || `Задача #${selectedTask.id}`) : "—";
  const canSaveTask = Boolean(
    taskForm.name.trim() &&
    taskForm.ourGroup.trim() &&
    competitorGroups.length > 0
  );

  return {
    competitorGroups,
    hasSelectedTask,
    selectedTask,
    selectedTaskName,
    canSaveTask
  };
}
