import {
  usePromotionTableColumns,
  usePromotionTableQuery
} from "./chunk-DSNCEHLY.js";
import "./chunk-S5KWI3MM.js";
import "./chunk-JHIMVLB2.js";
import {
  usePromotionTableFilters
} from "./chunk-S2WEYEQR.js";
import "./chunk-FMFVTERD.js";
import "./chunk-DB6ANJBO.js";
import "./chunk-PHOCVOZ5.js";
import "./chunk-ZZ4JC4WE.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import "./chunk-3QSRE5LS.js";
import "./chunk-FQERBIVS.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import "./chunk-YN3FGNJM.js";
import "./chunk-KCHYLLVG.js";
import "./chunk-CVXKASWV.js";
import "./chunk-THHRRYRS.js";
import "./chunk-LRD242C7.js";
import "./chunk-INNFZYX2.js";
import "./chunk-LAVHRER2.js";
import "./chunk-QHQWEERJ.js";
import "./chunk-Y7QKP6QU.js";
import {
  useAddOrRemoveCampaignPromotions,
  useCampaign,
  usePromotions
} from "./chunk-MR6REQKT.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/campaigns/[id]/add-promotions/add-promotions-page.tsx
import { useParams } from "react-router-dom";

// src/pages/campaigns/[id]/add-promotions/add-campaign-promotions-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Checkbox, Hint, Tooltip, toast } from "@medusajs/ui";
import {
  createColumnHelper
} from "@tanstack/react-table";
import { useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var AddCampaignPromotionsSchema = zod.object({
  promotion_ids: zod.array(zod.string()).min(1)
});
var PAGE_SIZE = 50;
var AddCampaignPromotionsForm = ({
  campaign
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: { promotion_ids: [] },
    resolver: zodResolver(AddCampaignPromotionsSchema)
  });
  const { setValue } = form;
  const { mutateAsync, isPending } = useAddOrRemoveCampaignPromotions(
    campaign.id
  );
  const [rowSelection, setRowSelection] = useState({});
  const updater = (fn) => {
    const state = typeof fn === "function" ? fn(rowSelection) : fn;
    const ids = Object.keys(state);
    setValue("promotion_ids", ids, {
      shouldDirty: true,
      shouldTouch: true
    });
    setRowSelection(state);
  };
  const { searchParams, raw } = usePromotionTableQuery({ pageSize: PAGE_SIZE });
  const { promotions: promotionsRaw, isPending: isLoading } = usePromotions({
    ...searchParams,
    fields: "+status"
  });
  const promotions = promotionsRaw?.filter((item) => item !== null);
  const count = promotions?.length ?? 0;
  const columns = useColumns();
  const filters = usePromotionTableFilters();
  const { table } = useDataTable({
    data: promotions ?? [],
    columns,
    enableRowSelection: (row) => {
      return row.original.campaign_id !== campaign.id;
    },
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE,
    count,
    rowSelection: {
      state: rowSelection,
      updater
    },
    meta: {
      campaignId: campaign.id,
      currencyCode: campaign?.budget?.currency_code,
      budgetType: campaign?.budget?.type
    }
  });
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      { add: values.promotion_ids },
      {
        onSuccess: () => {
          toast.success(
            t("campaigns.promotions.toast.success", {
              count: values.promotion_ids.length
            })
          );
          handleSuccess();
        },
        onError: (error) => toast.error(error.message)
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { children: /* @__PURE__ */ jsx("div", { className: "flex items-center justify-end gap-x-2", children: form.formState.errors.promotion_ids && /* @__PURE__ */ jsx(Hint, { variant: "error", children: form.formState.errors.promotion_ids.message }) }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex size-full flex-col overflow-y-auto", children: /* @__PURE__ */ jsx(
          _DataTable,
          {
            table,
            count,
            columns,
            pageSize: PAGE_SIZE,
            isLoading,
            filters,
            orderBy: [
              { key: "code", label: t("fields.code") },
              { key: "type", label: t("fields.type") },
              { key: "created_at", label: t("fields.createdAt") },
              { key: "updated_at", label: t("fields.updatedAt") }
            ],
            queryObject: raw,
            layout: "fill",
            pagination: true,
            search: "autofocus",
            noRecords: {
              message: t("campaigns.promotions.add.list.noRecordsMessage")
            }
          }
        ) }),
        /* @__PURE__ */ jsx(RouteFocusModal.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", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const base = usePromotionTableColumns();
  const { t } = useTranslation();
  return useMemo(
    () => [
      columnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllRowsSelected(!!value)
            }
          );
        },
        cell: ({ row, table }) => {
          const { campaignId, currencyCode, budgetType } = table.options.meta;
          const isTypeSpend = budgetType === "spend";
          const isAdded = row.original.campaign_id === campaignId;
          const isAddedToADiffCampaign = !!row.original.campaign_id && row.original.campaign_id !== campaignId;
          const currencyMismatch = isTypeSpend && row.original.application_method?.currency_code !== currencyCode;
          const isSelected = row.getIsSelected() || isAdded;
          const isIndeterminate = currencyMismatch || isAddedToADiffCampaign;
          const Component = /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: isIndeterminate ? "indeterminate" : isSelected,
              disabled: isAdded || isAddedToADiffCampaign || currencyMismatch,
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
          if (isAddedToADiffCampaign) {
            return /* @__PURE__ */ jsx(
              Tooltip,
              {
                content: t("campaigns.promotions.alreadyAddedDiffCampaign", {
                  name: row.original?.campaign?.name
                }),
                side: "right",
                children: Component
              }
            );
          }
          if (currencyMismatch) {
            return /* @__PURE__ */ jsx(
              Tooltip,
              {
                content: t("campaigns.promotions.currencyMismatch"),
                side: "right",
                children: Component
              }
            );
          }
          if (isAdded) {
            return /* @__PURE__ */ jsx(
              Tooltip,
              {
                content: t("campaigns.promotions.alreadyAdded"),
                side: "right",
                children: Component
              }
            );
          }
          return Component;
        }
      }),
      ...base
    ],
    [t, base]
  );
};

// src/pages/campaigns/[id]/add-promotions/add-promotions-page.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var AddPromotionsPage = () => {
  const { id } = useParams();
  const { campaign, isError, error } = useCampaign(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx2(RouteFocusModal, { children: campaign && /* @__PURE__ */ jsx2(AddCampaignPromotionsForm, { campaign }) });
};
export {
  AddPromotionsPage
};
