import { jsx, jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
function padText(value, width) {
  if (value.length >= width) return value.slice(0, width - 1) + "\u2026";
  return value.padEnd(width, " ");
}
function formatStatus(status) {
  switch (status) {
    case "running":
      return "running";
    case "completed":
      return "done";
    case "failed":
      return "failed";
    case "cancelled":
      return "cancelled";
    default:
      return status;
  }
}
function BackgroundTasksPanel({
  tasks,
  isExpanded,
  isProcessing = false
}) {
  if (!isExpanded) return null;
  const sortedTasks = [...tasks].sort((a, b) => {
    if (a.status === b.status) return 0;
    if (a.status === "running") return -1;
    if (b.status === "running") return 1;
    return a.status.localeCompare(b.status);
  });
  const runningCount = tasks.filter((task) => task.status === "running").length;
  const totalCount = tasks.length;
  const headerText = `\u{1F9F5} Background Tasks (${runningCount}/${totalCount} running)`;
  if (totalCount === 0) {
    return /* @__PURE__ */ jsxs(
      Box,
      {
        flexDirection: "column",
        borderStyle: isProcessing ? void 0 : "round",
        borderColor: "gray",
        paddingX: 1,
        marginX: 1,
        marginBottom: 1,
        children: [
          /* @__PURE__ */ jsx(Text, { bold: true, color: "cyan", children: headerText }),
          /* @__PURE__ */ jsx(Text, { color: "gray", dimColor: true, children: "No background tasks" })
        ]
      }
    );
  }
  const statusWidth = 10;
  const idWidth = 14;
  const descWidth = 48;
  return /* @__PURE__ */ jsxs(
    Box,
    {
      flexDirection: "column",
      borderStyle: isProcessing ? void 0 : "round",
      borderColor: "gray",
      paddingX: 1,
      marginX: 1,
      marginBottom: 1,
      children: [
        /* @__PURE__ */ jsxs(Box, { children: [
          /* @__PURE__ */ jsx(Text, { bold: true, color: "cyan", children: headerText }),
          /* @__PURE__ */ jsxs(Text, { color: "gray", dimColor: true, children: [
            " ",
            "\xB7 ctrl+b to hide bg tasks"
          ] })
        ] }),
        /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
          padText("status", statusWidth),
          padText("task id", idWidth),
          padText("description", descWidth)
        ] }) }),
        sortedTasks.map((task) => {
          const status = formatStatus(task.status);
          const desc = task.description ?? "";
          return /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsxs(Text, { color: task.status === "running" ? "yellow" : "gray", children: [
            padText(status, statusWidth),
            padText(task.taskId, idWidth),
            padText(desc, descWidth)
          ] }) }, task.taskId);
        })
      ]
    }
  );
}
export {
  BackgroundTasksPanel
};
