import {
  TextCell,
  TextHeader
} from "./chunk-JHIMVLB2.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useCommissionRates,
  useDeleteCommissionRate
} from "./chunk-OHOWEMHO.js";

// src/pages/commission-rates/commission-rate-list/commission-rate-list.tsx
import { Children as Children3 } from "react";

// src/pages/commission-rates/commission-rate-list/components/commission-rate-list-view/commission-rate-list-view.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/commission-rates/commission-rate-list/components/commission-rate-list-view/commission-rate-list-data-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import {
  Badge,
  StatusBadge,
  toast,
  usePrompt
} from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { jsx } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var CommissionRateListDataTable = () => {
  const { t } = useTranslation();
  const { commission_rates, count, isPending: isLoading, isError, error } = useCommissionRates(
    {
      limit: PAGE_SIZE,
      offset: 0,
      fields: "*rules"
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const columns = useColumns();
  const { table } = useDataTable({
    data: commission_rates ?? [],
    columns,
    count: count ?? 0,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      count: count ?? 0,
      pageSize: PAGE_SIZE,
      isLoading,
      navigateTo: (row) => `${row.original.id}`,
      pagination: true,
      search: true,
      orderBy: [
        { key: "name", label: t("commissionRates.fields.name") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ]
    }
  );
};
var CommissionRateActions = ({
  commissionRate
}) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteCommissionRate(commissionRate.id);
  const handleDelete = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("commissionRates.delete.description", {
        name: commissionRate.name
      }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(t("commissionRates.delete.successToast"));
      },
      onError: (e) => {
        toast.error(e.message);
      }
    });
  };
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              label: t("actions.edit"),
              to: `/settings/commission-rates/${commissionRate.id}/edit`,
              icon: /* @__PURE__ */ jsx(PencilSquare, {})
            }
          ]
        },
        {
          actions: [
            {
              label: t("actions.delete"),
              onClick: handleDelete,
              icon: /* @__PURE__ */ jsx(Trash, {})
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const { t } = useTranslation();
  return useMemo(
    () => [
      columnHelper.accessor("code", {
        header: t("commissionRates.fields.code"),
        cell: ({ getValue }) => /* @__PURE__ */ jsx(Badge, { size: "2xsmall", className: "uppercase", children: getValue() })
      }),
      columnHelper.accessor("type", {
        header: () => /* @__PURE__ */ jsx(TextHeader, { text: t("commissionRates.fields.type.label") }),
        cell: ({ getValue }) => {
          const type = getValue();
          return /* @__PURE__ */ jsx(
            TextCell,
            {
              text: type === "percentage" ? t("commissionRates.fields.type.percentage") : t("commissionRates.fields.type.fixed")
            }
          );
        }
      }),
      columnHelper.accessor("is_enabled", {
        header: t("fields.status"),
        cell: ({ getValue }) => {
          const enabled = getValue();
          return /* @__PURE__ */ jsx(StatusBadge, { color: enabled ? "green" : "grey", children: enabled ? t("commissionRates.status.enabled") : t("commissionRates.status.disabled") });
        }
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx(CommissionRateActions, { commissionRate: row.original })
      })
    ],
    [t]
  );
};

// src/pages/commission-rates/commission-rate-list/components/commission-rate-list-view/commission-rate-list-header.tsx
import { Children } from "react";
import { Button, Heading, Text } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
var CommissionRateListTitle = () => {
  const { t } = useTranslation2();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx2(Heading, { children: t("commissionRates.domain") }),
    /* @__PURE__ */ jsx2(Text, { className: "text-ui-fg-subtle", size: "small", children: t("commissionRates.description") })
  ] });
};
var CommissionRateListActions = ({
  children
}) => {
  const { t } = useTranslation2();
  return /* @__PURE__ */ jsx2("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx2(Link, { to: "/settings/commission-rates/create", children: /* @__PURE__ */ jsx2(Button, { size: "small", variant: "secondary", children: t("actions.create") }) }) });
};
var CommissionRateListHeader = ({
  children
}) => {
  return /* @__PURE__ */ jsx2("div", { className: "flex items-center justify-between px-6 py-4", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx2(CommissionRateListTitle, {}),
    /* @__PURE__ */ jsx2(CommissionRateListActions, {})
  ] }) });
};

// src/pages/commission-rates/commission-rate-list/components/commission-rate-list-view/commission-rate-list-view.tsx
import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
var CommissionRateListView = ({
  children
}) => {
  return /* @__PURE__ */ jsx3(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx3(CommissionRateListHeader, {}),
    /* @__PURE__ */ jsx3(CommissionRateListDataTable, {})
  ] }) });
};

// src/pages/commission-rates/commission-rate-list/commission-rate-list.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx4(SingleColumnPage, { children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx4(CommissionRateListView, {}) });
};
var CommissionRateListPage = Object.assign(Root, {
  Table: CommissionRateListView,
  Header: CommissionRateListHeader,
  HeaderTitle: CommissionRateListTitle,
  HeaderActions: CommissionRateListActions,
  DataTable: CommissionRateListDataTable
});

export {
  CommissionRateListPage
};
