import {
  DateCell
} from "./chunk-25LYVP3C.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useQueryParams
} from "./chunk-THHRRYRS.js";
import {
  useUsers
} from "./chunk-GKGLVDDV.js";

// src/pages/users/user-list/user-list.tsx
import { Children as Children3 } from "react";

// src/pages/users/user-list/components/user-list-table/user-list-table.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/users/user-list/components/user-list-table/user-list-header.tsx
import { Children } from "react";
import { Button, Heading } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var UserListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("users.domain") }) });
};
var UserListActions = ({
  children
}) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(Link, { to: "invite", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: t("users.invite") }) }) });
};
var UserListHeader = ({
  children
}) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-between px-6 py-4", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(UserListTitle, {}),
    /* @__PURE__ */ jsx(UserListActions, {})
  ] }) });
};

// src/pages/users/user-list/components/user-list-table/user-list-data-table.tsx
import { useMemo as useMemo2 } from "react";
import { PencilSquare } from "@medusajs/icons";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { useTranslation as useTranslation3 } from "react-i18next";
import { useNavigate } from "react-router-dom";

// src/hooks/table/query/use-user-table-query.tsx
var useUserTableQuery = (options) => {
  const { prefix, pageSize = 20 } = options ?? {};
  const queryObject = useQueryParams(["offset", "q", "created_at", "updated_at", "order"], prefix);
  const { offset, created_at, updated_at, q, order } = queryObject;
  const searchParams = {
    limit: pageSize,
    offset: offset ? Number(offset) : 0,
    created_at: created_at ? JSON.parse(created_at) : void 0,
    updated_at: updated_at ? JSON.parse(updated_at) : void 0,
    order: order ? order : "-created_at",
    q
  };
  return {
    searchParams,
    raw: queryObject
  };
};

// src/pages/users/user-list/components/user-list-table/use-user-table-filters.tsx
import { useMemo } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
var useUserTableFilters = () => {
  const { t } = useTranslation2();
  return useMemo(
    () => [
      {
        key: "created_at",
        label: t("fields.createdAt"),
        type: "date"
      },
      {
        key: "updated_at",
        label: t("fields.updatedAt"),
        type: "date"
      }
    ],
    [t]
  );
};

// src/pages/users/user-list/components/user-list-table/user-list-data-table.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var UserListDataTable = () => {
  const { t } = useTranslation3();
  const { searchParams, raw } = useUserTableQuery({ pageSize: PAGE_SIZE });
  const { users, count, isPending, isError, error } = useUsers(
    {
      ...searchParams
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useUserTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: users ?? [],
    columns,
    enablePagination: true,
    count,
    pageSize: PAGE_SIZE,
    getRowId: (row) => row.id
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx2(
    _DataTable,
    {
      table,
      columns,
      count,
      pageSize: PAGE_SIZE,
      filters,
      search: true,
      pagination: true,
      isLoading: isPending,
      queryObject: raw,
      navigateTo: (row) => `${row.original.id}`,
      orderBy: [
        { key: "email", label: t("fields.email") },
        { key: "first_name", label: t("fields.firstName") },
        { key: "last_name", label: t("fields.lastName") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      noRecords: {
        message: t("users.list.empty.description")
      }
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const { t } = useTranslation3();
  return useMemo2(
    () => [
      columnHelper.accessor("email", {
        header: () => /* @__PURE__ */ jsx2("div", { className: "flex h-full w-full items-center", children: /* @__PURE__ */ jsx2("span", { children: t("fields.email") }) }),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx2("span", { className: "truncate", children: row.original.email }) });
        }
      }),
      columnHelper.accessor("first_name", {
        header: () => /* @__PURE__ */ jsx2("div", { className: "flex h-full w-full items-center", children: /* @__PURE__ */ jsx2("span", { children: t("fields.firstName") }) }),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx2("span", { className: "truncate", children: row.original.first_name || "-" }) });
        }
      }),
      columnHelper.accessor("last_name", {
        header: () => /* @__PURE__ */ jsx2("div", { className: "flex h-full w-full items-center", children: /* @__PURE__ */ jsx2("span", { children: t("fields.lastName") }) }),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx2("span", { className: "truncate", children: row.original.last_name || "-" }) });
        }
      }),
      columnHelper.accessor("created_at", {
        header: () => /* @__PURE__ */ jsx2("div", { className: "flex h-full w-full items-center", children: /* @__PURE__ */ jsx2("span", { children: t("fields.createdAt") }) }),
        cell: ({ getValue }) => {
          const date = getValue();
          return /* @__PURE__ */ jsx2(DateCell, { date: date ? new Date(date) : null });
        }
      }),
      columnHelper.accessor("updated_at", {
        header: () => /* @__PURE__ */ jsx2("div", { className: "flex h-full w-full items-center", children: /* @__PURE__ */ jsx2("span", { children: t("fields.updatedAt") }) }),
        cell: ({ getValue }) => {
          const date = getValue();
          return /* @__PURE__ */ jsx2(DateCell, { date: date ? new Date(date) : null });
        }
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2(UserActions, { user: row.original });
        }
      })
    ],
    [t]
  );
};
var UserActions = ({ user }) => {
  const { t } = useTranslation3();
  const navigate = useNavigate();
  return /* @__PURE__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(PencilSquare, {}),
              label: t("actions.edit"),
              onClick: () => {
                navigate(`${user.id}/edit`);
              }
            }
          ]
        }
      ]
    }
  );
};

// src/pages/users/user-list/components/user-list-table/user-list-table.tsx
import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
var UserListTable = ({ children }) => {
  return /* @__PURE__ */ jsx3(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx3(UserListHeader, {}),
    /* @__PURE__ */ jsx3(UserListDataTable, {})
  ] }) });
};

// src/pages/users/user-list/user-list.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx4(SingleColumnPage, { children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx4(UserListTable, {}) });
};
var UserListPage = Object.assign(Root, {
  Table: UserListTable,
  Header: UserListHeader,
  HeaderTitle: UserListTitle,
  HeaderActions: UserListActions,
  DataTable: UserListDataTable
});

export {
  UserListPage
};
