import {
  useCampaignTableColumns
} from "./chunk-HINXQTCW.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useCampaignTableQuery
} from "./chunk-DY3XQPHC.js";
import {
  useCampaigns,
  useDeleteCampaign
} from "./chunk-HAFZQK53.js";

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

// src/pages/campaigns/campaign-list/components/campaign-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, 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 = 20;
var CampaignListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { level: "h2", "data-testid": "campaign-list-table-heading", children: t("campaigns.domain") });
};
var CampaignListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Link, { to: "/campaigns/create", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "campaign-list-table-create-button", children: t("actions.create") }) });
};
var CampaignListActions = ({ children }) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(CampaignListCreateButton, {}) });
};
var CampaignListHeader = ({ children }) => {
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "campaign-list-table-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(CampaignListTitle, {}),
        /* @__PURE__ */ jsx(CampaignListActions, {})
      ] })
    }
  );
};
var CampaignListDataTable = () => {
  const { t } = useTranslation();
  const { raw, searchParams } = useCampaignTableQuery({ pageSize: PAGE_SIZE });
  const {
    campaigns,
    count,
    isPending: isLoading,
    isError,
    error
  } = useCampaigns(searchParams, {
    placeholderData: keepPreviousData
  });
  const columns = useColumns();
  const { table } = useDataTable({
    data: campaigns ?? [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      count,
      pageSize: PAGE_SIZE,
      pagination: true,
      search: true,
      navigateTo: (row) => row.id,
      isLoading,
      queryObject: raw,
      orderBy: [
        { key: "name", label: t("fields.name") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      "data-testid": "campaign-list-table"
    }
  );
};
var CampaignListTable = ({ children }) => {
  return /* @__PURE__ */ jsx(Container, { className: "divide-y p-0", "data-testid": "campaign-list-table-container", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(CampaignListHeader, {}),
    /* @__PURE__ */ jsx(CampaignListDataTable, {})
  ] }) });
};
var CampaignActions = ({ campaign }) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteCampaign(campaign.id);
  const handleDelete = async () => {
    const confirm = await prompt({
      title: t("general.areYouSure"),
      description: t("campaigns.deleteCampaignWarning", {
        name: campaign.name
      }),
      verificationInstruction: t("general.typeToConfirm"),
      verificationText: campaign.name,
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!confirm) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(
          t("campaigns.delete.successToast", { name: campaign.name })
        );
      },
      onError: (e) => {
        toast.error(e.message);
      }
    });
  };
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/campaigns/${campaign.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ],
      "data-testid": `campaign-list-table-action-menu-${campaign.id}`
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = useCampaignTableColumns();
  return useMemo(
    () => [
      ...base,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(CampaignActions, { campaign: row.original });
        }
      })
    ],
    [base]
  );
};

// src/pages/campaigns/campaign-list/campaign-list.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx2(SingleColumnPage, { hasOutlet: true, children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsx2(CampaignListTable, {}) });
};
var CampaignListPage = Object.assign(Root, {
  Table: CampaignListTable,
  Header: CampaignListHeader,
  HeaderTitle: CampaignListTitle,
  HeaderActions: CampaignListActions,
  HeaderCreateButton: CampaignListCreateButton,
  DataTable: CampaignListDataTable
});

export {
  CampaignListPage
};
