import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  useSalesChannelTableColumns,
  useSalesChannelTableEmptyState,
  useSalesChannelTableFilters,
  useSalesChannelTableQuery
} from "./chunk-CKNQJPAG.js";
import {
  DataTable
} from "./chunk-OB4NOV4M.js";
import {
  useStore
} from "./chunk-FXWKGNRV.js";
import {
  useDeleteSalesChannelLazy,
  useSalesChannels
} from "./chunk-DL2ZIXBO.js";

// src/pages/sales-channels/sales-channel-list/sales-channel-list.tsx
import { Children as Children3 } from "react";

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

// src/pages/sales-channels/sales-channel-list/components/sales-channel-list-view/sales-channel-list-data-table.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import {
  createDataTableColumnHelper,
  toast,
  usePrompt
} from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { useCallback, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { jsx } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var SalesChannelListDataTable = () => {
  const { store } = useStore();
  const searchParams = useSalesChannelTableQuery({
    pageSize: PAGE_SIZE
  });
  const { sales_channels, count, isPending, isError, error } = useSalesChannels(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const columns = useColumns();
  const filters = useSalesChannelTableFilters();
  const emptyState = useSalesChannelTableEmptyState();
  const sales_channels_data = sales_channels?.map((sales_channel) => {
    return {
      ...sales_channel,
      is_default: store?.default_sales_channel_id === sales_channel.id
    };
  }) ?? [];
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx("div", { "data-testid": "sales-channel-list-table", children: /* @__PURE__ */ jsx(
    DataTable,
    {
      data: sales_channels_data,
      columns,
      rowCount: count,
      getRowId: (row) => row.id,
      pageSize: PAGE_SIZE,
      filters,
      isLoading: isPending,
      emptyState,
      rowHref: (row) => `/settings/sales-channels/${row.id}`
    }
  ) });
};
var columnHelper = createDataTableColumnHelper();
var useColumns = () => {
  const { t } = useTranslation();
  const prompt = usePrompt();
  const navigate = useNavigate();
  const base = useSalesChannelTableColumns();
  const { mutateAsync } = useDeleteSalesChannelLazy();
  const handleDelete = useCallback(
    async (salesChannel) => {
      const confirm = await prompt({
        title: t("general.areYouSure"),
        description: t("salesChannels.deleteSalesChannelWarning", {
          name: salesChannel.name
        }),
        verificationInstruction: t("general.typeToConfirm"),
        verificationText: salesChannel.name,
        confirmText: t("actions.delete"),
        cancelText: t("actions.cancel")
      });
      if (!confirm) {
        return;
      }
      await mutateAsync(salesChannel.id, {
        onSuccess: () => {
          toast.success(t("salesChannels.toast.delete"));
        },
        onError: (e) => {
          toast.error(e.message);
        }
      });
    },
    [t, prompt, mutateAsync]
  );
  return useMemo(
    () => [
      ...base,
      columnHelper.action({
        actions: (ctx) => {
          const disabledTooltip = ctx.row.original.is_default ? t("salesChannels.tooltip.cannotDeleteDefault") : void 0;
          return [
            [
              {
                icon: /* @__PURE__ */ jsx(PencilSquare, {}),
                label: t("actions.edit"),
                onClick: () => navigate(
                  `/settings/sales-channels/${ctx.row.original.id}/edit`
                )
              }
            ],
            [
              {
                icon: /* @__PURE__ */ jsx(Trash, {}),
                label: t("actions.delete"),
                onClick: () => handleDelete(ctx.row.original),
                disabled: ctx.row.original.is_default,
                disabledTooltip
              }
            ]
          ];
        }
      })
    ],
    [base, handleDelete, navigate, t]
  );
};

// src/pages/sales-channels/sales-channel-list/components/sales-channel-list-view/sales-channel-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 SalesChannelListTitle = () => {
  const { t } = useTranslation2();
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx2(Heading, { children: t("salesChannels.domain") }),
    /* @__PURE__ */ jsx2(Text, { className: "text-ui-fg-subtle", size: "small", children: t("salesChannels.subtitle") })
  ] });
};
var SalesChannelListActions = ({
  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(Button, { size: "small", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx2(Link, { to: "/settings/sales-channels/create", children: t("actions.create") }) }) });
};
var SalesChannelListHeader = ({
  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(SalesChannelListTitle, {}),
    /* @__PURE__ */ jsx2(SalesChannelListActions, {})
  ] }) });
};

// src/pages/sales-channels/sales-channel-list/components/sales-channel-list-view/sales-channel-list-view.tsx
import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
var SalesChannelListView = ({
  children
}) => {
  return /* @__PURE__ */ jsx3(Container, { className: "divide-y p-0", "data-testid": "sales-channel-list-table-container", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx3(SalesChannelListHeader, {}),
    /* @__PURE__ */ jsx3(SalesChannelListDataTable, {})
  ] }) });
};

// src/pages/sales-channels/sales-channel-list/sales-channel-list.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx4(SingleColumnPage, { hasOutlet: true, children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx4(SalesChannelListView, {}) });
};
var SalesChannelListPage = Object.assign(Root, {
  Table: SalesChannelListView,
  Header: SalesChannelListHeader,
  HeaderTitle: SalesChannelListTitle,
  HeaderActions: SalesChannelListActions,
  DataTable: SalesChannelListDataTable
});

export {
  SalesChannelListPage
};
