"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { ArrowLeft, Edit, Trash2 } from "lucide-react";
import { Button } from "../ui/button";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger
} from "../ui/alert-dialog";
import { CopyableId } from "../shared/CopyableId";
import { createCyId } from "../../lib/test";
import { usePermissions } from "../../lib/permissions/hooks";
function formatDate(dateString) {
  if (!dateString) return null;
  try {
    return new Date(dateString).toLocaleDateString("en-US", {
      month: "short",
      day: "numeric",
      year: "numeric"
    });
  } catch {
    return null;
  }
}
function EntityDetailHeader({
  entityConfig,
  mode = "view",
  entity,
  title,
  backHref,
  backLabel,
  deleteTitle,
  deleteDescription,
  badges,
  showDates = true,
  onEdit,
  onDelete,
  canEdit: canEditProp,
  canDelete: canDeleteProp,
  isDeleting = false,
  customActions,
  className
}) {
  const router = useRouter();
  const tButtons = useTranslations("buttons");
  const tEntities = useTranslations("entities");
  const slug = entityConfig.slug;
  const { canUpdate, canDelete: canDeletePermission } = usePermissions({
    canUpdate: `${slug}.update`,
    canDelete: `${slug}.delete`
  });
  const effectiveCanEdit = canEditProp ?? canUpdate;
  const effectiveCanDelete = canDeleteProp ?? canDeletePermission;
  const resolvedTitle = (() => {
    if (title) return title;
    switch (mode) {
      case "create":
        return tEntities("actions.create", { entity: entityConfig.names.singular });
      case "edit":
        return (entity == null ? void 0 : entity.title) || (entity == null ? void 0 : entity.name) || tEntities("actions.edit", { entity: entityConfig.names.singular });
      case "view":
      default:
        return (entity == null ? void 0 : entity.title) || (entity == null ? void 0 : entity.name) || (entity == null ? void 0 : entity.displayName) || entityConfig.names.singular;
    }
  })();
  const defaultBackHref = mode === "edit" && (entity == null ? void 0 : entity.id) ? `/dashboard/${slug}/${entity.id}` : `/dashboard/${slug}`;
  const resolvedBackHref = backHref ?? defaultBackHref;
  const resolvedBackLabel = backLabel ?? entityConfig.names.plural;
  const handleEdit = () => {
    if (onEdit) {
      onEdit();
    } else if (entity == null ? void 0 : entity.id) {
      router.push(`${resolvedBackHref}/${entity.id}/edit`);
    }
  };
  const handleBack = () => {
    router.push(resolvedBackHref);
  };
  const showActions = mode === "view";
  const showId = mode !== "create" && (entity == null ? void 0 : entity.id);
  const showDatesSection = mode === "view" && showDates && entity && (entity.createdAt || entity.updatedAt);
  const showBadges = mode !== "create" && badges;
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: `flex flex-col sm:flex-row sm:items-start sm:justify-between gap-4 ${className || ""}`,
      "data-cy": createCyId(slug, `${mode}-header`),
      children: /* @__PURE__ */ jsxs("div", { className: "space-y-2 flex-1", children: [
        /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between", children: [
          /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: handleBack,
              className: "gap-2 -ml-2 cursor-pointer",
              "data-cy": createCyId(slug, "back-btn"),
              children: [
                /* @__PURE__ */ jsx(ArrowLeft, { className: "h-4 w-4" }),
                resolvedBackLabel
              ]
            }
          ),
          /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
            customActions,
            showActions && effectiveCanEdit && /* @__PURE__ */ jsxs(
              Button,
              {
                variant: "outline",
                onClick: handleEdit,
                className: "gap-2 cursor-pointer",
                "data-cy": createCyId(slug, "edit-btn"),
                children: [
                  /* @__PURE__ */ jsx(Edit, { className: "h-4 w-4" }),
                  tButtons("edit")
                ]
              }
            ),
            showActions && effectiveCanDelete && onDelete && /* @__PURE__ */ jsxs(AlertDialog, { children: [
              /* @__PURE__ */ jsx(AlertDialogTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
                Button,
                {
                  variant: "outline",
                  className: "gap-2 text-destructive border-destructive hover:bg-destructive/10 cursor-pointer",
                  "data-cy": createCyId(slug, "delete-btn"),
                  children: [
                    /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" }),
                    tButtons("delete")
                  ]
                }
              ) }),
              /* @__PURE__ */ jsxs(AlertDialogContent, { "data-cy": createCyId(slug, "delete-dialog"), children: [
                /* @__PURE__ */ jsxs(AlertDialogHeader, { children: [
                  /* @__PURE__ */ jsx(AlertDialogTitle, { children: deleteTitle || tButtons("delete") }),
                  /* @__PURE__ */ jsx(AlertDialogDescription, { children: deleteDescription || `Are you sure you want to delete "${resolvedTitle}"? This action cannot be undone.` })
                ] }),
                /* @__PURE__ */ jsxs(AlertDialogFooter, { children: [
                  /* @__PURE__ */ jsx(AlertDialogCancel, { "data-cy": createCyId(slug, "delete-cancel"), children: tButtons("cancel") }),
                  /* @__PURE__ */ jsx(
                    AlertDialogAction,
                    {
                      onClick: onDelete,
                      disabled: isDeleting,
                      className: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
                      "data-cy": createCyId(slug, "delete-confirm"),
                      children: isDeleting ? "..." : tButtons("delete")
                    }
                  )
                ] })
              ] })
            ] })
          ] })
        ] }),
        /* @__PURE__ */ jsx(
          "h1",
          {
            className: "text-2xl font-bold text-foreground",
            "data-cy": createCyId(slug, "title"),
            children: resolvedTitle
          }
        ),
        (showBadges || showId || showDatesSection) && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-4 flex-wrap", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 flex-wrap", children: [
            showBadges,
            showId && /* @__PURE__ */ jsx(CopyableId, { id: entity.id, entitySlug: slug })
          ] }),
          showDatesSection && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3 text-xs text-muted-foreground shrink-0", children: [
            entity.createdAt && formatDate(entity.createdAt) && /* @__PURE__ */ jsxs("span", { children: [
              "Created ",
              formatDate(entity.createdAt)
            ] }),
            entity.updatedAt && formatDate(entity.updatedAt) && /* @__PURE__ */ jsxs("span", { children: [
              "Updated ",
              formatDate(entity.updatedAt)
            ] })
          ] })
        ] })
      ] })
    }
  );
}
export {
  EntityDetailHeader
};
