import {
  PRODUCT_DETAIL_QUERY
} from "./chunk-35NDMIYZ.js";
import {
  useSalesChannelTableColumns,
  useSalesChannelTableEmptyState,
  useSalesChannelTableFilters,
  useSalesChannelTableQuery
} from "./chunk-CKNQJPAG.js";
import "./chunk-MDABSESI.js";
import "./chunk-54RTJBYO.js";
import {
  DataTable
} from "./chunk-OB4NOV4M.js";
import "./chunk-BDGZ4EQO.js";
import "./chunk-LAVHRER2.js";
import "./chunk-Y7QKP6QU.js";
import "./chunk-THHRRYRS.js";
import "./chunk-UZWFZMOX.js";
import "./chunk-XQNVC4Q7.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import "./chunk-3QSRE5LS.js";
import {
  useSalesChannels
} from "./chunk-DL2ZIXBO.js";
import {
  useProduct,
  useUpdateProduct
} from "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/products/product-sales-channels/product-sales-channels.tsx
import { useParams } from "react-router-dom";

// src/pages/products/product-sales-channels/components/edit-sales-channels-form/edit-sales-channels-form.tsx
import { Button, createDataTableColumnHelper } from "@medusajs/ui";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { keepPreviousData } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { jsx, jsxs } from "react/jsx-runtime";
var EditSalesChannelsSchema = zod.object({
  sales_channels: zod.array(zod.string()).optional()
});
var PAGE_SIZE = 50;
var PREFIX = "sc";
var EditSalesChannelsForm = ({
  product
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      sales_channels: product.sales_channels?.map((sc) => sc.id) ?? []
    },
    resolver: zodResolver(EditSalesChannelsSchema)
  });
  const { setValue } = form;
  const initialState = product.sales_channels?.reduce((acc, curr) => {
    acc[curr.id] = true;
    return acc;
  }, {}) ?? {};
  const [rowSelection, setRowSelection] = useState(initialState);
  useEffect(() => {
    const ids = Object.keys(rowSelection);
    setValue("sales_channels", ids, {
      shouldDirty: true,
      shouldTouch: true
    });
  }, [rowSelection, setValue]);
  const searchParams = useSalesChannelTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX
  });
  const { sales_channels, count, isLoading, isError, error } = useSalesChannels(
    {
      ...searchParams
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useSalesChannelTableFilters();
  const emptyState = useSalesChannelTableEmptyState();
  const columns = useColumns();
  const { mutateAsync, isPending: isMutating } = useUpdateProduct(product.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    const arr = data.sales_channels ?? [];
    const sales_channels2 = arr.map((id) => {
      return {
        id
      };
    });
    await mutateAsync(
      {
        sales_channels: sales_channels2
      },
      {
        onSuccess: () => {
          handleSuccess();
        }
      }
    );
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "product-sales-channels-form", children: /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", "data-testid": "product-sales-channels-form-container", children: [
    /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "product-sales-channels-form-header" }),
    /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex-1 overflow-hidden", "data-testid": "product-sales-channels-form-body", children: /* @__PURE__ */ jsx(
      DataTable,
      {
        data: sales_channels,
        columns,
        getRowId: (row) => row.id,
        rowCount: count,
        isLoading,
        filters,
        rowSelection: {
          state: rowSelection,
          onRowSelectionChange: setRowSelection
        },
        autoFocusSearch: true,
        layout: "fill",
        emptyState,
        prefix: PREFIX,
        "data-testid": "product-sales-channels-data-table"
      }
    ) }),
    /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "product-sales-channels-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "product-sales-channels-form-footer-actions", children: [
      /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, "data-testid": "product-sales-channels-form-cancel-button-wrapper", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "product-sales-channels-form-cancel-button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(Button, { size: "small", isLoading: isMutating, onClick: handleSubmit, "data-testid": "product-sales-channels-form-save-button", children: t("actions.save") })
    ] }) })
  ] }) });
};
var columnHelper = createDataTableColumnHelper();
var useColumns = () => {
  const columns = useSalesChannelTableColumns();
  return useMemo(() => [columnHelper.select(), ...columns], [columns]);
};

// src/pages/products/product-sales-channels/product-sales-channels.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var ProductSalesChannels = () => {
  const { id } = useParams();
  const { product, isLoading, isError, error } = useProduct(id, PRODUCT_DETAIL_QUERY);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx2(RouteFocusModal, { "data-testid": "product-sales-channels-modal", children: !isLoading && product && /* @__PURE__ */ jsx2(EditSalesChannelsForm, { product }) });
};
export {
  ProductSalesChannels as Component
};
