import {
  DateRangeDisplay
} from "./chunk-RK5N3XIX.js";
import {
  getPromotionStatus
} from "./chunk-N57BOIPU.js";
import {
  formatPercentage
} from "./chunk-QDPLZDMX.js";
import {
  formatCurrency
} from "./chunk-XSX255FK.js";
import {
  TwoColumnPage
} from "./chunk-E74OUNA3.js";
import {
  NoRecords
} from "./chunk-634XVU3D.js";
import {
  TwoColumnPageSkeleton
} from "./chunk-INNFZYX2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useDeletePromotion,
  usePromotion,
  usePromotionRules
} from "./chunk-ZN7LWXVC.js";

// src/pages/promotions/promotion-detail/promotion-detail.tsx
import { Children } from "react";
import { useLoaderData, useParams as useParams4 } from "react-router-dom";

// src/pages/promotions/promotion-detail/components/campaign-section/campaign-section.tsx
import { ArrowUpRightOnBox, PencilSquare } from "@medusajs/icons";
import { Container, Heading, Text } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
import { jsx, jsxs } from "react/jsx-runtime";
var CampaignDetailSection = ({
  campaign
}) => {
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-muted flex items-center gap-x-1.5", children: [
      /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", className: "text-ui-fg-base", children: campaign.name }),
      /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "\xB7" }),
      /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: campaign.campaign_identifier })
    ] }),
    /* @__PURE__ */ jsx(
      DateRangeDisplay,
      {
        startsAt: campaign.starts_at,
        endsAt: campaign.ends_at,
        showTime: true
      }
    )
  ] });
};
var CampaignSection = ({
  campaign: campaignProp
}) => {
  const { t } = useTranslation();
  const { id } = useParams();
  const { promotion } = usePromotion(id, {
    enabled: campaignProp === void 0
  });
  const campaign = campaignProp !== void 0 ? campaignProp : promotion?.campaign ?? null;
  const actions = [
    {
      label: t("actions.edit"),
      to: "add-to-campaign",
      icon: /* @__PURE__ */ jsx(PencilSquare, {})
    }
  ];
  if (campaign) {
    actions.unshift({
      label: t("promotions.campaign.actions.goToCampaign"),
      to: `/campaigns/${campaign.id}`,
      icon: /* @__PURE__ */ jsx(ArrowUpRightOnBox, {})
    });
  }
  return /* @__PURE__ */ jsxs(Container, { "data-testid": "promotion-campaign-section-container", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", "data-testid": "promotion-campaign-section-header", children: [
      /* @__PURE__ */ jsx(Heading, { level: "h2", "data-testid": "promotion-campaign-section-heading", children: t("promotions.fields.campaign") }),
      /* @__PURE__ */ jsx(
        ActionMenu,
        {
          groups: [
            {
              actions
            }
          ],
          "data-testid": "promotion-campaign-section-action-menu"
        }
      )
    ] }),
    campaign ? /* @__PURE__ */ jsx("div", { "data-testid": "promotion-campaign-section-detail", children: /* @__PURE__ */ jsx(CampaignDetailSection, { campaign }) }) : /* @__PURE__ */ jsx("div", { "data-testid": "promotion-campaign-section-no-records", children: /* @__PURE__ */ jsx(
      NoRecords,
      {
        className: "h-[180px] pt-4 text-center",
        title: "Not part of a campaign",
        message: "Add this promotion to an existing campaign",
        action: {
          to: `/promotions/${id}/add-to-campaign`,
          label: "Add to Campaign"
        },
        buttonVariant: "transparentIconLeft",
        dataTestId: "promotion-campaign-section-add-to-campaign-button"
      }
    ) })
  ] });
};

// src/pages/promotions/promotion-detail/components/promotion-conditions-section/promotion-conditions-section.tsx
import { PencilSquare as PencilSquare2 } from "@medusajs/icons";
import { Badge as Badge2, Container as Container2, Heading as Heading2 } from "@medusajs/ui";
import { useTranslation as useTranslation3 } from "react-i18next";
import { useParams as useParams2 } from "react-router-dom";

