import {
  formatPercentage
} from "./chunk-QDPLZDMX.js";
import {
  DateRangeDisplay
} from "./chunk-BWGPN6XV.js";
import {
  StatusCell
} from "./chunk-S5KWI3MM.js";
import "./chunk-FMFVTERD.js";
import "./chunk-DB6ANJBO.js";
import "./chunk-ZZ4JC4WE.js";
import {
  TwoColumnPage
} from "./chunk-YV6MK4PZ.js";
import {
  NoRecords
} from "./chunk-CVXKASWV.js";
import "./chunk-LRD242C7.js";
import {
  TwoColumnPageSkeleton
} from "./chunk-INNFZYX2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import "./chunk-LAVHRER2.js";
import "./chunk-QHQWEERJ.js";
import "./chunk-Y7QKP6QU.js";
import {
  promotionsQueryKeys,
  useDeletePromotion,
  usePromotion,
  usePromotionRules
} from "./chunk-MR6REQKT.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  fetchQuery
} from "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/promotions/[id]/loader.tsx
import { getLinkQuery } from "@mercurjs/dashboard-shared";
var promotionDetailQuery = (id) => {
  const query = getLinkQuery("promotion", "+status");
  return {
    queryKey: promotionsQueryKeys.detail(id, query),
    queryFn: async () => fetchQuery(`/vendor/promotions/${id}`, {
      method: "GET",
      query
    })
  };
};
var loader = async ({ params }) => {
  const id = params.id;
  return queryClient.ensureQueryData(promotionDetailQuery(id));
};

// src/pages/promotions/[id]/promotion-detail-page.tsx
import { Children } from "react";
import { useLoaderData, useParams as useParams2 } from "react-router-dom";
import { useLinkQuery, WidgetZone } from "@mercurjs/dashboard-shared";

// src/pages/promotions/[id]/_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 { DisplayExtensionZone } from "@mercurjs/dashboard-shared";
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
}) => {
  const { t } = useTranslation();
  const { id } = useParams();
  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, { children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("promotions.fields.campaign") }),
      /* @__PURE__ */ jsx(
        ActionMenu,
        {
          groups: [
            {
              actions
            }
          ]
        }
      )
    ] }),
    campaign ? /* @__PURE__ */ jsx(CampaignDetailSection, { campaign }) : /* @__PURE__ */ jsx(
      NoRecords,
      {
        className: "h-[180px] pt-4 text-center",
        title: t("promotions.campaignSection.noRecordsTitle"),
        message: t("promotions.campaignSection.noRecordsMessage"),
        action: {
          to: `/promotions/${id}/add-to-campaign`,
          label: t("promotions.campaignSection.addToCampaign")
        },
        buttonVariant: "transparentIconLeft"
      }
    ),
    /* @__PURE__ */ jsx(DisplayExtensionZone, { model: "promotion", zone: "campaign", data: campaign })
  ] });
};

// src/pages/promotions/[id]/_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 { DisplayExtensionZone as DisplayExtensionZone2 } from "@mercurjs/dashboard-shared";

