"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { CheckCircle2, AlertCircle, ArrowUpRight } from "lucide-react";
import { cn } from "@open-mercato/shared/lib/utils";
import { Button } from "@open-mercato/ui/primitives/button";
import { Badge } from "@open-mercato/ui/primitives/badge";
function isOverdue(dueAt) {
  if (!dueAt) return false;
  return new Date(dueAt) < /* @__PURE__ */ new Date();
}
function priorityLabel(priority) {
  if (priority === null || priority === void 0) return { label: "None", variant: "muted" };
  if (priority >= 3) return { label: "High", variant: "destructive" };
  if (priority === 2) return { label: "Medium", variant: "default" };
  return { label: "Low", variant: "secondary" };
}
function OpenTasksWidget({
  tasks,
  currentUserId,
  t,
  onViewAll
}) {
  const [taskFilter, setTaskFilter] = React.useState("all");
  const overdueTasks = tasks.filter((task) => isOverdue(task.dueAt));
  const mineTasks = currentUserId ? tasks.filter((task) => {
    const assignee = task.assignedToUserId ?? task.createdByUserId;
    return assignee === currentUserId;
  }) : tasks;
  const filteredTasks = taskFilter === "overdue" ? overdueTasks : taskFilter === "mine" ? mineTasks : tasks;
  const filterTabs = [
    { key: "all", label: t("customers.tasks.filters.all", "All"), count: tasks.length },
    { key: "mine", label: t("customers.tasks.filters.mine", "Mine"), count: mineTasks.length },
    { key: "overdue", label: t("customers.tasks.filters.overdue", "Overdue"), count: overdueTasks.length }
  ];
  return /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-5", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsxs("h3", { className: "flex items-center gap-2 text-sm font-semibold text-foreground", children: [
        /* @__PURE__ */ jsx(CheckCircle2, { className: "size-4" }),
        t("customers.companies.dashboard.openTasks", "Open tasks"),
        /* @__PURE__ */ jsx("span", { className: "rounded-full bg-muted px-1.5 py-0.5 text-xs font-medium text-muted-foreground", children: tasks.length })
      ] }),
      /* @__PURE__ */ jsxs(Button, { type: "button", variant: "outline", size: "sm", className: "h-7 text-xs", onClick: onViewAll, children: [
        "+ ",
        t("customers.companies.dashboard.newTask", "New task")
      ] })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "mt-2 flex items-center gap-1", children: filterTabs.map((tab) => /* @__PURE__ */ jsxs(
      Button,
      {
        type: "button",
        variant: taskFilter === tab.key ? "default" : "outline",
        size: "sm",
        className: "h-6 px-2 text-xs",
        onClick: () => setTaskFilter(tab.key),
        children: [
          tab.label,
          /* @__PURE__ */ jsx("span", { className: "ml-1 rounded-full bg-muted/50 px-1 text-overline", children: tab.count })
        ]
      },
      tab.key
    )) }),
    /* @__PURE__ */ jsxs("div", { className: "mt-3 divide-y", children: [
      filteredTasks.slice(0, 4).map((task) => {
        const overdue = isOverdue(task.dueAt);
        const prio = priorityLabel(task.priority);
        return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3 py-2.5 first:pt-0 last:pb-0", children: [
          /* @__PURE__ */ jsxs("div", { className: "min-w-0 flex-1", children: [
            /* @__PURE__ */ jsx("p", { className: "truncate text-sm text-foreground", children: task.title || "\u2014" }),
            task.dueAt && /* @__PURE__ */ jsxs("p", { className: cn("mt-0.5 text-xs", overdue ? "text-destructive" : "text-muted-foreground"), children: [
              overdue && /* @__PURE__ */ jsx(AlertCircle, { className: "mr-1 inline size-3" }),
              overdue ? t("customers.companies.dashboard.overdueBy", "Overdue by {{days}} days", { days: Math.ceil((Date.now() - new Date(task.dueAt).getTime()) / 864e5) }) : t("customers.companies.dashboard.dueOn", "Due: {{date}}", { date: new Date(task.dueAt).toLocaleDateString() })
            ] })
          ] }),
          /* @__PURE__ */ jsx(Badge, { variant: prio.variant, className: "shrink-0 text-xs", children: prio.label }),
          /* @__PURE__ */ jsx(ArrowUpRight, { className: "size-3.5 shrink-0 text-muted-foreground" })
        ] }, task.id);
      }),
      filteredTasks.length === 0 && /* @__PURE__ */ jsx("p", { className: "py-3 text-sm text-muted-foreground", children: t("customers.companies.dashboard.noTasks", "No open tasks") })
    ] })
  ] });
}
export {
  OpenTasksWidget
};
//# sourceMappingURL=OpenTasksWidget.js.map
