import {
  OFFER_VARIANT_DETAIL_FIELDS
} from "./chunk-XQUF3WIS.js";
import {
  getLocaleAmount
} from "./chunk-XCHKUSUU.js";
import {
  PlaceholderCell
} from "./chunk-PHOCVOZ5.js";
import "./chunk-IQPN4PZJ.js";
import {
  SectionRow
} from "./chunk-4TLP3TAA.js";
import {
  TwoColumnPage
} from "./chunk-YV6MK4PZ.js";
import "./chunk-FQERBIVS.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import "./chunk-YN3FGNJM.js";
import "./chunk-KCHYLLVG.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 {
  useOffer
} from "./chunk-5DKUSZQ7.js";
import "./chunk-YAUGB4LD.js";
import "./chunk-A5DJIOHN.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/offers/[id]/variants/[offer_id]/offer-variant-detail-page.tsx
import { Children } from "react";
import { useLoaderData, useParams } from "react-router-dom";
import { WidgetZone, useLinkQuery } from "@mercurjs/dashboard-shared";

// src/pages/offers/[id]/_components/offer-inventory-section.tsx
import { Buildings } from "@medusajs/icons";
import { Container, Heading, Text } from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { DisplayExtensionZone } from "@mercurjs/dashboard-shared";
import { jsx, jsxs } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var PAGE_SIZE = 20;
var computeAvailable = (row) => {
  const levels = row.location_levels ?? [];
  let quantity = 0;
  let locations = 0;
  for (const level of levels) {
    if (level.available_quantity != null) {
      quantity += level.available_quantity;
    } else {
      const stocked = level.stocked_quantity ?? 0;
      const reserved = level.reserved_quantity ?? 0;
      quantity += Math.max(0, stocked - reserved);
    }
    locations += 1;
  }
  return { quantity, locations };
};
var InventoryActions = ({ item }) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(Buildings, {}),
              label: t("offers.detail.goToInventoryItem"),
              to: `/inventory/${item.id}`
            }
          ]
        }
      ]
    }
  );
};
var OfferInventorySection = ({ offer }) => {
  const { t } = useTranslation();
  const inventoryItems = useMemo(() => {
    const links = offer.inventory_item_link ?? [];
    return links.map((link) => {
      const item = link.inventory_item;
      if (!item?.id) return null;
      return {
        id: item.id,
        title: item.title,
        sku: item.sku,
        required_quantity: link.required_quantity,
        location_levels: item.location_levels
      };
    }).filter((x) => x !== null);
  }, [offer.inventory_item_link]);
  const columns = useMemo(
    () => [
      columnHelper.accessor("title", {
        header: t("fields.title"),
        cell: ({ getValue }) => {
          const title = getValue();
          if (!title) return /* @__PURE__ */ jsx(PlaceholderCell, {});
          return /* @__PURE__ */ jsx("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx("span", { className: "truncate", children: title }) });
        }
      }),
      columnHelper.accessor("sku", {
        header: t("offers.fields.sku"),
        cell: ({ getValue }) => {
          const sku = getValue();
          if (!sku) return /* @__PURE__ */ jsx(PlaceholderCell, {});
          return /* @__PURE__ */ jsx("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx("span", { className: "truncate", children: sku }) });
        }
      }),
      columnHelper.accessor("required_quantity", {
        header: t("offers.fields.requiredQuantity"),
        cell: ({ getValue }) => {
          const value = getValue();
          if (value == null) return /* @__PURE__ */ jsx(PlaceholderCell, {});
          return /* @__PURE__ */ jsx("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx("span", { className: "truncate", children: value }) });
        }
      }),
      columnHelper.display({
        id: "inventory",
        header: t("fields.inventory"),
        cell: ({ row }) => {
          const { quantity, locations } = computeAvailable(row.original);
          if (locations === 0) return /* @__PURE__ */ jsx(PlaceholderCell, {});
          const text = `${quantity} available at ${locations} ${locations === 1 ? "location" : "locations"}`;
          return /* @__PURE__ */ jsx("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx(
            Text,
            {
              size: "small",
              className: quantity === 0 ? "text-ui-fg-error truncate" : "truncate",
              children: text
            }
          ) });
        }
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx(InventoryActions, { item: row.original })
      })
    ],
    [t]
  );
  const { table } = useDataTable({
    data: inventoryItems,
    columns,
    count: inventoryItems.length,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  return /* @__PURE__ */ jsxs(
    Container,
    {
      className: "divide-y p-0",
      "data-testid": "offer-detail-inventory-section",
      children: [
        /* @__PURE__ */ jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: t("offers.detail.inventoryItems") }) }),
        inventoryItems.length === 0 ? /* @__PURE__ */ jsx("div", { className: "px-6 py-8", children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: t("offers.detail.noInventoryItems") }) }) : /* @__PURE__ */ jsx(
          _DataTable,
          {
            table,
            columns,
            pageSize: PAGE_SIZE,
            count: inventoryItems.length,
            navigateTo: (row) => `/inventory/${row.id}`
          }
        ),
        /* @__PURE__ */ jsx(DisplayExtensionZone, { model: "offer", zone: "inventory", data: offer })
      ]
    }
  );
};

