import {
  useCampaignTableColumns
} from "./chunk-7TIZP4YG.js";
import {
  useCampaignTableQuery
} from "./chunk-DY3XQPHC.js";
import {
  SingleColumnPage
} from "./chunk-YV6MK4PZ.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useCampaigns,
  useDeleteCampaign
} from "./chunk-MR6REQKT.js";

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

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

// src/pages/campaigns/_components/campaign-list-table/campaign-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 CampaignListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("campaigns.domain") });
};
var CampaignListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Link, { to: "/campaigns/create", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: t("actions.create") }) });
};
var CampaignListActions = ({
  children
}) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-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", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(CampaignListTitle, {}),
    /* @__PURE__ */ jsx(CampaignListActions, {})
  ] }) });
};

// src/pages/campaigns/_components/campaign-list-table/campaign-list-data-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { toast, usePrompt } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
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 { jsx as jsx2 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var CampaignListDataTable = () => {
  const { t } = useTranslation2();
  const { raw, searchParams } = useCampaignTableQuery({ pageSize: PAGE_SIZE });
  const linkQuery = useLinkQuery("campaign");
  const {
    campaigns,
    count,
    isPending: isLoading,
    isError,
    error
  } = useCampaigns(
    { ...searchParams, ...linkQuery },
    {
      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__ */ jsx2(
    _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") }
      ]
    }
  );
};
var CampaignActions = ({ campaign }) => {
  const { t } = useTranslation2();
  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__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/campaigns/${campaign.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = useCampaignTableColumns();
  const { columns: extended, filters } = useExtendableTable({
    model: "campaign",
    columns: base
  });
  const columns = useMemo(
    () => [
      ...extended,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2(CampaignActions, { campaign: row.original });
        }
      })
    ],
    [extended]
  );
  return { columns, filters };
};

// src/pages/campaigns/_components/campaign-list-table/campaign-list-table.tsx
import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
var CampaignListTable = ({ children }) => {
  return /* @__PURE__ */ jsx3(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx3(CampaignListHeader, {}),
    /* @__PURE__ */ jsx3(CampaignListDataTable, {})
  ] }) });
};

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

export {
  CampaignListPage
};
