import {
  useCustomerTableColumns
} from "./chunk-QG6PLWNH.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useCustomerTableFilters
} from "./chunk-I5THQVYP.js";
import {
  useCustomerTableQuery
} from "./chunk-PVZBDWLK.js";
import {
  useCustomers
} from "./chunk-PRBE5CM3.js";

// src/pages/customers/customer-list/customer-list.tsx
import { Children as Children2 } from "react";

// src/pages/customers/customer-list/components/customer-list-table/customer-list-table.tsx
import { PencilSquare } from "@medusajs/icons";
import { Button, Container, Heading } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo, Children } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var CustomerListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { "data-testid": "customer-list-heading", children: t("customers.domain") });
};
var CustomerListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Link, { to: "/customers/create", "data-testid": "customer-list-create-link", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "customer-list-create-button", children: t("actions.create") }) });
};
var CustomerListActions = ({ children }) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(CustomerListCreateButton, {}) });
};
var CustomerListHeader = ({ children }) => {
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "customer-list-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(CustomerListTitle, {}),
        /* @__PURE__ */ jsx(CustomerListActions, {})
      ] })
    }
  );
};
var CustomerListDataTable = () => {
  const { t } = useTranslation();
  const { searchParams, raw } = useCustomerTableQuery({ pageSize: PAGE_SIZE });
  const { customers, count, isLoading, isError, error } = useCustomers(
    {
      ...searchParams
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useCustomerTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: customers ?? [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      filters,
      orderBy: [
        { key: "email", label: t("fields.email") },
        { key: "first_name", label: t("fields.firstName") },
        { key: "last_name", label: t("fields.lastName") },
        { key: "has_account", label: t("customers.hasAccount") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      isLoading,
      navigateTo: (row) => row.original.id,
      search: true,
      queryObject: raw,
      noRecords: {
        message: t("customers.list.noRecordsMessage")
      }
    }
  );
};
var CustomerListTable = ({ children }) => {
  return /* @__PURE__ */ jsx(Container, { className: "divide-y p-0", "data-testid": "customer-list-container", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(CustomerListHeader, {}),
    /* @__PURE__ */ jsx(CustomerListDataTable, {})
  ] }) });
};
var CustomerActions = ({
  customer
}) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      "data-testid": `customer-list-row-actions-${customer.id}`,
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/customers/${customer.id}/edit`
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const columns = useCustomerTableColumns();
  return useMemo(
    () => [
      ...columns,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx(CustomerActions, { customer: row.original })
      })
    ],
    [columns]
  );
};

// src/pages/customers/customer-list/customer-list.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx2(SingleColumnPage, { children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsx2(CustomerListTable, {}) });
};
var CustomerListPage = Object.assign(Root, {
  Table: CustomerListTable,
  Header: CustomerListHeader,
  HeaderTitle: CustomerListTitle,
  HeaderActions: CustomerListActions,
  HeaderCreateButton: CustomerListCreateButton,
  DataTable: CustomerListDataTable
});

export {
  CustomerListPage
};