// src/pages/offers/[id]/_components/offer-pricing-section.tsx
import { CurrencyDollar } from "@medusajs/icons";
import { Button, Container as Container2, Heading as Heading2 } from "@medusajs/ui";
import { useState } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
import { DisplayExtensionZone as DisplayExtensionZone2 } from "@mercurjs/dashboard-shared";
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var PAGE_STEP = 3;
var isBaseRow = (price) => {
  const rules = price.price_rules ?? [];
  const extraRules = rules.filter((r) => r.attribute !== "offer_id");
  return extraRules.length === 0;
};
var OfferPricingSection = ({ offer }) => {
  const { t } = useTranslation2();
  const prices = (offer.prices ?? []).filter(isBaseRow).sort((a, b) => (a.currency_code ?? "").localeCompare(b.currency_code ?? ""));
  const hasPrices = prices.length > 0;
  const [pageSize, setPageSize] = useState(PAGE_STEP);
  const displayPrices = prices.slice(0, pageSize);
  return /* @__PURE__ */ jsxs2(
    Container2,
    {
      className: "flex flex-col divide-y p-0",
      "data-testid": "offer-detail-prices-section",
      children: [
        /* @__PURE__ */ jsxs2("div", { className: "flex items-center justify-between px-6 py-4", children: [
          /* @__PURE__ */ jsx2(Heading2, { level: "h2", children: t("labels.prices") }),
          /* @__PURE__ */ jsx2(
            ActionMenu,
            {
              groups: [
                {
                  actions: [
                    {
                      label: t("actions.edit"),
                      icon: /* @__PURE__ */ jsx2(CurrencyDollar, {}),
                      to: "pricing"
                    }
                  ]
                }
              ]
            }
          )
        ] }),
        !hasPrices && /* @__PURE__ */ jsx2(NoRecords, { className: "h-60" }),
        displayPrices.map((price) => /* @__PURE__ */ jsxs2(
          "div",
          {
            className: "txt-small text-ui-fg-subtle flex justify-between px-6 py-4",
            children: [
              /* @__PURE__ */ jsx2("span", { className: "font-medium", children: price.currency_code.toUpperCase() }),
              /* @__PURE__ */ jsx2("span", { children: getLocaleAmount(price.amount, price.currency_code) })
            ]
          },
          price.id ?? `${price.currency_code}-${price.amount}`
        )),
        hasPrices && prices.length > PAGE_STEP && /* @__PURE__ */ jsx2("div", { className: "txt-small text-ui-fg-subtle flex items-center justify-end px-6 py-4", children: /* @__PURE__ */ jsx2(
          Button,
          {
            onClick: () => setPageSize((p) => p + PAGE_STEP),
            disabled: pageSize >= prices.length,
            className: "-mr-3 text-blue-500",
            variant: "transparent",
            children: t("actions.showMore")
          }
        ) }),
        /* @__PURE__ */ jsx2(DisplayExtensionZone2, { model: "offer", zone: "prices", data: offer })
      ]
    }
  );
};

// src/pages/offers/[id]/variants/[offer_id]/_components/offer-variant-general-section.tsx
import { PencilSquare } from "@medusajs/icons";
import { Badge, Container as Container3, Heading as Heading3, Text as Text2 } from "@medusajs/ui";
import { useTranslation as useTranslation3 } from "react-i18next";
import { DisplayExtensionZone as DisplayExtensionZone3 } from "@mercurjs/dashboard-shared";
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
var OfferVariantGeneralSection = ({
  offer
}) => {
  const { t } = useTranslation3();
  const variant = offer.product_variant;
  const options = variant?.options ?? [];
  return /* @__PURE__ */ jsxs3(Container3, { className: "divide-y p-0", children: [
    /* @__PURE__ */ jsxs3("div", { className: "flex items-start justify-between px-6 py-4", children: [
      /* @__PURE__ */ jsxs3("div", { className: "flex flex-col", children: [
        /* @__PURE__ */ jsx3(Heading3, { children: variant?.title ?? "-" }),
        /* @__PURE__ */ jsx3(Text2, { size: "small", className: "text-ui-fg-subtle", children: t("offers.detail.offerVariant") })
      ] }),
      /* @__PURE__ */ jsx3(
        ActionMenu,
        {
          groups: [
            {
              actions: [
                {
                  label: t("actions.edit"),
                  to: "edit",
                  icon: /* @__PURE__ */ jsx3(PencilSquare, {})
                }
              ]
            }
          ]
        }
      )
    ] }),
    /* @__PURE__ */ jsx3(SectionRow, { title: t("offers.fields.sku"), value: offer.sku ?? "-" }),
    options.map((opt) => /* @__PURE__ */ jsx3(
      SectionRow,
      {
        title: opt.option?.title ?? "-",
        value: opt.value ? /* @__PURE__ */ jsx3(Badge, { size: "2xsmall", children: opt.value }) : "-"
      },
      opt.id ?? opt.option?.id ?? opt.value ?? ""
    )),
    /* @__PURE__ */ jsx3(DisplayExtensionZone3, { model: "offer", zone: "variant-general", data: offer })
  ] });
};