// src/components/common/badge-list-summary/badge-list-summary.tsx
import { Badge, Tooltip, clx } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var BadgeListSummary = ({
  list,
  className,
  inline,
  rounded = false,
  n = 2
}) => {
  const { t } = useTranslation2();
  const title = t("general.plusCount", {
    count: list.length - n
  });
  return /* @__PURE__ */ jsxs2(
    "div",
    {
      className: clx(
        "text-ui-fg-subtle txt-compact-small gap-x-2 overflow-hidden",
        {
          "inline-flex": inline,
          flex: !inline
        },
        className
      ),
      children: [
        list.slice(0, n).map((item) => {
          return /* @__PURE__ */ jsx2(Badge, { rounded: rounded ? "full" : "base", size: "2xsmall", children: item }, item);
        }),
        list.length > n && /* @__PURE__ */ jsx2("div", { className: "whitespace-nowrap", children: /* @__PURE__ */ jsx2(
          Tooltip,
          {
            content: /* @__PURE__ */ jsx2("ul", { children: list.slice(n).map((c) => /* @__PURE__ */ jsx2("li", { children: c }, c)) }),
            children: /* @__PURE__ */ jsx2(
              Badge,
              {
                rounded: rounded ? "full" : "base",
                size: "2xsmall",
                className: "cursor-default whitespace-nowrap",
                children: title
              }
            )
          }
        ) })
      ]
    }
  );
};

// src/pages/promotions/promotion-detail/components/promotion-conditions-section/promotion-conditions-section.tsx
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
function RuleBlock({ rule }) {
  return /* @__PURE__ */ jsx3("div", { className: "bg-ui-bg-subtle shadow-borders-base align-center flex justify-around rounded-md p-2", children: /* @__PURE__ */ jsxs3("div", { className: "text-ui-fg-subtle txt-compact-xsmall flex items-center whitespace-nowrap", children: [
    /* @__PURE__ */ jsx3(
      Badge2,
      {
        size: "2xsmall",
        className: "txt-compact-xsmall-plus tag-neutral-text mx-1 inline-block truncate",
        children: rule.attribute_label
      },
      "rule-attribute"
    ),
    /* @__PURE__ */ jsx3("span", { className: "txt-compact-2xsmall mx-1 inline-block", children: rule.operator_label }),
    /* @__PURE__ */ jsx3(
      BadgeListSummary,
      {
        inline: true,
        className: "!txt-compact-small-plus",
        list: rule.field_type === "number" ? [rule.values] : rule.values?.map((v) => v.label)
      }
    )
  ] }) });
}
var PromotionConditionsSection = ({
  rules: rulesProp,
  ruleType,
  applicationMethodTargetType
}) => {
  const { t } = useTranslation3();
  const { id } = useParams2();
  const { promotion } = usePromotion(id, {
    enabled: rulesProp === void 0
  });
  const query = {};
  if (promotion?.type === "buyget") {
    query.promotion_type = promotion.type;
  }
  const { rules: fetchedRules } = usePromotionRules(id, ruleType, query, {
    enabled: rulesProp === void 0
  });
  const rules = rulesProp ?? fetchedRules ?? [];
  return /* @__PURE__ */ jsxs3(
    Container2,
    {
      className: "p-0",
      "data-testid": `promotion-conditions-section-container-${ruleType}`,
      children: [
        /* @__PURE__ */ jsxs3(
          "div",
          {
            className: "flex items-center justify-between px-6 py-4",
            "data-testid": `promotion-conditions-section-header-${ruleType}`,
            children: [
              /* @__PURE__ */ jsx3("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx3(
                Heading2,
                {
                  "data-testid": `promotion-conditions-section-heading-${ruleType}`,
                  children: t(
                    ruleType === "target-rules" ? `promotions.fields.conditions.${ruleType}.${applicationMethodTargetType}.title` : `promotions.fields.conditions.${ruleType}.title`
                  )
                }
              ) }),
              /* @__PURE__ */ jsx3(
                ActionMenu,
                {
                  groups: [
                    {
                      actions: [
                        {
                          icon: /* @__PURE__ */ jsx3(PencilSquare2, {}),
                          label: t("actions.edit"),
                          to: `${ruleType}/edit`
                        }
                      ]
                    }
                  ],
                  "data-testid": `promotion-conditions-section-action-menu-${ruleType}`
                }
              )
            ]
          }
        ),
        /* @__PURE__ */ jsxs3(
          "div",
          {
            className: "text-ui-fg-subtle flex flex-col gap-2 px-6 pb-4 pt-2",
            "data-testid": `promotion-conditions-section-content-${ruleType}`,
            children: [
              !rules.length && /* @__PURE__ */ jsx3(
                "div",
                {
                  "data-testid": `promotion-conditions-section-no-records-${ruleType}`,
                  children: /* @__PURE__ */ jsx3(
                    NoRecords,
                    {
                      className: "h-[180px]",
                      title: t("general.noRecordsTitle"),
                      message: t("promotions.conditions.list.noRecordsMessage"),
                      action: {
                        to: `${ruleType}/edit`,
                        label: t("promotions.conditions.add")
                      },
                      buttonVariant: "transparentIconLeft",
                      dataTestId: `promotion-conditions-section-add-condition-button-${ruleType}`
                    }
                  )
                }
              ),
              rules.map((rule) => /* @__PURE__ */ jsx3(
                "div",
                {
                  "data-testid": `promotion-conditions-section-rule-${rule.id}`,
                  children: /* @__PURE__ */ jsx3(RuleBlock, { rule })
                },
                `${rule.id}-${rule.attribute}`
              ))
            ]
          }
        )
      ]
    }
  );
};