// 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/[id]/_components/promotion-conditions-section/promotion-conditions-section.tsx
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
function RuleBlock({ rule }) {
  const getValuesList = () => {
    if (rule.field_type === "number") {
      return Array.isArray(rule.values) ? rule.values.map((v) => String(v.value || v)) : [String(rule.values)];
    }
    return rule.values?.map((v) => v.label || v.value || String(v)).filter(Boolean) || [];
  };
  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: getValuesList()
      }
    )
  ] }) });
}
var PromotionConditionsSection = ({
  rules,
  ruleType
}) => {
  const { t } = useTranslation3();
  const translationKey = `promotions.fields.conditions.${ruleType}.title`;
  return /* @__PURE__ */ jsxs3(Container2, { className: "p-0", children: [
    /* @__PURE__ */ jsxs3("div", { className: "flex items-center justify-between px-6 py-4", children: [
      /* @__PURE__ */ jsx3("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx3(Heading2, { children: t(translationKey) }) }),
      /* @__PURE__ */ jsx3(
        ActionMenu,
        {
          groups: [
            {
              actions: [
                {
                  icon: /* @__PURE__ */ jsx3(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__ */ 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"
        }
      ),
      rules.map((rule) => /* @__PURE__ */ jsx3(RuleBlock, { rule }, `${rule.id}-${rule.attribute}`))
    ] }),
    /* @__PURE__ */ jsx3(
      DisplayExtensionZone2,
      {
        model: "promotion",
        zone: `conditions-${ruleType}`,
        data: rules
      }
    )
  ] });
};

// src/pages/promotions/[id]/_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, Text as Text2, usePrompt } from "@medusajs/ui";
import { useTranslation as useTranslation4 } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { DisplayExtensionZone as DisplayExtensionZone3, DisplayField } from "@mercurjs/dashboard-shared";

// src/lib/format-currency.ts
var formatCurrency = (amount, currency) => {
  return new Intl.NumberFormat(void 0, {
    style: "currency",
    currency,
    signDisplay: "auto"
  }).format(amount);
};

// src/pages/promotions/[id]/_components/promotion-general-section/promotion-general-section.tsx
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
}) => {
  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 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__ */ jsx4("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx4(DisplayField, { model: "promotion", zone: "general", id: "code", data: promotion, children: /* @__PURE__ */ jsx4(Heading3, { children: promotion.code }) }) }),
      /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-x-2", children: [
        /* @__PURE__ */ jsx4(
          DisplayField,
          {
            model: "promotion",
            zone: "general",
            id: "status",
            data: promotion,
            children: /* @__PURE__ */ jsx4(StatusCell, { promotion })
          }
        ),
        /* @__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
                  }
                ]
              }
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsx4(
      DisplayField,
      {
        model: "promotion",
        zone: "general",
        id: "is_automatic",
        data: promotion,
        children: /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
          /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.method") }),
          /* @__PURE__ */ jsx4(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__ */ jsx4(
      DisplayField,
      {
        model: "promotion",
        zone: "general",
        id: "code_value",
        data: promotion,
        children: /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4", children: [
          /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", children: t("fields.code") }),
          /* @__PURE__ */ jsx4(
            Copy,
            {
              content: promotion.code,
              className: "text-ui-tag-neutral-text",
              asChild: true,
              children: /* @__PURE__ */ jsx4(
                Badge3,
                {
                  size: "2xsmall",
                  rounded: "full",
                  className: "cursor-pointer text-pretty",
                  children: promotion.code
                }
              )
            }
          )
        ] })
      }
    ),
    /* @__PURE__ */ jsx4(DisplayField, { model: "promotion", zone: "general", id: "type", data: promotion, children: /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.type") }),
      /* @__PURE__ */ jsx4(
        Text2,
        {
          size: "small",
          leading: "compact",
          className: "text-pretty capitalize",
          children: promotion.type
        }
      )
    ] }) }),
    /* @__PURE__ */ jsx4(DisplayField, { model: "promotion", zone: "general", id: "value", data: promotion, children: /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
      /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.value") }),
      /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-x-2", children: [
        /* @__PURE__ */ jsx4(Text2, { className: "inline", size: "small", leading: "compact", children: displayValue || "-" }),
        promotion?.application_method?.type === "fixed" && /* @__PURE__ */ jsx4(Badge3, { size: "2xsmall", rounded: "full", children: promotion?.application_method?.currency_code?.toUpperCase() })
      ] })
    ] }) }),
    /* @__PURE__ */ jsx4(
      DisplayField,
      {
        model: "promotion",
        zone: "general",
        id: "allocation",
        data: promotion,
        children: /* @__PURE__ */ jsxs4("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4", children: [
          /* @__PURE__ */ jsx4(Text2, { size: "small", weight: "plus", leading: "compact", children: t("promotions.fields.allocation") }),
          /* @__PURE__ */ jsx4(
            Text2,
            {
              size: "small",
              leading: "compact",
              className: "text-pretty capitalize",
              children: promotion.application_method?.allocation
            }
          )
        ] })
      }
    ),
    /* @__PURE__ */ jsx4(
      DisplayExtensionZone3,
      {
        model: "promotion",
        zone: "general",
        data: promotion,
        builtInFieldIds: [
          "code",
          "status",
          "is_automatic",
          "code_value",
          "type",
          "value",
          "allocation"
        ]
      }
    )
  ] });
};

// src/pages/promotions/[id]/promotion-detail-page.tsx
import { Fragment, jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const initialData = useLoaderData();
  const { id } = useParams2();
  const linkQuery = useLinkQuery("promotion", "+status");
  const { promotion, isLoading } = usePromotion(id, linkQuery, {
    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 /* @__PURE__ */ jsx5(Fragment, { children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs5(TwoColumnPage, { data: promotion, hasOutlet: true, children: [
    /* @__PURE__ */ jsx5(TwoColumnPage.Main, { children: /* @__PURE__ */ jsxs5(WidgetZone, { id: "promotions.detail.main", data: promotion, children: [
      /* @__PURE__ */ jsx5(PromotionGeneralSection, { promotion }),
      /* @__PURE__ */ jsx5(PromotionConditionsSection, { rules: rules || [], ruleType: "rules" }),
      /* @__PURE__ */ jsx5(
        PromotionConditionsSection,
        {
          rules: targetRules || [],
          ruleType: "target-rules"
        }
      ),
      promotion.type === "buyget" && /* @__PURE__ */ jsx5(
        PromotionConditionsSection,
        {
          rules: buyRules || [],
          ruleType: "buy-rules"
        }
      )
    ] }) }),
    /* @__PURE__ */ jsx5(TwoColumnPage.Sidebar, { children: /* @__PURE__ */ jsx5(WidgetZone, { id: "promotions.detail.side", data: promotion, 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,
  loader
};