// src/pages/offers/[id]/variants/[offer_id]/_components/offer-variant-shipping-section.tsx
import { Buildings as Buildings2, PencilSquare as PencilSquare2, TriangleRightMini } from "@medusajs/icons";
import { Container as Container4, Heading as Heading4 } from "@medusajs/ui";
import { useTranslation as useTranslation4 } from "react-i18next";
import { Link } from "react-router-dom";
import { DisplayExtensionZone as DisplayExtensionZone4 } from "@mercurjs/dashboard-shared";
import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
var OfferVariantShippingSection = ({
  offer
}) => {
  const { t } = useTranslation4();
  const profile = offer.shipping_profile;
  return /* @__PURE__ */ jsxs4(Container4, { className: "p-0", children: [
    /* @__PURE__ */ jsxs4("div", { className: "flex items-center justify-between px-6 py-4", children: [
      /* @__PURE__ */ jsx4(Heading4, { level: "h2", children: t("offers.detail.shippingConfiguration") }),
      /* @__PURE__ */ jsx4(
        ActionMenu,
        {
          groups: [
            {
              actions: [
                {
                  label: t("actions.edit"),
                  to: "shipping",
                  icon: /* @__PURE__ */ jsx4(PencilSquare2, {})
                }
              ]
            }
          ]
        }
      )
    ] }),
    profile?.name ? /* @__PURE__ */ jsx4("div", { className: "txt-small flex flex-col gap-2 px-2 pb-2", children: /* @__PURE__ */ jsx4(
      Link,
      {
        to: `/settings/locations/shipping-profiles/${profile.id}`,
        className: "outline-none focus-within:shadow-borders-interactive-with-focus rounded-md [&:hover>div]:bg-ui-bg-component-hover",
        "data-testid": "offer-variant-shipping-link",
        children: /* @__PURE__ */ jsx4("div", { className: "shadow-elevation-card-rest bg-ui-bg-component rounded-md px-4 py-2 transition-colors", children: /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-3", children: [
          /* @__PURE__ */ jsx4("div", { className: "shadow-elevation-card-rest text-ui-fg-subtle flex size-7 items-center justify-center rounded-md", children: /* @__PURE__ */ jsx4(Buildings2, {}) }),
          /* @__PURE__ */ jsxs4("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
            /* @__PURE__ */ jsx4("span", { className: "text-ui-fg-base truncate font-medium", children: profile.name }),
            profile.type && /* @__PURE__ */ jsx4("span", { className: "text-ui-fg-subtle truncate", children: profile.type })
          ] }),
          /* @__PURE__ */ jsx4("div", { className: "size-7 flex items-center justify-center", children: /* @__PURE__ */ jsx4(TriangleRightMini, { className: "text-ui-fg-muted rtl:rotate-180" }) })
        ] }) })
      }
    ) }) : /* @__PURE__ */ jsx4(NoRecords, { className: "h-40" }),
    /* @__PURE__ */ jsx4(DisplayExtensionZone4, { model: "offer", zone: "shipping", data: offer })
  ] });
};

// src/pages/offers/[id]/variants/[offer_id]/offer-variant-detail-page.tsx
import { Fragment, jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const { offer_id } = useParams();
  const initialData = useLoaderData();
  const query = useLinkQuery("offer", OFFER_VARIANT_DETAIL_FIELDS);
  const {
    offer,
    isPending: isLoading,
    isError,
    error
  } = useOffer(offer_id, query, { initialData });
  if (isError) {
    throw error;
  }
  if (isLoading || !offer) {
    return /* @__PURE__ */ jsx5(TwoColumnPageSkeleton, { mainSections: 2, sidebarSections: 2 });
  }
  const typed = offer;
  return /* @__PURE__ */ jsx5(Fragment, { children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs5(TwoColumnPage, { data: typed, hasOutlet: true, children: [
    /* @__PURE__ */ jsx5(TwoColumnPage.Main, { children: /* @__PURE__ */ jsxs5(WidgetZone, { id: "offer-variants.detail.main", data: typed, children: [
      /* @__PURE__ */ jsx5(OfferVariantGeneralSection, { offer: typed }),
      /* @__PURE__ */ jsx5(OfferInventorySection, { offer: typed })
    ] }) }),
    /* @__PURE__ */ jsx5(TwoColumnPage.Sidebar, { children: /* @__PURE__ */ jsxs5(WidgetZone, { id: "offer-variants.detail.side", data: typed, children: [
      /* @__PURE__ */ jsx5(OfferVariantShippingSection, { offer: typed }),
      /* @__PURE__ */ jsx5(OfferPricingSection, { offer: typed })
    ] }) })
  ] }) });
};
var OfferVariantDetailPage = Object.assign(Root, {
  Main: TwoColumnPage.Main,
  Sidebar: TwoColumnPage.Sidebar,
  General: OfferVariantGeneralSection,
  Inventory: OfferInventorySection,
  Shipping: OfferVariantShippingSection,
  Pricing: OfferPricingSection
});
var Component = Root;
export {
  Component,
  OfferVariantDetailPage
};