// src/pages/promotions/promotion-detail/components/promotion-general-section/promotion-general-section.tsx
import { PencilSquare as PencilSquare3, Trash } from "@medusajs/icons";
import {
  Badge as Badge3,
  Container as Container3,
  Copy,
  Heading as Heading3,
  StatusBadge,
  Text as Text2,
  usePrompt
} from "@medusajs/ui";
import { useTranslation as useTranslation4 } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { useParams as useParams3 } from "react-router-dom";
import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
function getDisplayValue(promotion) {
  const value = promotion.application_method?.value;
  if (!value) {
    return null;
  }
  if (promotion.application_method?.type === "fixed") {
    const currency = promotion.application_method?.currency_code;
    if (!currency) {
      return null;
    }
    return formatCurrency(value, currency);
  } else if (promotion.application_method?.type === "percentage") {
    return formatPercentage(value);
  }
  return null;
}
var PromotionGeneralSection = ({
  promotion: promotionProp
}) => {
  const { t } = useTranslation4();
  const prompt = usePrompt();
  const navigate = useNavigate();
  const { id } = useParams3();
  const { promotion: fetched } = usePromotion(id, {
    enabled: !promotionProp
  });
  const promotion = promotionProp ?? fetched;
  const { mutateAsync } = useDeletePromotion(promotion?.id);
  if (!promotion) {
    return null;
  }
  const handleDelete = async () => {
    const confirm = await prompt({
      title: t("general.areYouSure"),
      description: t("promotions.deleteWarning", {
        code: promotion.code
      }),
      verificationInstruction: t("general.typeToConfirm"),
      verificationText: promotion.code,
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!confirm) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        navigate("/promotions", { replace: true });
      }
    });
  };
  const [color, text] = getPromotionStatus(promotion);
  const displayValue = getDisplayValue(promotion);
  return /* @__PURE__ */ jsxs4(Container3, { className: "divide-y p-0", "data-testid": "promotion-general-section-container", children: [
    /* @__PURE__ */ jsxs4("div", { className: "flex items-center justify-between px-6 py-4", "data-testid": "promotion-general-section-header", children: [
      /* @__PURE__ */ jsx4("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx4(Heading3, { "data-testid": "promotion-general-section-code", children: promotion.code }) }),
      /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-x-2", children: [
        /* @__PURE__ */ jsx4(StatusBadge, { color, "data-testid": "promotion-general-section-status", children: text }),
        /* @__PURE__ */ jsx4(
          ActionMenu,
          {
            groups: [
              {
                actions: [
                  {
                    icon: /* @__PURE__ */ jsx4(PencilSquare3, {}),
                    label: t("actions.edit"),
                    to: `/promotions/${promotion.id}/edit`
                  }
                ]
              },
              {
                actions: [
                  {
                    icon: /* @__PURE__ */ jsx4(Trash, {}),
                    label: t("actions.delete"),
                    onClick: handleDelete
                  }
                ]
              }
            ],
            "data-testid": "promotion-general-section-action-menu"
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", "data-testid": "promotion-general-section-method", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", "data-testid": "promotion-general-section-method-label", children: t("promotions.fields.method") }),
      /* @__PURE__ */ jsx4(Text2, { size: "small", leading: "compact", className: "text-pretty", "data-testid": "promotion-general-section-method-value", children: promotion.is_automatic ? t("promotions.form.method.automatic.title") : t("promotions.form.method.code.title") })
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4", "data-testid": "promotion-general-section-code-field", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", "data-testid": "promotion-general-section-code-label", children: t("fields.code") }),
      /* @__PURE__ */ jsx4(
        Copy,
        {
          content: promotion.code,
          className: "text-ui-tag-neutral-text",
          asChild: true,
          "data-testid": "promotion-general-section-code-copy",
          children: /* @__PURE__ */ jsx4(
            Badge3,
            {
              size: "2xsmall",
              rounded: "full",
              className: "cursor-pointer text-pretty",
              "data-testid": "promotion-general-section-code-badge",
              children: promotion.code
            }
          )
        }
      )
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", "data-testid": "promotion-general-section-type", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", "data-testid": "promotion-general-section-type-label", children: t("promotions.fields.type") }),
      /* @__PURE__ */ jsx4(Text2, { size: "small", leading: "compact", className: "text-pretty capitalize", "data-testid": "promotion-general-section-type-value", children: promotion.type })
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", "data-testid": "promotion-general-section-value", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", "data-testid": "promotion-general-section-value-label", children: t("promotions.fields.value") }),
      /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-x-2", "data-testid": "promotion-general-section-value-content", children: [
        /* @__PURE__ */ jsx4(Text2, { className: "inline", size: "small", leading: "compact", "data-testid": "promotion-general-section-value-text", children: displayValue || "-" }),
        promotion?.application_method?.type === "fixed" && /* @__PURE__ */ jsx4(Badge3, { size: "2xsmall", rounded: "full", "data-testid": "promotion-general-section-value-currency", children: promotion?.application_method?.currency_code?.toUpperCase() })
      ] })
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", "data-testid": "promotion-general-section-allocation", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", "data-testid": "promotion-general-section-allocation-label", children: t("promotions.fields.allocation") }),
      /* @__PURE__ */ jsx4(Text2, { size: "small", leading: "compact", className: "text-pretty capitalize", "data-testid": "promotion-general-section-allocation-value", children: promotion.application_method?.allocation })
    ] }),
    promotion.application_method?.type === "fixed" && /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", "data-testid": "promotion-general-section-tax-inclusive", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", "data-testid": "promotion-general-section-tax-inclusive-label", children: t("promotions.fields.taxInclusive") }),
      /* @__PURE__ */ jsx4("div", { className: "flex items-center gap-x-2", "data-testid": "promotion-general-section-tax-inclusive-value", children: /* @__PURE__ */ jsx4(Text2, { className: "inline", size: "small", leading: "compact", children: promotion.is_tax_inclusive ? t("fields.true") : t("fields.false") }) })
    ] })
  ] });
};

