"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useTranslations } from "next-intl";
import { useState } from "react";
import { Badge } from "../../ui/badge";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "../../ui/table";
import { Button } from "../../ui/button";
import { ChevronDownIcon, ChevronRightIcon } from "lucide-react";
import { sel } from "../../../lib/test";
function ActionsTable({ actions }) {
  const t = useTranslations("devtools.scheduledActions");
  const [expandedRows, setExpandedRows] = useState(/* @__PURE__ */ new Set());
  const toggleRow = (id) => {
    setExpandedRows((prev) => {
      const next = new Set(prev);
      if (next.has(id)) {
        next.delete(id);
      } else {
        next.add(id);
      }
      return next;
    });
  };
  const getStatusBadge = (status) => {
    const variants = {
      pending: { variant: "secondary", cy: sel("devtools.scheduledActions.statusPending") },
      running: { variant: "default", cy: sel("devtools.scheduledActions.statusRunning") },
      completed: { variant: "outline", cy: sel("devtools.scheduledActions.statusCompleted") },
      failed: { variant: "destructive", cy: sel("devtools.scheduledActions.statusFailed") }
    };
    const config = variants[status];
    return /* @__PURE__ */ jsx(Badge, { variant: config.variant, "data-cy": config.cy, children: t(`status.${status}`) });
  };
  const formatDate = (date) => {
    return new Date(date).toLocaleString();
  };
  if (actions.length === 0) {
    return /* @__PURE__ */ jsxs(
      "div",
      {
        className: "text-center py-12 text-muted-foreground",
        "data-cy": sel("devtools.scheduledActions.emptyState"),
        children: [
          /* @__PURE__ */ jsx("p", { className: "text-lg font-medium", children: t("empty") }),
          /* @__PURE__ */ jsx("p", { className: "text-sm mt-1", children: t("emptyDescription") })
        ]
      }
    );
  }
  return /* @__PURE__ */ jsx("div", { className: "border rounded-lg", "data-cy": sel("devtools.scheduledActions.table"), children: /* @__PURE__ */ jsxs(Table, { children: [
    /* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
      /* @__PURE__ */ jsx(TableHead, { className: "w-[40px]" }),
      /* @__PURE__ */ jsx(TableHead, { "data-cy": sel("devtools.scheduledActions.cellType"), children: t("table.type") }),
      /* @__PURE__ */ jsx(TableHead, { "data-cy": sel("devtools.scheduledActions.cellStatus"), children: t("table.status") }),
      /* @__PURE__ */ jsx(TableHead, { "data-cy": sel("devtools.scheduledActions.cellScheduledAt"), children: t("table.scheduledAt") }),
      /* @__PURE__ */ jsx(TableHead, { "data-cy": sel("devtools.scheduledActions.cellTeam"), children: t("table.team") }),
      /* @__PURE__ */ jsx(TableHead, { "data-cy": sel("devtools.scheduledActions.cellPayload"), children: t("table.payload") }),
      /* @__PURE__ */ jsx(TableHead, { "data-cy": sel("devtools.scheduledActions.cellError"), children: t("table.error") })
    ] }) }),
    /* @__PURE__ */ jsx(TableBody, { children: actions.map((action) => {
      const isExpanded = expandedRows.has(action.id);
      return /* @__PURE__ */ jsxs(
        TableRow,
        {
          "data-cy": sel("devtools.scheduledActions.row", { id: action.id }),
          children: [
            /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(
              Button,
              {
                variant: "ghost",
                size: "sm",
                className: "h-8 w-8 p-0",
                onClick: () => toggleRow(action.id),
                children: isExpanded ? /* @__PURE__ */ jsx(ChevronDownIcon, { className: "h-4 w-4" }) : /* @__PURE__ */ jsx(ChevronRightIcon, { className: "h-4 w-4" })
              }
            ) }),
            /* @__PURE__ */ jsx(TableCell, { className: "font-mono text-sm", children: action.actionType }),
            /* @__PURE__ */ jsx(TableCell, { children: getStatusBadge(action.status) }),
            /* @__PURE__ */ jsx(TableCell, { className: "text-sm", children: formatDate(action.scheduledAt) }),
            /* @__PURE__ */ jsx(TableCell, { className: "text-sm", children: action.teamId ? /* @__PURE__ */ jsxs("code", { className: "text-xs", children: [
              action.teamId.slice(0, 8),
              "..."
            ] }) : /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: t("table.noTeam") }) }),
            /* @__PURE__ */ jsx(TableCell, { children: isExpanded ? /* @__PURE__ */ jsx("pre", { className: "text-xs bg-muted p-2 rounded max-w-md overflow-auto", children: JSON.stringify(action.payload, null, 2) }) : /* @__PURE__ */ jsxs("code", { className: "text-xs text-muted-foreground", children: [
              JSON.stringify(action.payload).slice(0, 50),
              "..."
            ] }) }),
            /* @__PURE__ */ jsx(TableCell, { children: action.errorMessage ? /* @__PURE__ */ jsx("code", { className: "text-xs text-destructive", children: isExpanded ? action.errorMessage : `${action.errorMessage.slice(0, 30)}...` }) : /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "-" }) })
          ]
        },
        action.id
      );
    }) })
  ] }) });
}
export {
  ActionsTable
};
