import {
  SingleColumnPage
} from "./chunk-YV6MK4PZ.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import {
  useQueryParams
} from "./chunk-THHRRYRS.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useDeleteShippingProfile,
  useShippingProfiles
} from "./chunk-EPSDO2MJ.js";

// src/pages/settings/shipping-profiles/shipping-profile-list-page.tsx
import { Children as Children3 } from "react";
import { WidgetZone } from "@mercurjs/dashboard-shared";

// src/pages/settings/shipping-profiles/_components/shipping-profile-list-table.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/settings/shipping-profiles/_components/shipping-profile-list-header.tsx
import { Children } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Button, Heading, Text } from "@medusajs/ui";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var ShippingProfileListTitle = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx(Heading, { children: t("shippingProfile.domain") }),
    /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", size: "small", children: t("shippingProfile.subtitle") })
  ] });
};
var ShippingProfileListCreateButton = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx(Link, { to: "create", children: t("actions.create") }) });
};
var ShippingProfileListActions = ({
  children
}) => {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(ShippingProfileListCreateButton, {}) });
};
var ShippingProfileListHeader = ({
  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(ShippingProfileListTitle, {}),
    /* @__PURE__ */ jsx(ShippingProfileListActions, {})
  ] }) });
};

// src/pages/settings/shipping-profiles/_components/shipping-profile-list-data-table.tsx
import { keepPreviousData } from "@tanstack/react-query";
import { useTranslation as useTranslation5 } from "react-i18next";

// src/pages/settings/shipping-profiles/_components/use-shipping-profile-table-columns.tsx
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation3 } from "react-i18next";

// src/pages/settings/shipping-profiles/_components/shipping-options-row-actions.tsx
import { Trash } from "@medusajs/icons";
import { toast, usePrompt } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2 } from "react/jsx-runtime";
var ShippingOptionsRowActions = ({
  profile
}) => {
  const { t } = useTranslation2();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteShippingProfile(profile.id);
  const handleDelete = async () => {
    const res = await prompt({
      title: t("shippingProfile.delete.title"),
      description: t("shippingProfile.delete.description", {
        name: profile.name
      }),
      verificationText: profile.name,
      verificationInstruction: t("general.typeToConfirm"),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(
          t("shippingProfile.delete.successToast", {
            name: profile.name
          })
        );
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  };
  return /* @__PURE__ */ jsx2(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx2(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ]
    }
  );
};

// src/pages/settings/shipping-profiles/_components/use-shipping-profile-table-columns.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var useShippingProfileTableColumns = () => {
  const { t } = useTranslation3();
  return useMemo(
    () => [
      columnHelper.accessor("name", {
        header: t("fields.name"),
        cell: (cell) => cell.getValue()
      }),
      columnHelper.accessor("type", {
        header: t("fields.type"),
        cell: (cell) => cell.getValue()
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx3(ShippingOptionsRowActions, { profile: row.original })
      })
    ],
    [t]
  );
};

// src/pages/settings/shipping-profiles/_components/use-shipping-profile-table-filters.tsx
import { useTranslation as useTranslation4 } from "react-i18next";
var useShippingProfileTableFilters = () => {
  const { t } = useTranslation4();
  let filters = [];
  filters.push({
    key: "name",
    label: t("fields.name"),
    type: "string"
  });
  filters.push({
    key: "type",
    label: t("fields.type"),
    type: "string"
  });
  const dateFilters = [
    { label: t("fields.createdAt"), key: "created_at" },
    { label: t("fields.updatedAt"), key: "updated_at" }
  ].map((f) => ({
    key: f.key,
    label: f.label,
    type: "date"
  }));
  filters = [...filters, ...dateFilters];
  return filters;
};

// src/pages/settings/shipping-profiles/_components/use-shipping-profile-table-query.tsx
var useShippingProfileTableQuery = ({
  pageSize = 20,
  prefix
}) => {
  const raw = useQueryParams(
    ["offset", "q", "order", "created_at", "updated_at", "name", "type"],
    prefix
  );
  const searchParams = {
    limit: pageSize,
    offset: raw.offset ? parseInt(raw.offset) : 0,
    q: raw.q,
    order: raw.order,
    created_at: raw.created_at ? JSON.parse(raw.created_at) : void 0,
    updated_at: raw.updated_at ? JSON.parse(raw.updated_at) : void 0,
    name: raw.name,
    type: raw.type
  };
  return {
    searchParams,
    raw
  };
};

// src/pages/settings/shipping-profiles/_components/shipping-profile-list-data-table.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var ShippingProfileListDataTable = () => {
  const { t } = useTranslation5();
  const { raw, searchParams } = useShippingProfileTableQuery({
    pageSize: PAGE_SIZE
  });
  const { shipping_profiles, count, isLoading, isError, error } = useShippingProfiles(searchParams, {
    placeholderData: keepPreviousData
  });
  const columns = useShippingProfileTableColumns();
  const filters = useShippingProfileTableFilters();
  const { table } = useDataTable({
    data: shipping_profiles ?? [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row, index) => row?.id ?? `row-${index}`,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx4(
    _DataTable,
    {
      table,
      pageSize: PAGE_SIZE,
      count,
      columns,
      filters,
      orderBy: [
        { key: "name", label: t("fields.name") },
        { key: "type", label: t("fields.type") },
        {
          key: "created_at",
          label: t("fields.createdAt")
        },
        {
          key: "updated_at",
          label: t("fields.updatedAt")
        }
      ],
      isLoading,
      navigateTo: (row) => row.id,
      queryObject: raw,
      search: true,
      pagination: true
    }
  );
};

// src/pages/settings/shipping-profiles/_components/shipping-profile-list-table.tsx
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
var ShippingProfileListTable = ({ children }) => {
  return /* @__PURE__ */ jsx5(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx5(ShippingProfileListHeader, {}),
    /* @__PURE__ */ jsx5(ShippingProfileListDataTable, {})
  ] }) });
};

// src/pages/settings/shipping-profiles/shipping-profile-list-page.tsx
import { jsx as jsx6 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx6(SingleColumnPage, { hasOutlet: true, children: /* @__PURE__ */ jsx6(WidgetZone, { id: "shipping-profiles.list", children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx6(ShippingProfileListTable, {}) }) });
};
var ShippingProfileListPage = Object.assign(Root, {
  Table: ShippingProfileListTable,
  Header: ShippingProfileListHeader,
  HeaderTitle: ShippingProfileListTitle,
  HeaderActions: ShippingProfileListActions,
  HeaderCreateButton: ShippingProfileListCreateButton,
  DataTable: ShippingProfileListDataTable
});

export {
  ShippingProfileListPage
};
