import {
  usePromotionTableColumns,
  usePromotionTableQuery
} from "./chunk-DSNCEHLY.js";
import {
  usePromotionTableFilters
} from "./chunk-S2WEYEQR.js";
import {
  SingleColumnPage
} from "./chunk-YV6MK4PZ.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useDeletePromotion,
  usePromotions
} from "./chunk-MR6REQKT.js";

// src/pages/promotions/promotion-list-page.tsx
import { Children as Children3 } from "react";
import { WidgetZone } from "@mercurjs/dashboard-shared";

// src/pages/promotions/_components/promotion-list-table/promotion-list-table.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/promotions/_components/promotion-list-table/promotion-list-header.tsx
import { Children } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Button, Heading } from "@medusajs/ui";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var PromotionListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("promotions.domain") });
};
var PromotionListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) });
};
var PromotionListActions = ({
  children
}) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-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", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(PromotionListTitle, {}),
    /* @__PURE__ */ jsx(PromotionListActions, {})
  ] }) });
};

// src/pages/promotions/_components/promotion-list-table/promotion-list-data-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { usePrompt } from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { useExtendableTable, useLinkQuery } from "@mercurjs/dashboard-shared";
import { useMemo } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
import { Outlet, useNavigate } from "react-router-dom";
import { Fragment as Fragment2, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var PromotionListDataTable = () => {
  const { t } = useTranslation2();
  const { searchParams, raw } = usePromotionTableQuery({
    pageSize: PAGE_SIZE
  });
  const linkQuery = useLinkQuery("promotion", "+status");
  const {
    promotions: data,
    count,
    isLoading,
    isError,
    error
  } = usePromotions({
    ...linkQuery,
    ...searchParams
  });
  const promotions = data?.filter((item) => item !== null);
  const baseFilters = usePromotionTableFilters();
  const { columns, filters: extFilters } = useColumns();
  const filters = useMemo(
    () => [...baseFilters, ...extFilters],
    [baseFilters, extFilters]
  );
  const { table } = useDataTable({
    data: promotions ?? [],
    columns,
    count,
    enablePagination: true,
    pageSize: PAGE_SIZE,
    getRowId: (row) => row.id
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx2(
      _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")
          }
        ]
      }
    ),
    /* @__PURE__ */ jsx2(Outlet, {})
  ] });
};
var PromotionActions = ({
  promotion
}) => {
  const { t } = useTranslation2();
  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__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/promotions/${promotion.id}/edit`
            },
            {
              icon: /* @__PURE__ */ jsx2(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = usePromotionTableColumns();
  const { columns: extended, filters } = useExtendableTable({
    model: "promotion",
    columns: base
  });
  const columns = useMemo(
    () => [
      ...extended,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2(PromotionActions, { promotion: row.original });
        }
      })
    ],
    [extended]
  );
  return { columns, filters };
};

// src/pages/promotions/_components/promotion-list-table/promotion-list-table.tsx
import { Fragment as Fragment3, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
var PromotionListTable = ({ children }) => {
  return /* @__PURE__ */ jsx3(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs3(Fragment3, { children: [
    /* @__PURE__ */ jsx3(PromotionListHeader, {}),
    /* @__PURE__ */ jsx3(PromotionListDataTable, {})
  ] }) });
};

// src/pages/promotions/promotion-list-page.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx4(SingleColumnPage, { children: /* @__PURE__ */ jsx4(WidgetZone, { id: "promotions.list", children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx4(PromotionListTable, {}) }) });
};
var PromotionListPage = Object.assign(Root, {
  Table: PromotionListTable,
  Header: PromotionListHeader,
  HeaderTitle: PromotionListTitle,
  HeaderActions: PromotionListActions,
  HeaderCreateButton: PromotionListCreateButton,
  DataTable: PromotionListDataTable
});

export {
  PromotionListPage
};
