import {
  useRegionTableColumns
} from "./chunk-LQAAVRET.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useRegionTableFilters
} from "./chunk-ZQZHFMD7.js";
import {
  useRegionTableQuery
} from "./chunk-7SVLKK6Q.js";
import {
  useDeleteRegion,
  useRegions
} from "./chunk-ESPLVOFC.js";

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

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

// src/pages/regions/region-list/components/region-list-view/region-list-data-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { 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 RegionListDataTable = () => {
  const { t } = useTranslation();
  const { searchParams, raw } = useRegionTableQuery({ pageSize: PAGE_SIZE });
  const {
    regions,
    count,
    isPending: isLoading,
    isError,
    error
  } = useRegions(
    {
      ...searchParams,
      fields: "*payment_providers"
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useRegionTableFilters();
  const columns = useColumns();
  const { table } = useDataTable({
    data: regions ?? [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      count,
      pageSize: PAGE_SIZE,
      isLoading,
      filters,
      orderBy: [
        { key: "name", label: t("fields.name") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      navigateTo: (row) => `${row.original.id}`,
      pagination: true,
      search: true,
      queryObject: raw,
      noRecords: {
        message: t("regions.list.noRecordsMessage")
      },
      "data-testid": "region-list-table"
    }
  );
};
var RegionActions = ({ region }) => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteRegion(region.id);
  const handleDelete = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("regions.deleteRegionWarning", {
        name: region.name
      }),
      verificationText: region.name,
      verificationInstruction: t("general.typeToConfirm"),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(t("regions.toast.delete"));
      },
      onError: (e) => {
        toast.error(e.message);
      }
    });
  };
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              label: t("actions.edit"),
              to: `/settings/regions/${region.id}/edit`,
              icon: /* @__PURE__ */ jsx(PencilSquare, {})
            }
          ]
        },
        {
          actions: [
            {
              label: t("actions.delete"),
              onClick: handleDelete,
              icon: /* @__PURE__ */ jsx(Trash, {})
            }
          ]
        }
      ],
      "data-testid": `region-list-table-action-menu-${region.id}`
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = useRegionTableColumns();
  return useMemo(
    () => [
      ...base,
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(RegionActions, { region: row.original });
        }
      })
    ],
    [base]
  );
};

// src/pages/regions/region-list/components/region-list-view/region-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 RegionListTitle = () => {
  const { t } = useTranslation2();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx2(Heading, { "data-testid": "region-list-table-heading", children: t("regions.domain") }),
    /* @__PURE__ */ jsx2(
      Text,
      {
        className: "text-ui-fg-subtle",
        size: "small",
        "data-testid": "region-list-table-subtitle",
        children: t("regions.subtitle")
      }
    )
  ] });
};
var RegionListActions = ({
  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/regions/create", children: /* @__PURE__ */ jsx2(
    Button,
    {
      size: "small",
      variant: "secondary",
      "data-testid": "region-list-table-create-button",
      children: t("actions.create")
    }
  ) }) });
};
var RegionListHeader = ({
  children
}) => {
  return /* @__PURE__ */ jsx2(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "region-list-table-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx2(RegionListTitle, {}),
        /* @__PURE__ */ jsx2(RegionListActions, {})
      ] })
    }
  );
};

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

// src/pages/regions/region-list/region-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(RegionListView, {}) });
};
var RegionListPage = Object.assign(Root, {
  Table: RegionListView,
  Header: RegionListHeader,
  HeaderTitle: RegionListTitle,
  HeaderActions: RegionListActions,
  DataTable: RegionListDataTable
});

export {
  RegionListPage
};
