import {
  TextCell,
  TextHeader
} from "./chunk-JHIMVLB2.js";
import {
  useDeleteProductCategoryAction
} from "./chunk-YEUUI6JF.js";
import {
  getCategoryPath,
  getIsActiveProps,
  getIsInternalProps
} from "./chunk-Y456KVIN.js";
import {
  StatusCell
} from "./chunk-DB6ANJBO.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useQueryParams
} from "./chunk-THHRRYRS.js";
import {
  useProductCategories
} from "./chunk-Z2QQZOG3.js";

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

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

// src/pages/categories/category-list/components/category-list-table/category-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 CategoryListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx(Heading, { children: t("categories.domain") }),
    /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", size: "small", children: t("categories.subtitle") })
  ] });
};
var CategoryListActions = ({
  children
}) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx("div", { className: "flex items-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx(Link, { to: "organize", children: t("categories.organize.action") }) }),
    /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) })
  ] }) });
};
var CategoryListHeader = ({
  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(CategoryListTitle, {}),
    /* @__PURE__ */ jsx(CategoryListActions, {})
  ] }) });
};

// src/pages/categories/category-list/components/category-list-table/category-list-data-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper as createColumnHelper2 } from "@tanstack/react-table";
import { useMemo as useMemo2 } from "react";
import { useTranslation as useTranslation3 } from "react-i18next";

// src/pages/categories/category-list/components/category-list-table/use-category-table-columns.tsx
import { TriangleRightMini } from "@medusajs/icons";
import { IconButton, Text as Text2, clx } from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var useCategoryTableColumns = () => {
  const { t } = useTranslation2();
  return useMemo(
    () => [
      columnHelper.accessor("name", {
        header: () => /* @__PURE__ */ jsx2(TextHeader, { text: t("fields.name") }),
        cell: ({ getValue, row }) => {
          const expandHandler = row.getToggleExpandedHandler();
          if (row.original.parent_category !== void 0) {
            const path = getCategoryPath(row.original);
            return /* @__PURE__ */ jsx2("div", { className: "flex size-full items-center gap-1 overflow-hidden", children: path.map((chip, index) => /* @__PURE__ */ jsxs2(
              "div",
              {
                className: clx("overflow-hidden", {
                  "text-ui-fg-muted flex items-center gap-x-1": index !== path.length - 1
                }),
                children: [
                  /* @__PURE__ */ jsx2(Text2, { size: "small", leading: "compact", className: "truncate", children: chip.name }),
                  index !== path.length - 1 && /* @__PURE__ */ jsx2(Text2, { size: "small", leading: "compact", children: "/" })
                ]
              },
              chip.id
            )) });
          }
          return /* @__PURE__ */ jsxs2("div", { className: "flex size-full items-center gap-x-3 overflow-hidden", children: [
            /* @__PURE__ */ jsx2("div", { className: "flex size-7 items-center justify-center", children: row.getCanExpand() ? /* @__PURE__ */ jsx2(
              IconButton,
              {
                type: "button",
                onClick: (e) => {
                  e.stopPropagation();
                  e.preventDefault();
                  expandHandler();
                },
                size: "small",
                variant: "transparent",
                className: "text-ui-fg-subtle",
                children: /* @__PURE__ */ jsx2(
                  TriangleRightMini,
                  {
                    className: clx({
                      "rotate-90 transition-transform will-change-transform": row.getIsExpanded()
                    })
                  }
                )
              }
            ) : null }),
            /* @__PURE__ */ jsx2("span", { className: "truncate", children: getValue() })
          ] });
        }
      }),
      columnHelper.accessor("handle", {
        header: () => /* @__PURE__ */ jsx2(TextHeader, { text: t("fields.handle") }),
        cell: ({ getValue }) => {
          return /* @__PURE__ */ jsx2(TextCell, { text: `/${getValue()}` });
        }
      }),
      columnHelper.accessor("is_active", {
        header: () => /* @__PURE__ */ jsx2(TextHeader, { text: t("fields.status") }),
        cell: ({ getValue }) => {
          const { color, label } = getIsActiveProps(getValue(), t);
          return /* @__PURE__ */ jsx2(StatusCell, { color, children: label });
        }
      }),
      columnHelper.accessor("is_internal", {
        header: () => /* @__PURE__ */ jsx2(TextHeader, { text: t("categories.fields.visibility.label") }),
        cell: ({ getValue }) => {
          const { color, label } = getIsInternalProps(getValue(), t);
          return /* @__PURE__ */ jsx2(StatusCell, { color, children: label });
        }
      })
    ],
    [t]
  );
};

