import {
  DateRangeDisplay
} from "./chunk-PFZTPUFP.mjs";
import {
  getPromotionStatus
} from "./chunk-SC6BSTC7.mjs";
import {
  formatPercentage
} from "./chunk-4FM6S5CX.mjs";
import {
  formatCurrency
} from "./chunk-WATKBUHQ.mjs";
import {
  NoRecords
} from "./chunk-EMIHDNB7.mjs";
import {
  TwoColumnPage
} from "./chunk-GIZFNLKK.mjs";
import "./chunk-3BQAAPDR.mjs";
import "./chunk-4GQOUCX6.mjs";
import "./chunk-PMA2D6IL.mjs";
import {
  useExtension
} from "./chunk-C5P5PL3E.mjs";
import {
  TwoColumnPageSkeleton
} from "./chunk-LPEUYMRK.mjs";
import {
  ActionMenu
} from "./chunk-OZPB6JBL.mjs";
import "./chunk-OC7BQLYI.mjs";
import "./chunk-S4DMV3ZT.mjs";
import "./chunk-QJ63TWAK.mjs";
import "./chunk-OW2UL5FI.mjs";
import "./chunk-5DUV4AET.mjs";
import "./chunk-FAB3VINY.mjs";
import "./chunk-Q7YBE4IP.mjs";
import "./chunk-HI6URQ7H.mjs";
import "./chunk-6CLQKVAU.mjs";
import "./chunk-KI7TOXBR.mjs";
import "./chunk-QBIQEVTU.mjs";
import "./chunk-WAXMT4IY.mjs";
import "./chunk-DYDGGABK.mjs";
import "./chunk-LGNTHZ5Y.mjs";
import "./chunk-CN7JXSGW.mjs";
import "./chunk-AO2USOOE.mjs";
import "./chunk-KWWLGCSY.mjs";
import "./chunk-5AFMB7XQ.mjs";
import "./chunk-5U6AZEO7.mjs";
import {
  promotionsQueryKeys,
  useDeletePromotion,
  usePromotion,
  usePromotionRules
} from "./chunk-A63RZVX6.mjs";
import "./chunk-HBXV7ENS.mjs";
import "./chunk-ZBMENGLV.mjs";
import "./chunk-LGLPAUIO.mjs";
import "./chunk-CDORR33H.mjs";
import "./chunk-DE44HCDH.mjs";
import "./chunk-MUOBXNL6.mjs";
import "./chunk-BEQ5XTWH.mjs";
import "./chunk-HP2JH45P.mjs";
import "./chunk-VCSSQVPD.mjs";
import "./chunk-SQDIZZDW.mjs";
import "./chunk-LA2ILUXH.mjs";
import "./chunk-7AXHHXCX.mjs";
import {
  queryClient
} from "./chunk-FXYH54JP.mjs";
import "./chunk-774WSTCC.mjs";
import {
  sdk
} from "./chunk-NFEK63OE.mjs";
import "./chunk-QZ7TP4HQ.mjs";

// src/routes/promotions/promotion-detail/breadcrumb.tsx
import { jsx } from "react/jsx-runtime";
var PromotionDetailBreadcrumb = (props) => {
  const { id } = props.params || {};
  const { promotion } = usePromotion(id, {
    initialData: props.data,
    enabled: Boolean(id)
  });
  if (!promotion) {
    return null;
  }
  return /* @__PURE__ */ jsx("span", { children: promotion.code });
};

// src/routes/promotions/promotion-detail/loader.ts
var promotionDetailQuery = (id) => ({
  queryKey: promotionsQueryKeys.detail(id),
  queryFn: async () => sdk.admin.promotion.retrieve(id)
});
var promotionLoader = async ({ params }) => {
  const id = params.id;
  const query = promotionDetailQuery(id);
  return queryClient.ensureQueryData(query);
};

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

