import {
  useProductTableColumns
} from "./chunk-AAKR2CB4.js";
import {
  DEFAULT_FIELDS,
  useProductTableQuery
} from "./chunk-BEPAXQMU.js";
import {
  useProductTableFilters
} from "./chunk-LFX6R456.js";
import {
  SingleColumnPage
} from "./chunk-YV6MK4PZ.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useDeleteProduct,
  useProducts
} from "./chunk-YAUGB4LD.js";
import {
  sdk
} from "./chunk-DTGEFKK4.js";

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

// src/pages/products/_components/product-list-table/product-list-table.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";
import { Outlet } from "react-router-dom";

// src/pages/products/_components/product-list-table/product-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 ProductListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("products.domain") });
};
var ProductListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) });
};
var ProductListActions = ({ children }) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(ProductListCreateButton, {}) }) });
};
var ProductListHeader = ({ 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(ProductListTitle, {}),
    /* @__PURE__ */ jsx(ProductListActions, {})
  ] }) });
};

// src/pages/products/_components/product-list-table/product-list-data-table.tsx
import { toast, usePrompt } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { useExtendableTable } from "@mercurjs/dashboard-shared";
import { useMemo } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useLoaderData } from "react-router-dom";
import { PencilSquare, Tag, Trash } from "@medusajs/icons";
import { jsx as jsx2 } from "react/jsx-runtime";
var PAGE_SIZE = 10;
var ProductListDataTable = () => {
  const { t } = useTranslation2();
  const { searchParams, raw } = useProductTableQuery({
    pageSize: PAGE_SIZE
  });
  const initialData = useLoaderData();
  const { products, count, isLoading, isError, error } = useProducts(
    searchParams,
    {
      initialData,
      placeholderData: keepPreviousData
    }
  );
  const baseFilters = useProductTableFilters();
  const { columns, filters: extFilters } = useColumns();
  const filters = useMemo(
    () => [...baseFilters, ...extFilters],
    [baseFilters, extFilters]
  );
  const { table } = useDataTable({
    data: products,
    columns,
    count,
    enablePagination: true,
    pageSize: PAGE_SIZE,
    getRowId: (row) => row?.id || ""
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx2(
    _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")
        }
      ],
      defaultOrderBy: "-created_at",
      noRecords: {
        icon: /* @__PURE__ */ jsx2(Tag, { className: "text-ui-fg-subtle" }),
        title: t("products.list.noRecordsTitle"),
        message: t("products.list.noRecordsMessage"),
        action: {
          to: "create",
          label: t("actions.create")
        }
      }
    }
  );
};
var ProductActions = ({ product }) => {
  const { t } = useTranslation2();
  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__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/products/${product.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = useProductTableColumns();
  const { columns: extended, filters } = useExtendableTable(
    {
      model: "product",
      columns: base
    }
  );
  const columns = useMemo(
    () => [
      ...extended,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2(ProductActions, { product: row.original });
        }
      })
    ],
    [extended]
  );
  return { columns, filters };
};

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

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

// src/pages/products/list-loader.tsx
import { getLinkQuery } from "@mercurjs/dashboard-shared";
var productListLoader = async () => {
  const { fields } = getLinkQuery("product", DEFAULT_FIELDS);
  return sdk.vendor.products.query({
    limit: PAGE_SIZE,
    offset: 0,
    fields
  });
};

export {
  ProductListPage,
  productListLoader
};
