"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import Link from "next/link";
import { usePathname, useSearchParams } from "next/navigation";
import { Trash2, Loader2 } from "lucide-react";
import { Button } from "../../primitives/button.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { FormActionButtons } from "./FormActionButtons.js";
import { ActionsDropdown } from "./ActionsDropdown.js";
import { InjectionSpot } from "../injection/InjectionSpot.js";
function FormHeader(props) {
  const t = useT();
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const resolvedBackLabel = props.backLabel ?? t("ui.navigation.back");
  const injectionContext = React.useMemo(
    () => ({
      path: pathname ?? "",
      query: searchParams?.toString() ?? ""
    }),
    [pathname, searchParams]
  );
  if (props.mode === "detail") {
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(DetailHeader, { ...props, resolvedBackLabel }),
      /* @__PURE__ */ jsx(InjectionSpot, { spotId: "form-header:detail", context: injectionContext })
    ] });
  }
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(EditHeader, { ...props, resolvedBackLabel }),
    /* @__PURE__ */ jsx(InjectionSpot, { spotId: "form-header:edit", context: injectionContext })
  ] });
}
function EditHeader({
  backHref,
  resolvedBackLabel,
  title,
  actions,
  actionsContent
}) {
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between sm:gap-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
      backHref ? /* @__PURE__ */ jsxs(Link, { href: backHref, className: "text-sm text-muted-foreground hover:text-foreground", children: [
        "\u2190 ",
        resolvedBackLabel
      ] }) : null,
      title ? /* @__PURE__ */ jsx("div", { className: "text-base font-medium", children: title }) : null
    ] }),
    actionsContent ?? (actions ? /* @__PURE__ */ jsx(FormActionButtons, { ...actions }) : null)
  ] });
}
function DetailHeader({
  backHref,
  resolvedBackLabel,
  title,
  entityTypeLabel,
  subtitle,
  statusBadge,
  menuActions,
  menuLabel,
  menuTriggerMode,
  menuAriaLabel,
  utilityActions,
  onDelete,
  deleteLabel,
  isDeleting,
  actionsContent
}) {
  const t = useT();
  const resolvedDeleteLabel = deleteLabel ?? t("ui.forms.actions.delete");
  const hasActions = actionsContent || utilityActions || menuActions?.length || onDelete;
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2 md:gap-3 md:flex-row md:items-center md:justify-between", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-2 md:gap-3 min-w-0", children: [
      backHref ? /* @__PURE__ */ jsxs(
        Link,
        {
          href: backHref,
          className: "inline-flex items-center text-sm text-muted-foreground hover:text-foreground shrink-0",
          children: [
            /* @__PURE__ */ jsx("span", { "aria-hidden": true, className: "mr-1 text-base", children: "\u2190" }),
            /* @__PURE__ */ jsx("span", { className: "sr-only", children: resolvedBackLabel })
          ]
        }
      ) : null,
      /* @__PURE__ */ jsxs("div", { className: "space-y-0.5 md:space-y-1 min-w-0", children: [
        entityTypeLabel ? /* @__PURE__ */ jsx("p", { className: "text-xs uppercase text-muted-foreground", children: entityTypeLabel }) : null,
        title ? typeof title === "string" ? /* @__PURE__ */ jsx("h1", { className: "text-lg md:text-2xl font-semibold leading-tight truncate", children: title }) : /* @__PURE__ */ jsx("div", { className: "text-lg md:text-2xl font-semibold leading-tight", children: title }) : null,
        statusBadge,
        subtitle ? /* @__PURE__ */ jsx("p", { className: "text-xs md:text-sm text-muted-foreground", children: subtitle }) : null
      ] })
    ] }),
    hasActions ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap items-center gap-2", children: actionsContent ? actionsContent : /* @__PURE__ */ jsxs(Fragment, { children: [
      utilityActions,
      menuActions?.length ? /* @__PURE__ */ jsx(
        ActionsDropdown,
        {
          items: menuActions,
          label: menuLabel,
          triggerMode: menuTriggerMode,
          ariaLabel: menuAriaLabel
        }
      ) : null,
      onDelete ? /* @__PURE__ */ jsxs(
        Button,
        {
          type: "button",
          variant: "outline",
          size: "sm",
          onClick: onDelete,
          disabled: isDeleting,
          className: "text-red-600 border-red-200 hover:bg-red-50 rounded",
          children: [
            isDeleting ? /* @__PURE__ */ jsx(Loader2, { className: "size-4 mr-2 animate-spin" }) : /* @__PURE__ */ jsx(Trash2, { className: "size-4 mr-2" }),
            resolvedDeleteLabel
          ]
        }
      ) : null
    ] }) }) : null
  ] });
}
export {
  FormHeader
};
//# sourceMappingURL=FormHeader.js.map
