import {
  useDeletePriceListAction
} from "./chunk-ZLBTDKSI.js";
import {
  getPriceListStatus
} from "./chunk-U5MJIHLY.js";
import {
  TextCell,
  TextHeader
} from "./chunk-JHIMVLB2.js";
import {
  StatusCell
} from "./chunk-DB6ANJBO.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useQueryParams
} from "./chunk-THHRRYRS.js";
import {
  usePriceLists
} from "./chunk-K4BGK3CO.js";

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

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

// src/pages/price-lists/price-list-list/components/price-list-list-table/price-list-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 PriceListListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx(Heading, { children: t("priceLists.domain") }),
    /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", size: "small", children: t("priceLists.subtitle") })
  ] });
};
var PriceListListActions = ({
  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(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) }) });
};
var PriceListListHeader = ({
  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(PriceListListTitle, {}),
    /* @__PURE__ */ jsx(PriceListListActions, {})
  ] }) });
};

// src/pages/price-lists/price-list-list/components/price-list-list-table/price-list-list-data-table.tsx
import { keepPreviousData } from "@tanstack/react-query";
import { useTranslation as useTranslation5 } from "react-i18next";

// src/pages/price-lists/price-list-list/components/price-list-list-table/use-pricing-table-columns.tsx
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation3 } from "react-i18next";

// src/pages/price-lists/price-list-list/components/price-list-list-table/price-list-list-table-actions.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2 } from "react/jsx-runtime";
var PriceListListTableActions = ({ priceList }) => {
  const { t } = useTranslation2();
  const handleDelete = useDeletePriceListAction({ priceList, navigateOnSuccess: false });
  return /* @__PURE__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              label: t("actions.edit"),
              to: `${priceList.id}/edit`,
              icon: /* @__PURE__ */ jsx2(PencilSquare, {})
            }
          ]
        },
        {
          actions: [
            {
              label: t("actions.delete"),
              onClick: handleDelete,
              icon: /* @__PURE__ */ jsx2(Trash, {})
            }
          ]
        }
      ]
    }
  );
};

// src/pages/price-lists/price-list-list/components/price-list-list-table/use-pricing-table-columns.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var usePricingTableColumns = () => {
  const { t } = useTranslation3();
  return useMemo(
    () => [
      columnHelper.accessor("title", {
        header: () => /* @__PURE__ */ jsx3(TextHeader, { text: t("fields.title") }),
        cell: (info) => info.getValue()
      }),
      columnHelper.accessor("status", {
        header: t("priceLists.fields.status.label"),
        cell: ({ row }) => {
          const { color, text } = getPriceListStatus(t, row.original);
          return /* @__PURE__ */ jsx3(StatusCell, { color, children: text });
        }
      }),
      columnHelper.accessor("prices", {
        header: t("priceLists.fields.priceOverrides.header"),
        cell: (info) => /* @__PURE__ */ jsx3(TextCell, { text: `${info.getValue()?.length || "-"}` })
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx3(PriceListListTableActions, { priceList: row.original })
      })
    ],
    [t]
  );
};

// src/pages/price-lists/price-list-list/components/price-list-list-table/use-pricing-table-filters.tsx
import { useTranslation as useTranslation4 } from "react-i18next";
var usePricingTableFilters = () => {
  const { t } = useTranslation4();
  const filters = [
    { label: t("fields.createdAt"), key: "starts_at", type: "date" },
    { label: t("fields.updatedAt"), key: "ends_at", type: "date" }
  ];
  return filters;
};

// src/pages/price-lists/price-list-list/components/price-list-list-table/use-pricing-table-query.tsx
var usePricingTableQuery = ({
  pageSize = 20,
  prefix
}) => {
  const raw = useQueryParams(
    ["offset", "q", "order", "status", "starts_at", "ends_at"],
    prefix
  );
  const searchParams = {
    limit: pageSize,
    offset: raw.offset ? Number(raw.offset) : 0,
    order: raw.order,
    status: raw.status?.split(","),
    starts_at: raw.starts_at ? JSON.parse(raw.starts_at) : void 0,
    ends_at: raw.ends_at ? JSON.parse(raw.ends_at) : void 0,
    q: raw.q
  };
  return {
    searchParams,
    raw
  };
};

// src/pages/price-lists/price-list-list/components/price-list-list-table/price-list-list-data-table.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var PriceListListDataTable = () => {
  const { t } = useTranslation5();
  const { searchParams, raw } = usePricingTableQuery({
    pageSize: PAGE_SIZE
  });
  const { price_lists, count, isLoading, isError, error } = usePriceLists(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = usePricingTableFilters();
  const columns = usePricingTableColumns();
  const { table } = useDataTable({
    data: price_lists || [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx4(
    _DataTable,
    {
      table,
      columns,
      count,
      filters,
      orderBy: [
        { key: "title", label: t("fields.title") },
        { key: "status", label: t("fields.status") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      queryObject: raw,
      pageSize: PAGE_SIZE,
      navigateTo: (row) => row.original.id,
      isLoading,
      pagination: true,
      search: true
    }
  );
};

// src/pages/price-lists/price-list-list/components/price-list-list-table/price-list-list-table.tsx
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
var PriceListListTable = ({ children }) => {
  return /* @__PURE__ */ jsx5(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx5(PriceListListHeader, {}),
    /* @__PURE__ */ jsx5(PriceListListDataTable, {})
  ] }) });
};

// src/pages/price-lists/price-list-list/price-list-list.tsx
import { jsx as jsx6 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx6(SingleColumnPage, { children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx6(PriceListListTable, {}) });
};
var PriceListListPage = Object.assign(Root, {
  Table: PriceListListTable,
  Header: PriceListListHeader,
  HeaderTitle: PriceListListTitle,
  HeaderActions: PriceListListActions,
  DataTable: PriceListListDataTable
});

export {
  PriceListListPage
};