// src/routes/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 as jsx2, 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__ */ jsx2(Text, { size: "small", weight: "plus", className: "text-ui-fg-base", children: campaign.name }),
      /* @__PURE__ */ jsx2(Text, { size: "small", weight: "plus", children: "\xB7" }),
      /* @__PURE__ */ jsx2(Text, { size: "small", weight: "plus", children: campaign.campaign_identifier })
    ] }),
    /* @__PURE__ */ jsx2(
      DateRangeDisplay,
      {
        startsAt: campaign.starts_at,
        endsAt: campaign.ends_at,
        showTime: true
      }
    )
  ] });
};
var CampaignSection = ({
  campaign
}) => {
  const { t } = useTranslation();
  const { id } = useParams();
  const actions = [
    {
      label: t("actions.edit"),
      to: "add-to-campaign",
      icon: /* @__PURE__ */ jsx2(PencilSquare, {})
    }
  ];
  if (campaign) {
    actions.unshift({
      label: t("promotions.campaign.actions.goToCampaign"),
      to: `/campaigns/${campaign.id}`,
      icon: /* @__PURE__ */ jsx2(ArrowUpRightOnBox, {})
    });
  }
  return /* @__PURE__ */ jsxs(Container, { children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsx2(Heading, { level: "h2", children: t("promotions.fields.campaign") }),
      /* @__PURE__ */ jsx2(
        ActionMenu,
        {
          groups: [
            {
              actions
            }
          ]
        }
      )
    ] }),
    campaign ? /* @__PURE__ */ jsx2(CampaignDetailSection, { campaign }) : /* @__PURE__ */ jsx2(
      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"
      }
    )
  ] });
};

// src/routes/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";

// 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 jsx3, 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__ */ jsx3(Badge, { rounded: rounded ? "full" : "base", size: "2xsmall", children: item }, item);
        }),
        list.length > n && /* @__PURE__ */ jsx3("div", { className: "whitespace-nowrap", children: /* @__PURE__ */ jsx3(
          Tooltip,
          {
            content: /* @__PURE__ */ jsx3("ul", { children: list.slice(n).map((c) => /* @__PURE__ */ jsx3("li", { children: c }, c)) }),
            children: /* @__PURE__ */ jsx3(
              Badge,
              {
                rounded: rounded ? "full" : "base",
                size: "2xsmall",
                className: "cursor-default whitespace-nowrap",
                children: title
              }
            )
          }
        ) })
      ]
    }
  );
};

// src/routes/promotions/promotion-detail/components/promotion-conditions-section/promotion-conditions-section.tsx
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
function RuleBlock({ rule }) {
  return /* @__PURE__ */ jsx4("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__ */ jsx4(
      Badge2,
      {
        size: "2xsmall",
        className: "txt-compact-xsmall-plus tag-neutral-text mx-1 inline-block truncate",
        children: rule.attribute_label
      },
      "rule-attribute"
    ),
    /* @__PURE__ */ jsx4("span", { className: "txt-compact-2xsmall mx-1 inline-block", children: rule.operator_label }),
    /* @__PURE__ */ jsx4(
      BadgeListSummary,
      {
        inline: true,
        className: "!txt-compact-small-plus",
        list: (rule.field_type === "number" ? [rule.values] : rule.values?.map((v) => v.label) || []).filter(Boolean)
      }
    )
  ] }) });
}
var PromotionConditionsSection = ({
  rules,
  ruleType,
  applicationMethodTargetType
}) => {
  const { t } = useTranslation3();
  return /* @__PURE__ */ jsxs3(Container2, { className: "p-0", children: [
    /* @__PURE__ */ jsxs3("div", { className: "flex items-center justify-between px-6 py-4", children: [
      /* @__PURE__ */ jsx4("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx4(Heading2, { level: "h2", children: t(
        ruleType === "target-rules" ? `promotions.fields.conditions.${ruleType}.${applicationMethodTargetType}.title` : `promotions.fields.conditions.${ruleType}.title`
      ) }) }),
      /* @__PURE__ */ jsx4(
        ActionMenu,
        {
          groups: [
            {
              actions: [
                {
                  icon: /* @__PURE__ */ jsx4(PencilSquare2, {}),
                  label: t("actions.edit"),
                  to: `${ruleType}/edit`
                }
              ]
            }
          ]
        }
      )
    ] }),
    /* @__PURE__ */ jsxs3("div", { className: "text-ui-fg-subtle flex flex-col gap-2 px-6 pb-4 pt-2", children: [
      !rules.length && /* @__PURE__ */ jsx4(
        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"
        }
      ),
      rules.map((rule) => /* @__PURE__ */ jsx4(RuleBlock, { rule }, `${rule.id}-${rule.attribute}`))
    ] })
  ] });
};

