import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useCustomerGroupTableFilters
} from "./chunk-CMHI2RPL.js";
import {
  useCustomerGroupTableQuery
} from "./chunk-VKOE5J66.js";
import {
  useDate
} from "./chunk-UZWFZMOX.js";
import {
  useCustomerGroups,
  useDeleteCustomerGroupLazy
} from "./chunk-UU2RQMXS.js";

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

// src/pages/customer-groups/customer-group-list/components/customer-group-list-table/customer-group-list-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import {
  Button,
  Container,
  Heading,
  toast,
  usePrompt
} from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { Children, useCallback, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var PAGE_SIZE = 10;
var CustomerGroupListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { "data-testid": "customer-group-list-heading", children: t("customerGroups.domain") });
};
var CustomerGroupListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Link, { to: "/customer-groups/create", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: t("actions.create") }) });
};
var CustomerGroupListActions = ({ children }) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(CustomerGroupListCreateButton, {}) });
};
var CustomerGroupListHeader = ({ children }) => {
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "customer-group-list-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(CustomerGroupListTitle, {}),
        /* @__PURE__ */ jsx(CustomerGroupListActions, {})
      ] })
    }
  );
};
var CustomerGroupListDataTable = () => {
  const { t } = useTranslation();
  const { searchParams, raw } = useCustomerGroupTableQuery({ pageSize: PAGE_SIZE });
  const { customer_groups, count, isPending, isError, error } = useCustomerGroups(
    {
      ...searchParams,
      fields: "id,name,created_at,updated_at,customers.id"
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useCustomerGroupTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: customer_groups ?? [],
    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,
      isLoading: isPending,
      navigateTo: (row) => `/customer-groups/${row.original.id}`,
      search: true,
      filters,
      queryObject: raw,
      orderBy: [
        { key: "name", label: t("fields.name") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      noRecords: {
        message: t("customerGroups.list.empty.heading")
      }
    }
  );
};
var CustomerGroupListTable = ({ children }) => {
  return /* @__PURE__ */ jsx(
    Container,
    {
      className: "divide-y p-0",
      "data-testid": "customer-group-list-container",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(CustomerGroupListHeader, {}),
        /* @__PURE__ */ jsx(CustomerGroupListDataTable, {})
      ] })
    }
  );
};
var CustomerGroupActions = ({
  group
}) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync: deleteCustomerGroup } = useDeleteCustomerGroupLazy();
  const handleDelete = useCallback(async () => {
    const res = await prompt({
      title: t("customerGroups.delete.title"),
      description: t("customerGroups.delete.description", {
        name: group.name
      }),
      verificationText: group.name ?? void 0,
      verificationInstruction: t("general.typeToConfirm"),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await deleteCustomerGroup(
      { id: group.id },
      {
        onSuccess: () => {
          toast.success(
            t("customerGroups.delete.successToast", { name: group.name })
          );
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  }, [t, prompt, deleteCustomerGroup, group]);
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/customer-groups/${group.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const { t } = useTranslation();
  const { getFullDate } = useDate();
  return useMemo(
    () => [
      columnHelper.accessor("name", {
        header: t("fields.name")
      }),
      columnHelper.display({
        id: "customers",
        header: t("customers.domain"),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx("span", { children: row.original.customers?.length ?? 0 });
        }
      }),
      columnHelper.accessor("created_at", {
        header: t("fields.createdAt"),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx("span", { children: getFullDate({
            date: row.original.created_at,
            includeTime: true
          }) });
        }
      }),
      columnHelper.accessor("updated_at", {
        header: t("fields.updatedAt"),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx("span", { children: getFullDate({
            date: row.original.updated_at,
            includeTime: true
          }) });
        }
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx(CustomerGroupActions, { group: row.original })
      })
    ],
    [t, getFullDate]
  );
};

// src/pages/customer-groups/customer-group-list/customer-group-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(CustomerGroupListTable, {}) });
};
var CustomerGroupListPage = Object.assign(Root, {
  Table: CustomerGroupListTable,
  Header: CustomerGroupListHeader,
  HeaderTitle: CustomerGroupListTitle,
  HeaderActions: CustomerGroupListActions,
  HeaderCreateButton: CustomerGroupListCreateButton,
  DataTable: CustomerGroupListDataTable
});

export {
  CustomerGroupListPage
};
