import {
  usePromotionTableColumns,
  usePromotionTableQuery
} from "./chunk-UYKWOC5A.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  usePromotionTableFilters
} from "./chunk-S2WEYEQR.js";
import {
  useDeletePromotion,
  usePromotions
} from "./chunk-HAFZQK53.js";

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

// src/pages/promotions/promotion-list/components/promotion-list-table/promotion-list-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { Button, Container, Heading, usePrompt } from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { Children, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Link, Outlet, useLoaderData, useNavigate } from "react-router-dom";
import { keepPreviousData } from "@tanstack/react-query";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var PromotionListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { level: "h2", "data-testid": "promotion-list-table-heading", children: t("promotions.domain") });
};
var PromotionListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", asChild: true, "data-testid": "promotion-list-table-create-button", children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) });
};
var PromotionListActions = ({ children }) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(PromotionListCreateButton, {}) });
};
var PromotionListHeader = ({ children }) => {
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "promotion-list-table-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(PromotionListTitle, {}),
        /* @__PURE__ */ jsx(PromotionListActions, {})
      ] })
    }
  );
};
var PromotionListDataTable = () => {
  const { t } = useTranslation();
  const initialData = useLoaderData();
  const { searchParams, raw } = usePromotionTableQuery({ pageSize: PAGE_SIZE });
  const { promotions, count, isLoading, isError, error } = usePromotions(
    { ...searchParams },
    {
      initialData,
      placeholderData: keepPreviousData
    }
  );
  const filters = usePromotionTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: promotions ?? [],
    columns,
    count,
    enablePagination: true,
    pageSize: PAGE_SIZE,
    getRowId: (row) => row.id
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      count,
      pageSize: PAGE_SIZE,
      filters,
      search: true,
      pagination: true,
      isLoading,
      queryObject: raw,
      navigateTo: (row) => `${row.original.id}`,
      orderBy: [
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      "data-testid": "promotion-list-table"
    }
  );
};
var PromotionListTable = ({ children }) => {
  return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", "data-testid": "promotion-list-table-container", children: [
    Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(PromotionListHeader, {}),
      /* @__PURE__ */ jsx(PromotionListDataTable, {})
    ] }),
    /* @__PURE__ */ jsx(Outlet, {})
  ] });
};
var PromotionActions = ({ promotion }) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const navigate = useNavigate();
  const { mutateAsync } = useDeletePromotion(promotion.id);
  const handleDelete = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("promotions.deleteWarning", { code: promotion.code }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel"),
      verificationInstruction: t("general.typeToConfirm"),
      verificationText: promotion.code
    });
    if (!res) {
      return;
    }
    try {
      await mutateAsync(void 0, {
        onSuccess: () => {
          navigate("/promotions", { replace: true });
        }
      });
    } catch {
      throw new Error(
        `Promotion with code ${promotion.code} could not be deleted`
      );
    }
  };
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/promotions/${promotion.id}/edit`
            },
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ],
      "data-testid": `promotion-list-table-action-menu-${promotion.id}`
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = usePromotionTableColumns();
  return useMemo(
    () => [
      ...base,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(PromotionActions, { promotion: row.original });
        }
      })
    ],
    [base]
  );
};

// src/pages/promotions/promotion-list/promotions-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(PromotionListTable, {}) });
};
var PromotionListPage = Object.assign(Root, {
  Table: PromotionListTable,
  Header: PromotionListHeader,
  HeaderTitle: PromotionListTitle,
  HeaderActions: PromotionListActions,
  HeaderCreateButton: PromotionListCreateButton,
  DataTable: PromotionListDataTable
});

export {
  PromotionListPage
};
