import {
  useProductTableColumns
} from "./chunk-Z5I2Z6L3.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useProductTableFilters
} from "./chunk-LNAXUDW2.js";
import {
  useProductTableQuery
} from "./chunk-VVWB3GIK.js";
import {
  useDeleteProduct,
  useProducts
} from "./chunk-5CK6XEFV.js";

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

// src/pages/products/product-list/components/product-list-table/product-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 { useMemo, Children } from "react";
import { useTranslation } from "react-i18next";
import { Link, Outlet, useLoaderData, useLocation } from "react-router-dom";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var ProductListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { level: "h2", "data-testid": "products-list-title", children: t("products.domain") });
};
var ProductListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(
    Button,
    {
      size: "small",
      variant: "secondary",
      asChild: true,
      "data-testid": "products-create-button",
      children: /* @__PURE__ */ jsx(Link, { to: "create", "data-testid": "products-create-link", children: t("actions.create") })
    }
  );
};
var ProductListExportButton = () => {
  const { t } = useTranslation();
  const location = useLocation();
  return /* @__PURE__ */ jsx(
    Button,
    {
      size: "small",
      variant: "secondary",
      asChild: true,
      "data-testid": "products-export-button",
      children: /* @__PURE__ */ jsx(
        Link,
        {
          to: `export${location.search}`,
          "data-testid": "products-export-link",
          children: t("actions.export")
        }
      )
    }
  );
};
var ProductListImportButton = () => {
  const { t } = useTranslation();
  const location = useLocation();
  return /* @__PURE__ */ jsx(
    Button,
    {
      size: "small",
      variant: "secondary",
      asChild: true,
      "data-testid": "products-import-button",
      children: /* @__PURE__ */ jsx(
        Link,
        {
          to: `import${location.search}`,
          "data-testid": "products-import-link",
          children: t("actions.import")
        }
      )
    }
  );
};
var ProductListActions = ({ children }) => {
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex items-center justify-center gap-x-2",
      "data-testid": "products-list-actions",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(ProductListExportButton, {}),
        /* @__PURE__ */ jsx(ProductListImportButton, {}),
        /* @__PURE__ */ jsx(ProductListCreateButton, {})
      ] })
    }
  );
};
var ProductListHeader = ({ children }) => {
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "products-list-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(ProductListTitle, {}),
        /* @__PURE__ */ jsx(ProductListActions, {})
      ] })
    }
  );
};
var ProductListDataTable = () => {
  const { t } = useTranslation();
  const initialData = useLoaderData();
  const { searchParams, raw } = useProductTableQuery({ pageSize: PAGE_SIZE });
  const { products, count, isLoading, isError, error } = useProducts(
    {
      ...searchParams,
      is_giftcard: false
    },
    {
      initialData,
      placeholderData: keepPreviousData
    }
  );
  const filters = useProductTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: products ?? [],
    columns,
    count,
    enablePagination: true,
    pageSize: PAGE_SIZE,
    getRowId: (row) => row.id
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx("div", { "data-testid": "products-data-table", children: /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      count,
      pageSize: PAGE_SIZE,
      filters,
      search: true,
      pagination: true,
      isLoading,
      queryObject: raw,
      navigateTo: (row) => `${row.original.id}`,
      orderBy: [
        { key: "title", label: t("fields.title") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      noRecords: {
        message: t("products.list.noRecordsMessage")
      }
    }
  ) });
};
var ProductListTable = ({ children }) => {
  return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", "data-testid": "products-list-table", children: [
    Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(ProductListHeader, {}),
      /* @__PURE__ */ jsx(ProductListDataTable, {})
    ] }),
    /* @__PURE__ */ jsx(Outlet, {})
  ] });
};
var ProductActions = ({ product }) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteProduct(product.id);
  const handleDelete = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("products.deleteWarning", {
        title: product.title
      }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(t("products.toasts.delete.success.header"), {
          description: t("products.toasts.delete.success.description", {
            title: product.title
          })
        });
      },
      onError: (e) => {
        toast.error(t("products.toasts.delete.error.header"), {
          description: e.message
        });
      }
    });
  };
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/products/${product.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ],
      "data-testid": `product-actions-${product.id}`
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const { t } = useTranslation();
  const base = useProductTableColumns();
  const columns = useMemo(
    () => [
      ...base,
      columnHelper.display({
        id: "seller",
        header: t("store.domain"),
        cell: ({ row }) => {
          const seller = row.original.seller;
          return seller?.name || "-";
        }
      }),
      columnHelper.display({
        id: "actions",
        header: () => /* @__PURE__ */ jsx(
          "div",
          {
            className: "flex h-full w-full items-center",
            "data-testid": "products-table-header-actions",
            children: /* @__PURE__ */ jsx("span", { "data-testid": "products-table-header-actions-text" })
          }
        ),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(ProductActions, { product: row.original });
        }
      })
    ],
    [base, t]
  );
  return columns;
};

// src/pages/products/product-list/product-list.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx2("div", { "data-testid": "products-page", children: /* @__PURE__ */ jsx2(SingleColumnPage, { children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsx2(ProductListTable, {}) }) });
};
var ProductListPage = Object.assign(Root, {
  Table: ProductListTable,
  Header: ProductListHeader,
  HeaderTitle: ProductListTitle,
  HeaderActions: ProductListActions,
  HeaderCreateButton: ProductListCreateButton,
  HeaderExportButton: ProductListExportButton,
  HeaderImportButton: ProductListImportButton,
  DataTable: ProductListDataTable
});

export {
  ProductListPage
};
