import {
  useReturnReasonTableColumns
} from "./chunk-5A4JNOK3.js";
import {
  useDeleteReturnReason,
  useReturnReasons
} from "./chunk-2FGRDEAD.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useReturnReasonTableQuery
} from "./chunk-HO7Z6MC3.js";

// src/pages/return-reasons/return-reason-list/return-reason-list.tsx
import { Children } from "react";

// src/pages/return-reasons/return-reason-list/components/return-reason-list-table/return-reason-list-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { Button, Container, Heading, Text } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
import { Link } from "react-router-dom";

// src/pages/return-reasons/common/hooks/use-delete-return-reason-action.tsx
import { toast, usePrompt } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
var useDeleteReturnReasonAction = ({
  id,
  label
}) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteReturnReason(id);
  const handleDelete = async () => {
    const result = await prompt({
      title: t("general.areYouSure"),
      description: t("returnReasons.delete.confirmation", {
        label
      }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!result) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(t("returnReasons.delete.successToast", { label }));
      },
      onError: (e) => {
        toast.error(e.message);
      }
    });
  };
  return handleDelete;
};

// src/pages/return-reasons/return-reason-list/components/return-reason-list-table/return-reason-list-table.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var ReturnReasonListTable = () => {
  const { t } = useTranslation2();
  const { searchParams, raw } = useReturnReasonTableQuery({
    pageSize: PAGE_SIZE
  });
  const { return_reasons, count, isPending, isError, error } = useReturnReasons(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const columns = useColumns();
  const { table } = useDataTable({
    data: return_reasons,
    columns,
    count,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs(Container, { className: "divide-y px-0 py-0", "data-testid": "return-reason-list-table-container", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-6 py-4", "data-testid": "return-reason-list-table-header", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Heading, { "data-testid": "return-reason-list-table-heading", children: t("returnReasons.domain") }),
        /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", size: "small", "data-testid": "return-reason-list-table-subtitle", children: t("returnReasons.subtitle") })
      ] }),
      /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", asChild: true, "data-testid": "return-reason-list-table-create-button", children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) })
    ] }),
    /* @__PURE__ */ jsx(
      _DataTable,
      {
        table,
        queryObject: raw,
        count,
        isLoading: isPending,
        columns,
        pageSize: PAGE_SIZE,
        noHeader: true,
        pagination: true,
        search: true,
        "data-testid": "return-reason-list-table"
      }
    )
  ] });
};
var ReturnReasonRowActions = ({
  returnReason
}) => {
  const { t } = useTranslation2();
  const handleDelete = useDeleteReturnReasonAction(returnReason);
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `${returnReason.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ],
      "data-testid": `return-reason-list-table-action-menu-${returnReason.id}`
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = useReturnReasonTableColumns();
  return useMemo(
    () => [
      ...base,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx(ReturnReasonRowActions, { returnReason: row.original })
      })
    ],
    [base]
  );
};

// src/pages/return-reasons/return-reason-list/return-reason-list.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx2(SingleColumnPage, { showMetadata: false, showJSON: false, hasOutlet: true, children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx2(ReturnReasonListTable, {}) });
};
var ReturnReasonList = Object.assign(Root, {
  Table: ReturnReasonListTable
});

export {
  ReturnReasonList
};