// src/pages/categories/category-list/components/category-list-table/use-category-table-query.tsx
var useCategoryTableQuery = ({
  pageSize = 20,
  prefix
}) => {
  const raw = useQueryParams(["q", "offset", "order"], prefix);
  const searchParams = {
    q: raw.q,
    limit: pageSize,
    offset: raw.offset ? Number(raw.offset) : 0,
    order: raw.order
  };
  return {
    raw,
    searchParams
  };
};

// src/pages/categories/category-list/components/category-list-table/category-list-data-table.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var CategoryListDataTable = () => {
  const { raw, searchParams } = useCategoryTableQuery({ pageSize: PAGE_SIZE });
  const query = raw.q ? {
    include_ancestors_tree: true,
    fields: "id,name,handle,is_active,is_internal,parent_category",
    ...searchParams
  } : {
    include_descendants_tree: true,
    parent_category_id: "null",
    fields: "id,name,category_children,handle,is_internal,is_active",
    ...searchParams
  };
  const { product_categories, count, isLoading, isError, error } = useProductCategories(
    {
      ...query
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const columns = useColumns();
  const { table } = useDataTable({
    data: product_categories || [],
    columns,
    count,
    getRowId: (original) => original.id,
    getSubRows: (original) => original.category_children,
    enableExpandableRows: true,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx3(
    _DataTable,
    {
      table,
      columns,
      count,
      pageSize: PAGE_SIZE,
      isLoading,
      navigateTo: (row) => row.id,
      queryObject: raw,
      search: true,
      pagination: true
    }
  );
};
var CategoryRowActions = ({
  category
}) => {
  const { t } = useTranslation3();
  const handleDelete = useDeleteProductCategoryAction(category);
  return /* @__PURE__ */ jsx3(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              label: t("actions.edit"),
              icon: /* @__PURE__ */ jsx3(PencilSquare, {}),
              to: `${category.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              label: t("actions.delete"),
              icon: /* @__PURE__ */ jsx3(Trash, {}),
              onClick: handleDelete
            }
          ]
        }
      ]
    }
  );
};
var columnHelper2 = createColumnHelper2();
var useColumns = () => {
  const base = useCategoryTableColumns();
  return useMemo2(
    () => [
      ...base,
      columnHelper2.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx3(CategoryRowActions, { category: row.original });
        }
      })
    ],
    [base]
  );
};

// src/pages/categories/category-list/components/category-list-table/category-list-table.tsx
import { Fragment as Fragment2, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
var CategoryListTable = ({ children }) => {
  return /* @__PURE__ */ jsx4(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs3(Fragment2, { children: [
    /* @__PURE__ */ jsx4(CategoryListHeader, {}),
    /* @__PURE__ */ jsx4(CategoryListDataTable, {})
  ] }) });
};

// src/pages/categories/category-list/category-list.tsx
import { jsx as jsx5 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx5(SingleColumnPage, { hasOutlet: true, children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx5(CategoryListTable, {}) });
};
var CategoryListPage = Object.assign(Root, {
  Table: CategoryListTable,
  Header: CategoryListHeader,
  HeaderTitle: CategoryListTitle,
  HeaderActions: CategoryListActions,
  DataTable: CategoryListDataTable
});

export {
  CategoryListPage
};
