import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  SingleColumnPageSkeleton
} from "./chunk-INNFZYX2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useDeleteUser,
  useUser
} from "./chunk-GKGLVDDV.js";

// src/pages/users/user-detail/user-detail.tsx
import { Children } from "react";
import { useLoaderData, useParams } from "react-router-dom";

// src/pages/users/user-detail/components/user-general-section/user-general-section.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { Container, Heading, Text, toast, usePrompt } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { jsx, jsxs } from "react/jsx-runtime";
var UserGeneralSection = ({ user }) => {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteUser(user.id);
  const name = [user.first_name, user.last_name].filter(Boolean).join(" ");
  const handleDeleteUser = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("users.deleteUserWarning", {
        name: name ?? user.email
      }),
      verificationText: name ?? user.email,
      verificationInstruction: t("general.typeToConfirm"),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(t("users.deleteUserSuccess", { name: user.email }));
        navigate("..");
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  };
  return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", "data-testid": "user-general-section-container", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-6 py-4", "data-testid": "user-general-section-header", children: [
      /* @__PURE__ */ jsx(Heading, { "data-testid": "user-general-section-email", children: user.email }),
      /* @__PURE__ */ jsx(
        ActionMenu,
        {
          groups: [
            {
              actions: [
                {
                  label: t("actions.edit"),
                  to: "edit",
                  icon: /* @__PURE__ */ jsx(PencilSquare, {})
                }
              ]
            },
            {
              actions: [
                {
                  label: t("actions.delete"),
                  onClick: handleDeleteUser,
                  icon: /* @__PURE__ */ jsx(Trash, {})
                }
              ]
            }
          ],
          "data-testid": "user-general-section-action-menu"
        }
      )
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4", "data-testid": "user-general-section-name", children: [
      /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", weight: "plus", "data-testid": "user-general-section-name-label", children: t("fields.name") }),
      /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", "data-testid": "user-general-section-name-value", children: name ?? "-" })
    ] })
  ] });
};

// src/pages/users/user-detail/user-detail.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const initialData = useLoaderData();
  const { id } = useParams();
  const {
    user,
    isPending: isLoading,
    isError,
    error
  } = useUser(id, void 0, {
    initialData
  });
  if (isLoading || !user) {
    return /* @__PURE__ */ jsx2(SingleColumnPageSkeleton, { sections: 1, showJSON: true, showMetadata: true });
  }
  if (isError) {
    throw error;
  }
  return Children.count(children) > 0 ? /* @__PURE__ */ jsx2(SingleColumnPage, { data: user, showJSON: true, showMetadata: true, children }) : /* @__PURE__ */ jsx2(SingleColumnPage, { data: user, showJSON: true, showMetadata: true, children: /* @__PURE__ */ jsx2(UserGeneralSection, { user }) });
};
var UserDetailPage = Object.assign(Root, {
  GeneralSection: UserGeneralSection
});

export {
  UserDetailPage
};
