import { useTranslation } from "next-i18next";

import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";

export default function Component({ service }) {
  const { t } = useTranslation();

  const { widget } = service;

  const { data: queueData, error: queueError } = useWidgetAPI(widget, "queue");

  if (queueError) {
    return <Container error={t("widget.api_error")} />;
  }

  if (!queueData) {
    return (
      <Container>
        <Block label={t("sabnzbd.rate")} />
        <Block label={t("sabnzbd.queue")} />
        <Block label={t("sabnzbd.timeleft")} />
      </Container>
    );
  }

  return (
    <Container>
      <Block label={t("sabnzbd.rate")} value={`${queueData.queue.speed}B/s`} />
      <Block label={t("sabnzbd.queue")} value={t("common.number", { value: queueData.queue.noofslots })} />
      <Block label={t("sabnzbd.timeleft")} value={queueData.queue.timeleft} />
    </Container>
  );
}