// src/pages/promotions/promotion-detail/promotion-detail.tsx
import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const initialData = useLoaderData();
  const { id } = useParams4();
  const { promotion, isLoading } = usePromotion(id, { initialData });
  const query = {};
  if (promotion?.type === "buyget") {
    query.promotion_type = promotion.type;
  }
  const { rules } = usePromotionRules(id, "rules", query);
  const { rules: targetRules } = usePromotionRules(id, "target-rules", query);
  const { rules: buyRules } = usePromotionRules(id, "buy-rules", query);
  if (isLoading || !promotion) {
    return /* @__PURE__ */ jsx5(TwoColumnPageSkeleton, { mainSections: 3, sidebarSections: 1, showJSON: true });
  }
  return Children.count(children) > 0 ? /* @__PURE__ */ jsx5(TwoColumnPage, { data: promotion, hasOutlet: true, showJSON: true, "data-testid": "promotion-detail-page", children }) : /* @__PURE__ */ jsxs5(TwoColumnPage, { data: promotion, hasOutlet: true, showJSON: true, "data-testid": "promotion-detail-page", children: [
    /* @__PURE__ */ jsxs5(TwoColumnPage.Main, { children: [
      /* @__PURE__ */ jsx5(PromotionGeneralSection, { promotion }),
      /* @__PURE__ */ jsx5(
        PromotionConditionsSection,
        {
          rules: rules || [],
          ruleType: "rules"
        }
      ),
      /* @__PURE__ */ jsx5(
        PromotionConditionsSection,
        {
          rules: targetRules || [],
          ruleType: "target-rules",
          applicationMethodTargetType: promotion.application_method?.target_type || "items"
        }
      ),
      promotion.type === "buyget" && /* @__PURE__ */ jsx5(
        PromotionConditionsSection,
        {
          rules: buyRules || [],
          ruleType: "buy-rules",
          applicationMethodTargetType: "items"
        }
      )
    ] }),
    /* @__PURE__ */ jsx5(TwoColumnPage.Sidebar, { children: /* @__PURE__ */ jsx5(CampaignSection, { campaign: promotion.campaign }) })
  ] });
};
var PromotionDetailPage = Object.assign(Root, {
  Main: TwoColumnPage.Main,
  Sidebar: TwoColumnPage.Sidebar,
  MainGeneralSection: PromotionGeneralSection,
  MainConditionsSection: PromotionConditionsSection,
  SidebarCampaignSection: CampaignSection
});

export {
  PromotionDetailPage
};
