import {
  require_dist
} from "./chunk-ZZO3INVE.js";
import {
  DateCell
} from "./chunk-25LYVP3C.js";
import {
  StatusCell
} from "./chunk-DB6ANJBO.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useSellerTableFilters
} from "./chunk-BE5QWX5N.js";
import {
  useSellersTableQuery
} from "./chunk-NNI6QKKZ.js";
import {
  sellersQueryKeys,
  useSellers
} from "./chunk-M3YP3NX4.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import {
  sdk
} from "./chunk-RIN4CBRB.js";
import {
  __toESM
} from "./chunk-NBMM2TZK.js";

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

// src/pages/stores/store-list/components/store-list-table/store-list-table.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/stores/store-list/components/store-list-table/store-list-header.tsx
import { Children } from "react";
import { Button, Heading } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var StoreListTitle = () => {
  return /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Stores" }) });
};
var StoreListActions = ({
  children
}) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx(Link, { to: "create", children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", children: t("actions.create") }) }) });
};
var StoreListHeader = ({
  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(StoreListTitle, {}),
    /* @__PURE__ */ jsx(StoreListActions, {})
  ] }) });
};

// src/pages/stores/store-list/components/store-list-table/store-list-data-table.tsx
import { useMemo as useMemo2, useState } from "react";
import { useTranslation as useTranslation4 } from "react-i18next";
import { Checkbox, toast } from "@medusajs/ui";
import { PencilSquare } from "@medusajs/icons";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper as createColumnHelper2 } from "@tanstack/react-table";

// src/hooks/table/columns/use-seller-table-columns.tsx
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation3 } from "react-i18next";

// src/components/table/table-cells/seller/seller-status-cell/seller-status-cell.tsx
import { useTranslation as useTranslation2 } from "react-i18next";
var import_types = __toESM(require_dist(), 1);
import { jsx as jsx2 } from "react/jsx-runtime";
var SellerStatusCell = ({ status }) => {
  const { t } = useTranslation2();
  const [color, text] = {
    [import_types.SellerStatus.OPEN]: ["green", t("stores.status.open")],
    [import_types.SellerStatus.PENDING_APPROVAL]: ["orange", t("stores.status.pendingApproval")],
    [import_types.SellerStatus.SUSPENDED]: ["red", t("stores.status.suspended")],
    [import_types.SellerStatus.TERMINATED]: ["grey", t("stores.status.terminated")]
  }[status];
  return /* @__PURE__ */ jsx2(StatusCell, { color, children: text });
};

// src/hooks/table/columns/use-seller-table-columns.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var useSellersTableColumns = () => {
  const { t } = useTranslation3();
  return useMemo(
    () => [
      columnHelper.display({
        id: "name",
        header: t("stores.fields.name"),
        cell: ({ row }) => row.original.name
      }),
      columnHelper.display({
        id: "email",
        header: t("stores.fields.email"),
        cell: ({ row }) => row.original.email
      }),
      columnHelper.display({
        id: "status",
        header: t("fields.status"),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx3(
            SellerStatusCell,
            {
              status: row.original.status
            }
          );
        }
      }),
      columnHelper.display({
        id: "is_premium",
        header: t("stores.fields.premium"),
        cell: ({ row }) => {
          const isPremium = row.original.is_premium;
          return /* @__PURE__ */ jsx3(StatusCell, { color: isPremium ? "purple" : "grey", children: isPremium ? t("stores.premium.yes") : t("stores.premium.no") });
        }
      }),
      columnHelper.display({
        id: "created_at",
        header: t("fields.createdAt"),
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx3(DateCell, { date: row.original.created_at });
        }
      })
    ],
    [t]
  );
};

// src/pages/stores/store-list/components/store-list-table/store-list-data-table.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var PAGE_SIZE = 10;
var StoreListDataTable = () => {
  const { t } = useTranslation4();
  const [selection, setSelection] = useState({});
  const { searchParams, raw } = useSellersTableQuery({
    pageSize: PAGE_SIZE
  });
  const { sellers, count, isLoading } = useSellers(
    {
      ...searchParams
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const columns = useColumns();
  const filters = useSellerTableFilters();
  const { table } = useDataTable({
    data: sellers ?? [],
    columns,
    count: count ?? 0,
    enablePagination: true,
    pageSize: PAGE_SIZE,
    getRowId: (row) => row?.id || "",
    enableRowSelection: true,
    rowSelection: {
      state: selection,
      updater: setSelection
    }
  });
  return /* @__PURE__ */ jsx4(
    _DataTable,
    {
      table,
      columns,
      count: count ?? 0,
      pageSize: PAGE_SIZE,
      filters,
      isLoading,
      queryObject: raw,
      search: true,
      pagination: true,
      navigateTo: (row) => `/stores/${row.original.id}`,
      orderBy: [
        { key: "name", label: t("stores.fields.name") },
        { key: "email", label: t("stores.fields.email") },
        { key: "created_at", label: t("fields.createdAt") }
      ],
      commands: [
        {
          action: async (selection2) => {
            const ids = Object.keys(selection2);
            try {
              await Promise.all(
                ids.map(
                  (id) => sdk.admin.sellers.$id.suspend.mutate({ $id: id })
                )
              );
              toast.success(t("stores.actions.suspend.successToast", { count: ids.length }));
            } catch {
              toast.error(t("stores.actions.suspend.errorToast"));
            }
            await queryClient.invalidateQueries({
              queryKey: sellersQueryKeys.lists()
            });
          },
          label: t("stores.actions.suspend.label"),
          shortcut: "s"
        },
        {
          action: async (selection2) => {
            const ids = Object.keys(selection2);
            try {
              await Promise.all(
                ids.map(
                  (id) => sdk.admin.sellers.$id.unsuspend.mutate({ $id: id })
                )
              );
              toast.success(t("stores.actions.unsuspend.successToast", { count: ids.length }));
            } catch {
              toast.error(t("stores.actions.unsuspend.errorToast"));
            }
            await queryClient.invalidateQueries({
              queryKey: sellersQueryKeys.lists()
            });
          },
          label: t("stores.actions.unsuspend.label"),
          shortcut: "u"
        }
      ]
    }
  );
};
var columnHelper2 = createColumnHelper2();
var StoreActions = ({ seller }) => {
  return /* @__PURE__ */ jsx4(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx4(PencilSquare, {}),
              label: "Edit",
              to: `/stores/${seller.id}/edit`
            }
          ]
        }
      ]
    }
  );
};
var useColumns = () => {
  const base = useSellersTableColumns();
  return useMemo2(
    () => [
      columnHelper2.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx4(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx4(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...base,
      columnHelper2.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx4(StoreActions, { seller: row.original })
      })
    ],
    [base]
  );
};

// src/pages/stores/store-list/components/store-list-table/store-list-table.tsx
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
var StoreListTable = ({ children }) => {
  return /* @__PURE__ */ jsx5(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx5(StoreListHeader, {}),
    /* @__PURE__ */ jsx5(StoreListDataTable, {})
  ] }) });
};

// src/pages/stores/store-list/store-list.tsx
import { jsx as jsx6 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx6(SingleColumnPage, { hasOutlet: true, children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx6(StoreListTable, {}) });
};
var StoreListPage = Object.assign(Root, {
  Table: StoreListTable,
  Header: StoreListHeader,
  HeaderTitle: StoreListTitle,
  HeaderActions: StoreListActions,
  DataTable: StoreListDataTable
});

export {
  StoreListPage
};
