import {
  DataGrid,
  createDataGridHelper
} from "./chunk-MWX5JCOM.js";
import "./chunk-IQPN4PZJ.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import "./chunk-INNFZYX2.js";
import "./chunk-LAVHRER2.js";
import "./chunk-Y7QKP6QU.js";
import {
  require_dist
} from "./chunk-ZZO3INVE.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import "./chunk-3QSRE5LS.js";
import {
  sellersQueryKeys,
  useSellers
} from "./chunk-LEG67A2R.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-ZBWL72TB.js";
import {
  __toESM
} from "./chunk-NBMM2TZK.js";

// src/pages/stores/store-bulk-edit/components/store-bulk-edit-form.tsx
import { useEffect, useMemo } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useSearchParams } from "react-router-dom";
import { Button, toast } from "@medusajs/ui";
var import_types = __toESM(require_dist(), 1);
import { jsx, jsxs } from "react/jsx-runtime";
var helper = createDataGridHelper();
var useColumns = () => {
  const { t } = useTranslation();
  return useMemo(
    () => [
      helper.column({
        id: "name",
        name: t("fields.name"),
        header: t("fields.name"),
        cell: (context) => /* @__PURE__ */ jsx(DataGrid.ReadonlyCell, { context, color: "normal", children: context.row.original.name }),
        disableHiding: true
      }),
      helper.column({
        id: "status",
        name: t("fields.status"),
        header: t("fields.status"),
        type: "select",
        field: (context) => `statuses.${context.row.original.id}`,
        cell: (context) => /* @__PURE__ */ jsx(
          DataGrid.SelectCell,
          {
            context,
            options: [
              {
                label: t("stores.status.open"),
                value: import_types.SellerStatus.OPEN
              },
              {
                label: t("stores.status.pendingApproval"),
                value: import_types.SellerStatus.PENDING_APPROVAL
              },
              {
                label: t("stores.status.suspended"),
                value: import_types.SellerStatus.SUSPENDED
              }
            ]
          }
        )
      })
    ],
    [t]
  );
};
var StoreBulkEditForm = () => {
  const { t } = useTranslation();
  const [searchParams] = useSearchParams();
  const { handleSuccess, setCloseOnEscape } = useRouteModal();
  const ids = useMemo(
    () => (searchParams.get("ids") ?? "").split(",").map((id) => id.trim()).filter(Boolean),
    [searchParams]
  );
  const { sellers, isLoading } = useSellers(
    { id: ids, limit: ids.length || 1 },
    { enabled: ids.length > 0 }
  );
  const form = useForm({
    defaultValues: { statuses: {} }
  });
  useEffect(() => {
    if (sellers) {
      form.reset({
        statuses: Object.fromEntries(
          sellers.map((s) => [s.id, s.status])
        )
      });
    }
  }, [sellers, form]);
  const columns = useColumns();
  const handleSubmit = form.handleSubmit(async ({ statuses }) => {
    if (!sellers) return;
    const updates = sellers.filter((seller) => statuses[seller.id] !== seller.status).map(
      (seller) => sdk.admin.sellers.$id.mutate({
        $id: seller.id,
        status: statuses[seller.id]
      })
    );
    if (!updates.length) {
      handleSuccess();
      return;
    }
    try {
      await Promise.all(updates);
      await queryClient.invalidateQueries({
        queryKey: sellersQueryKeys.lists()
      });
      toast.success(
        t("stores.bulkEdit.successToast", { count: updates.length })
      );
      handleSuccess();
    } catch (error) {
      toast.error(error.message);
    }
  });
  const { isSubmitting } = form.formState;
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "store-bulk-edit-form", children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex size-full flex-col", children: [
    /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "store-bulk-edit-form-header" }),
    /* @__PURE__ */ jsx(
      RouteFocusModal.Body,
      {
        className: "flex flex-col overflow-hidden",
        "data-testid": "store-bulk-edit-form-body",
        children: /* @__PURE__ */ jsx(
          DataGrid,
          {
            state: form,
            columns,
            data: sellers ?? [],
            isLoading,
            onEditingChange: (editing) => setCloseOnEscape(!editing),
            disableInteractions: isSubmitting,
            "data-testid": "store-bulk-edit-data-grid"
          }
        )
      }
    ),
    /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "store-bulk-edit-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
      /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(
        Button,
        {
          size: "small",
          variant: "secondary",
          type: "button",
          "data-testid": "store-bulk-edit-cancel-button",
          children: t("actions.cancel")
        }
      ) }),
      /* @__PURE__ */ jsx(
        Button,
        {
          size: "small",
          type: "submit",
          isLoading: isSubmitting,
          "data-testid": "store-bulk-edit-save-button",
          children: t("actions.save")
        }
      )
    ] }) })
  ] }) });
};

// src/pages/stores/store-bulk-edit/index.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Root = () => /* @__PURE__ */ jsx2(RouteFocusModal, { children: /* @__PURE__ */ jsx2(StoreBulkEditForm, {}) });
var StoreBulkEditPage = Object.assign(Root, {
  Form: StoreBulkEditForm
});
var Component = Root;
export {
  Component,
  StoreBulkEditPage
};
