import { useMemo } from "react";

export default function useUiComputed({
  taskStatus,
  assignedAccountCount,
  roleSummary,
  inviteAccessWarn,
  taskAccountRoles,
  taskStatusMap,
  tasks,
  formatCountdown,
  now
}) {
  const pauseReason = useMemo(() => {
    if (!taskStatus || taskStatus.running) return "";
    if (taskStatus.lastStopReason) return taskStatus.lastStopReason;
    if (taskStatus.dailyRemaining === 0 && taskStatus.dailyLimit > 0) return "Дневной лимит исчерпан";
    if (Number(taskStatus.queueCount || 0) === 0) return "Очередь пуста";
    if (assignedAccountCount === 0) return "Нет назначенных аккаунтов";
    if (roleSummary.invite.length === 0) return "Нет аккаунтов с ролью инвайта";
    if (inviteAccessWarn) return "Есть аккаунты без прав на инвайт";
    return "";
  }, [taskStatus, assignedAccountCount, roleSummary, inviteAccessWarn]);

  const hasPerAccountInviteLimits = useMemo(() => {
    return Object.values(taskAccountRoles || {}).some(
      (roles) => roles && roles.invite && Number(roles.inviteLimit || 0) > 0
    );
  }, [taskAccountRoles]);

  const formatCountdownWithNow = useMemo(() => {
    return (target) => formatCountdown(target, now);
  }, [formatCountdown, now]);

  return {
    pauseReason,
    hasPerAccountInviteLimits,
    formatCountdownWithNow
  };
}