// src/routes/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 { jsx as jsx5, 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
}) => {
  const { t } = useTranslation4();
  const prompt = usePrompt();
  const navigate = useNavigate();
  const { mutateAsync } = useDeletePromotion(promotion.id);
  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", children: [
    /* @__PURE__ */ jsxs4("div", { className: "flex items-center justify-between px-6 py-4", children: [
      /* @__PURE__ */ jsx5("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx5(Heading3, { children: promotion.code }) }),
      /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-x-2", children: [
        /* @__PURE__ */ jsx5(StatusBadge, { color, children: text }),
        /* @__PURE__ */ jsx5(
          ActionMenu,
          {
            groups: [
              {
                actions: [
                  {
                    icon: /* @__PURE__ */ jsx5(PencilSquare3, {}),
                    label: t("actions.edit"),
                    to: `/promotions/${promotion.id}/edit`
                  }
                ]
              },
              {
                actions: [
                  {
                    icon: /* @__PURE__ */ jsx5(Trash, {}),
                    label: t("actions.delete"),
                    onClick: handleDelete
                  }
                ]
              }
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.campaign") }),
      /* @__PURE__ */ jsx5(Text2, { size: "small", leading: "compact", className: "text-pretty", 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", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: t("fields.code") }),
      /* @__PURE__ */ jsx5(
        Copy,
        {
          content: promotion.code,
          className: "text-ui-tag-neutral-text",
          asChild: true,
          children: /* @__PURE__ */ jsx5(
            Badge3,
            {
              size: "2xsmall",
              rounded: "full",
              className: "cursor-pointer text-pretty",
              children: promotion.code
            }
          )
        }
      )
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.type") }),
      /* @__PURE__ */ jsx5(Text2, { size: "small", leading: "compact", className: "text-pretty capitalize", children: promotion.type })
    ] }),
    /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.value") }),
      /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-x-2", children: [
        /* @__PURE__ */ jsx5(Text2, { className: "inline", size: "small", leading: "compact", children: displayValue || "-" }),
        promotion?.application_method?.type === "fixed" && /* @__PURE__ */ jsx5(Badge3, { size: "2xsmall", rounded: "full", 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", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.allocation") }),
      /* @__PURE__ */ jsx5(Text2, { size: "small", leading: "compact", className: "text-pretty capitalize", 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", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.taxInclusive") }),
      /* @__PURE__ */ jsx5("div", { className: "flex items-center gap-x-2", children: /* @__PURE__ */ jsx5(Text2, { className: "inline", size: "small", leading: "compact", children: promotion.is_tax_inclusive ? t("fields.true") : t("fields.false") }) })
    ] }),
    typeof promotion.limit === "number" && /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
      /* @__PURE__ */ jsx5(Text2, { size: "small", weight: "plus", leading: "compact", children: "Usage Limit" }),
      /* @__PURE__ */ jsx5("div", { className: "flex items-center gap-x-2", children: /* @__PURE__ */ jsxs4(Text2, { className: "inline", size: "small", leading: "compact", children: [
        promotion.used || 0,
        " / ",
        promotion.limit
      ] }) })
    ] })
  ] });
};

// src/routes/promotions/promotion-detail/promotion-detail.tsx
import { jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
var PromotionDetail = () => {
  const initialData = useLoaderData();
  const { id } = useParams2();
  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);
  const { getWidgets } = useExtension();
  if (isLoading || !promotion) {
    return /* @__PURE__ */ jsx6(TwoColumnPageSkeleton, { mainSections: 3, sidebarSections: 1, showJSON: true });
  }
  return /* @__PURE__ */ jsxs5(
    TwoColumnPage,
    {
      data: promotion,
      widgets: {
        after: getWidgets("promotion.details.after"),
        before: getWidgets("promotion.details.before"),
        sideAfter: getWidgets("promotion.details.side.after"),
        sideBefore: getWidgets("promotion.details.side.before")
      },
      hasOutlet: true,
      showJSON: true,
      children: [
        /* @__PURE__ */ jsxs5(TwoColumnPage.Main, { children: [
          /* @__PURE__ */ jsx6(PromotionGeneralSection, { promotion }),
          /* @__PURE__ */ jsx6(PromotionConditionsSection, { rules: rules || [], ruleType: "rules" }),
          /* @__PURE__ */ jsx6(
            PromotionConditionsSection,
            {
              rules: targetRules || [],
              ruleType: "target-rules",
              applicationMethodTargetType: promotion.application_method?.target_type || "items"
            }
          ),
          promotion.type === "buyget" && /* @__PURE__ */ jsx6(
            PromotionConditionsSection,
            {
              rules: buyRules || [],
              ruleType: "buy-rules",
              applicationMethodTargetType: "items"
            }
          )
        ] }),
        /* @__PURE__ */ jsx6(TwoColumnPage.Sidebar, { children: /* @__PURE__ */ jsx6(CampaignSection, { campaign: promotion.campaign }) })
      ]
    }
  );
};
export {
  PromotionDetailBreadcrumb as Breadcrumb,
  PromotionDetail as Component,
  promotionLoader as loader
};
