import {
  useCollectionTableColumns
} from "./chunk-JQV7CXEC.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useCollectionTableFilters
} from "./chunk-BE5QWX5N.js";
import {
  useCollectionTableQuery
} from "./chunk-NNI6QKKZ.js";
import {
  useCollections,
  useDeleteCollection
} from "./chunk-YYTI5JIC.js";

// src/pages/collections/collection-list/collection-list.tsx
import { Children as Children3 } from "react";

// src/pages/collections/collection-list/components/collection-list-table/collection-list-table.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/collections/collection-list/components/collection-list-table/collection-list-header.tsx
import { Children } from "react";
import { Button, Heading, Text } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var CollectionListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx(Heading, { children: t("collections.domain") }),
    /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", size: "small", children: t("collections.subtitle") })
  ] });
};
var CollectionListActions = ({
  children
}) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(Link, { to: "/collections/create", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: t("actions.create") }) }) });
};
var CollectionListHeader = ({
  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(CollectionListTitle, {}),
    /* @__PURE__ */ jsx(CollectionListActions, {})
  ] }) });
};

// src/pages/collections/collection-list/components/collection-list-table/collection-list-data-table.tsx
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation3 } from "react-i18next";

// src/pages/collections/collection-list/components/collection-list-table/collection-row-actions.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { usePrompt } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2 } from "react/jsx-runtime";
var CollectionRowActions = ({
  collection
}) => {
  const { t } = useTranslation2();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteCollection(collection.id);
  const handleDeleteCollection = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("collections.deleteWarning", {
        title: collection.title
      }),
      verificationText: collection.title,
      verificationInstruction: t("general.typeToConfirm"),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync();
  };
  return /* @__PURE__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              label: t("actions.edit"),
              to: `/collections/${collection.id}/edit`,
              icon: /* @__PURE__ */ jsx2(PencilSquare, {})
            }
          ]
        },
        {
          actions: [
            {
              label: t("actions.delete"),
              onClick: handleDeleteCollection,
              icon: /* @__PURE__ */ jsx2(Trash, {}),
              disabled: !collection.id
            }
          ]
        }
      ]
    }
  );
};

// src/pages/collections/collection-list/components/collection-list-table/collection-list-data-table.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var CollectionListDataTable = () => {
  const { t } = useTranslation3();
  const { searchParams, raw } = useCollectionTableQuery({ pageSize: PAGE_SIZE });
  const { collections, count, isError, error, isLoading } = useCollections(
    {
      ...searchParams,
      fields: "+products.id"
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useCollectionTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: collections ?? [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row, index) => row.id ?? `${index}`,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx3(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      filters,
      orderBy: [
        { key: "title", label: t("fields.title") },
        { key: "handle", label: t("fields.handle") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      search: true,
      navigateTo: (row) => `/collections/${row.original.id}`,
      queryObject: raw,
      isLoading
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = useCollectionTableColumns();
  return useMemo(
    () => [
      ...base,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx3(CollectionRowActions, { collection: row.original })
      })
    ],
    [base]
  );
};

// src/pages/collections/collection-list/components/collection-list-table/collection-list-table.tsx
import { Fragment as Fragment2, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
var CollectionListTable = ({ children }) => {
  return /* @__PURE__ */ jsx4(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx4(CollectionListHeader, {}),
    /* @__PURE__ */ jsx4(CollectionListDataTable, {})
  ] }) });
};

// src/pages/collections/collection-list/collection-list.tsx
import { jsx as jsx5 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx5(SingleColumnPage, { children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx5(CollectionListTable, {}) });
};
var CollectionListPage = Object.assign(Root, {
  Table: CollectionListTable,
  Header: CollectionListHeader,
  HeaderTitle: CollectionListTitle,
  HeaderActions: CollectionListActions,
  DataTable: CollectionListDataTable
});

export {
  CollectionListPage
};
