import {
  require_dist
} from "./chunk-ZZO3INVE.js";
import "./chunk-FQERBIVS.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import "./chunk-YBZWO4ZV.js";
import "./chunk-KAZ5BYTQ.js";
import "./chunk-3EF54XFY.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import "./chunk-INNFZYX2.js";
import "./chunk-Y7QKP6QU.js";
import "./chunk-NNI6QKKZ.js";
import "./chunk-MG4GKUJW.js";
import "./chunk-AYVH6Q5O.js";
import "./chunk-VOHEQ7B5.js";
import "./chunk-C3WE6EFO.js";
import {
  useUserInviteTableQuery
} from "./chunk-N5IUYCUZ.js";
import "./chunk-7SVLKK6Q.js";
import "./chunk-GR6JUIRU.js";
import "./chunk-PVZBDWLK.js";
import "./chunk-3B6MTW5G.js";
import "./chunk-VKOE5J66.js";
import "./chunk-DY3XQPHC.js";
import "./chunk-VVWB3GIK.js";
import "./chunk-KBYZLKI4.js";
import "./chunk-YWWUOGJA.js";
import "./chunk-THHRRYRS.js";
import "./chunk-UZWFZMOX.js";
import "./chunk-XQNVC4Q7.js";
import {
  RouteFocusModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useInviteSellerMember,
  useSellerInvites
} from "./chunk-M3YP3NX4.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import {
  __toESM
} from "./chunk-NBMM2TZK.js";

