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 {
  useDeleteShippingProfile,
  useShippingProfiles
} from "./chunk-DF3OGJAK.js";

// src/pages/shipping-profiles/shipping-profiles-list/shipping-profile-list.tsx
import { Children as Children3 } from "react";

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

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

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

// src/pages/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-table/shipping-options-row-actions.tsx
import { Trash } from "@medusajs/icons";
import { toast, usePrompt } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { jsx } from "react/jsx-runtime";
var ShippingOptionsRowActions = ({
  profile
}) => {
  const { t } = useTranslation();
  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__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete
            }
          ]
        }
      ],
      "data-testid": `shipping-profile-list-table-action-menu-${profile.id}`
    }
  );
};

// src/pages/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-table/use-shipping-profile-table-columns.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var useShippingProfileTableColumns = () => {
  const { t } = useTranslation2();
  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__ */ jsx2(ShippingOptionsRowActions, { profile: row.original })
      })
    ],
    [t]
  );
};

// src/pages/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-table/use-shipping-profile-table-filters.tsx
import { useTranslation as useTranslation3 } from "react-i18next";
var useShippingProfileTableFilters = () => {
  const { t } = useTranslation3();
  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/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-table/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/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-view/shipping-profile-list-data-table.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var ShippingProfileListDataTable = () => {
  const { t } = useTranslation4();
  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) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx3(
    _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,
      "data-testid": "shipping-profile-list-table"
    }
  );
};

// src/pages/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-view/shipping-profile-list-header.tsx
import { Children } from "react";
import { Button, Heading, Text } from "@medusajs/ui";
import { useTranslation as useTranslation5 } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx as jsx4, jsxs } from "react/jsx-runtime";
var ShippingProfileListTitle = () => {
  const { t } = useTranslation5();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx4(Heading, { "data-testid": "shipping-profile-list-table-heading", children: t("shippingProfile.domain") }),
    /* @__PURE__ */ jsx4(
      Text,
      {
        className: "text-ui-fg-subtle",
        size: "small",
        "data-testid": "shipping-profile-list-table-subtitle",
        children: t("shippingProfile.subtitle")
      }
    )
  ] });
};
var ShippingProfileListActions = ({
  children
}) => {
  const { t } = useTranslation5();
  return /* @__PURE__ */ jsx4("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx4(
    Button,
    {
      size: "small",
      variant: "secondary",
      asChild: true,
      "data-testid": "shipping-profile-list-table-create-button",
      children: /* @__PURE__ */ jsx4(Link, { to: "create", children: t("actions.create") })
    }
  ) });
};
var ShippingProfileListHeader = ({
  children
}) => {
  return /* @__PURE__ */ jsx4(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": "shipping-profile-list-table-header",
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx4(ShippingProfileListTitle, {}),
        /* @__PURE__ */ jsx4(ShippingProfileListActions, {})
      ] })
    }
  );
};

// src/pages/shipping-profiles/shipping-profiles-list/components/shipping-profile-list-view/shipping-profile-list-view.tsx
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
var ShippingProfileListView = ({
  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/shipping-profiles/shipping-profiles-list/shipping-profile-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(ShippingProfileListView, {}) });
};
var ShippingProfileListPage = Object.assign(Root, {
  Table: ShippingProfileListView,
  Header: ShippingProfileListHeader,
  HeaderTitle: ShippingProfileListTitle,
  HeaderActions: ShippingProfileListActions,
  DataTable: ShippingProfileListDataTable
});

export {
  ShippingProfileListPage
};