// src/pages/stores/store-member-invite/components/invite-member-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import {
  Button,
  Container,
  Heading,
  Input,
  Select,
  StatusBadge,
  Text,
  toast,
  Tooltip
} from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { format } from "date-fns";
import { useMemo } from "react";
import { useForm } from "react-hook-form";
import { Trans, useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
import * as zod from "zod";
var import_types = __toESM(require_dist(), 1);
import { jsx, jsxs } from "react/jsx-runtime";
var ROLE_OPTIONS = [
  {
    value: import_types.SellerRole.SELLER_ADMINISTRATION,
    labelKey: "users.roles.administration"
  },
  {
    value: import_types.SellerRole.INVENTORY_MANAGEMENT,
    labelKey: "users.roles.inventoryManagement"
  },
  {
    value: import_types.SellerRole.ORDER_MANAGEMENT,
    labelKey: "users.roles.orderManagement"
  },
  { value: import_types.SellerRole.ACCOUNTING, labelKey: "users.roles.accounting" },
  { value: import_types.SellerRole.SUPPORT, labelKey: "users.roles.support" }
];
var ROLE_LABEL_MAP = Object.fromEntries(
  ROLE_OPTIONS.map((r) => [r.value, r.labelKey])
);
var InviteMemberSchema = zod.object({
  email: zod.string().email(),
  role_id: zod.string().min(1)
});
var PAGE_SIZE = 10;
var PREFIX = "inv";
var InviteMemberForm = () => {
  const { t } = useTranslation();
  const { id } = useParams();
  const form = useForm({
    defaultValues: {
      email: "",
      role_id: ""
    },
    resolver: zodResolver(InviteMemberSchema)
  });
  const { raw, searchParams } = useUserInviteTableQuery({
    prefix: PREFIX,
    pageSize: PAGE_SIZE
  });
  const {
    member_invites,
    count,
    isPending: isLoading,
    isError,
    error
  } = useSellerInvites(id, searchParams);
  const columns = useColumns();
  const { table } = useDataTable({
    data: member_invites ?? [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE,
    prefix: PREFIX
  });
  const { mutateAsync, isPending } = useInviteSellerMember(id);
  const handleSubmit = form.handleSubmit(async (values) => {
    try {
      await mutateAsync({
        email: values.email,
        role_id: values.role_id
      });
      toast.success(t("users.inviteSuccess", "Invite sent successfully"));
      form.reset();
    } catch (error2) {
      toast.error(error2.message);
    }
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8 px-2 py-16", children: [
          /* @__PURE__ */ jsxs("div", { children: [
            /* @__PURE__ */ jsx(Heading, { children: t("users.inviteUser", "Invite User") }),
            /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: t(
              "users.inviteUserHint",
              "Invite a new user to this store."
            ) })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
            /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "email",
                  render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsx(Form.Label, { children: t("fields.email") }),
                    /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                  ] })
                }
              ),
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "role_id",
                  render: ({ field: { onChange, ref, ...field } }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsx(Form.Label, { children: t("fields.role") }),
                    /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(Select, { ...field, onValueChange: onChange, children: [
                      /* @__PURE__ */ jsx(Select.Trigger, { ref, children: /* @__PURE__ */ jsx(
                        Select.Value,
                        {
                          placeholder: t(
                            "users.selectRole",
                            "Select a role"
                          )
                        }
                      ) }),
                      /* @__PURE__ */ jsx(Select.Content, { children: ROLE_OPTIONS.map((role) => /* @__PURE__ */ jsx(
                        Select.Item,
                        {
                          value: role.value,
                          children: t(role.labelKey)
                        },
                        role.value
                      )) })
                    ] }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                  ] })
                }
              )
            ] }),
            /* @__PURE__ */ jsx("div", { className: "flex items-center justify-end", children: /* @__PURE__ */ jsx(
              Button,
              {
                size: "small",
                variant: "secondary",
                type: "submit",
                isLoading: isPending,
                children: t("users.sendInvite", "Send invite")
              }
            ) })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
            /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("users.pendingInvites", "Pending Invites") }),
            /* @__PURE__ */ jsx(Container, { className: "overflow-hidden p-0", children: /* @__PURE__ */ jsx(
              _DataTable,
              {
                table,
                columns,
                count,
                pageSize: PAGE_SIZE,
                pagination: true,
                isLoading,
                queryObject: raw,
                prefix: PREFIX,
                orderBy: [
                  { key: "email", label: t("fields.email") },
                  {
                    key: "created_at",
                    label: t("fields.createdAt")
                  }
                ]
              }
            ) })
          ] })
        ] }) }) })
      ]
    }
  ) });
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const { t } = useTranslation();
  return useMemo(
    () => [
      columnHelper.accessor("email", {
        header: t("fields.email"),
        cell: ({ getValue }) => getValue()
      }),
      columnHelper.accessor("role_id", {
        header: t("fields.role"),
        cell: ({ getValue }) => {
          const roleId = getValue();
          const labelKey = ROLE_LABEL_MAP[roleId];
          return labelKey ? t(labelKey) : "-";
        }
      }),
      columnHelper.accessor("accepted", {
        header: t("fields.status"),
        cell: ({ getValue, row }) => {
          const accepted = getValue();
          const expired = new Date(row.original.expires_at) < /* @__PURE__ */ new Date();
          if (accepted) {
            return /* @__PURE__ */ jsx(
              Tooltip,
              {
                content: t("users.acceptedOnDate", {
                  date: format(
                    new Date(row.original.updated_at),
                    "dd MMM, yyyy"
                  )
                }),
                children: /* @__PURE__ */ jsx(StatusBadge, { color: "green", children: t("users.inviteStatus.accepted") })
              }
            );
          }
          if (expired) {
            return /* @__PURE__ */ jsx(
              Tooltip,
              {
                content: t("users.expiredOnDate", {
                  date: format(
                    new Date(row.original.expires_at),
                    "dd MMM, yyyy"
                  )
                }),
                children: /* @__PURE__ */ jsx(StatusBadge, { color: "red", children: t("users.inviteStatus.expired") })
              }
            );
          }
          return /* @__PURE__ */ jsx(
            Tooltip,
            {
              content: /* @__PURE__ */ jsx(
                Trans,
                {
                  i18nKey: "users.validFromUntil",
                  components: [
                    /* @__PURE__ */ jsx("span", { className: "font-medium" }, "from"),
                    /* @__PURE__ */ jsx("span", { className: "font-medium" }, "until")
                  ],
                  values: {
                    from: format(
                      new Date(row.original.created_at),
                      "dd MMM, yyyy"
                    ),
                    until: format(
                      new Date(row.original.expires_at),
                      "dd MMM, yyyy"
                    )
                  }
                }
              ),
              children: /* @__PURE__ */ jsx(StatusBadge, { color: "orange", children: t("users.inviteStatus.pending") })
            }
          );
        }
      })
    ],
    [t]
  );
};

// src/pages/stores/store-member-invite/index.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Component = () => {
  return /* @__PURE__ */ jsx2(RouteFocusModal, { children: /* @__PURE__ */ jsx2(InviteMemberForm, {}) });
};
export {
  Component
};
