import {
  getCollectionGallery,
  getCollectionIcon
} from "./chunk-42R54Z2X.js";
import {
  useProductTableColumns
} from "./chunk-AAKR2CB4.js";
import "./chunk-G64YUZSL.js";
import {
  useProductTableQuery
} from "./chunk-BEPAXQMU.js";
import "./chunk-FWPGOIFA.js";
import {
  useProductTableFilters
} from "./chunk-LFX6R456.js";
import "./chunk-DB6ANJBO.js";
import "./chunk-PHOCVOZ5.js";
import {
  SingleColumnPage
} from "./chunk-YV6MK4PZ.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-52KJSIHS.js";
import "./chunk-WIYFXWRI.js";
import {
  SingleColumnPageSkeleton
} from "./chunk-INNFZYX2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import "./chunk-LAVHRER2.js";
import "./chunk-QHQWEERJ.js";
import "./chunk-Y7QKP6QU.js";
import "./chunk-RLRWDHME.js";
import "./chunk-K5324KIX.js";
import "./chunk-FV76G2D7.js";
import "./chunk-GHDLST2P.js";
import "./chunk-YVYYOBAE.js";
import "./chunk-L2NJPDUD.js";
import "./chunk-P7UBW2EI.js";
import "./chunk-MR6REQKT.js";
import "./chunk-EPSDO2MJ.js";
import "./chunk-BHEU34FL.js";
import "./chunk-W34KDYZA.js";
import "./chunk-II3CLUXA.js";
import {
  collectionsQueryKeys,
  useCollection,
  useUpdateCollectionProducts
} from "./chunk-XKEYHWBS.js";
import "./chunk-J2XUEO2E.js";
import "./chunk-656G74I2.js";
import "./chunk-VWPNMEOE.js";
import "./chunk-O72AIZUN.js";
import "./chunk-VUWAOX7H.js";
import "./chunk-5DKUSZQ7.js";
import "./chunk-DIAWPVYS.js";
import "./chunk-4U2BLH45.js";
import {
  useProducts
} from "./chunk-YAUGB4LD.js";
import "./chunk-A5DJIOHN.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/collections/[id]/loader.tsx
import { getLinkQuery } from "@mercurjs/dashboard-shared";
var collectionDetailQuery = (id) => {
  const query = getLinkQuery("collection");
  return {
    queryKey: collectionsQueryKeys.detail(id, query),
    queryFn: async () => sdk.vendor.collections.$id.query({ $id: id, ...query })
  };
};
var loader = async ({ params }) => {
  const id = params.id;
  const query = collectionDetailQuery(id);
  return queryClient.ensureQueryData(query);
};

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

// src/pages/collections/[id]/_components/collection-general-section/collection-general-section.tsx
import { Container, Heading, Text } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { DisplayExtensionZone, DisplayField } from "@mercurjs/dashboard-shared";
import { jsx, jsxs } from "react/jsx-runtime";
var GENERAL_FIELD_IDS = ["title", "handle"];
var CollectionGeneralSection = ({
  collection
}) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", children: [
    /* @__PURE__ */ jsx(DisplayField, { model: "collection", zone: "general", id: "title", data: collection, children: /* @__PURE__ */ jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx(Heading, { children: collection.title }) }) }),
    /* @__PURE__ */ jsx(DisplayField, { model: "collection", zone: "general", id: "handle", data: collection, children: /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4", children: [
      /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", weight: "plus", children: t("fields.handle") }),
      /* @__PURE__ */ jsxs(Text, { size: "small", children: [
        "/",
        collection.handle
      ] })
    ] }) }),
    /* @__PURE__ */ jsx(
      DisplayExtensionZone,
      {
        model: "collection",
        zone: "general",
        data: collection,
        builtInFieldIds: GENERAL_FIELD_IDS
      }
    )
  ] });
};

// src/pages/collections/[id]/_components/collection-icon-section/collection-icon-section.tsx
import { Container as Container2, Heading as Heading2, Text as Text2 } from "@medusajs/ui";
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 CollectionIconSection = ({
  collection
}) => {
  const { t } = useTranslation2();
  const icon = getCollectionIcon(collection.media_images);
  return /* @__PURE__ */ jsxs2(Container2, { className: "divide-y p-0", "data-testid": "collection-icon-section", children: [
    /* @__PURE__ */ jsx2("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx2(Heading2, { children: t("collections.icon.label") }) }),
    /* @__PURE__ */ jsx2("div", { className: "px-6 py-4", children: icon ? /* @__PURE__ */ jsx2("div", { className: "bg-ui-bg-base shadow-elevation-card-rest relative aspect-square w-24 overflow-hidden rounded-[8px]", children: /* @__PURE__ */ jsx2(
      "img",
      {
        src: icon.url,
        alt: "",
        className: "size-full object-contain object-center p-2",
        "data-testid": "collection-icon-section-image"
      }
    ) }) : /* @__PURE__ */ jsxs2("div", { className: "flex flex-col items-center justify-center gap-y-1 py-8", children: [
      /* @__PURE__ */ jsx2(
        Text2,
        {
          size: "small",
          leading: "compact",
          weight: "plus",
          className: "text-ui-fg-subtle",
          children: t("collections.icon.empty.header")
        }
      ),
      /* @__PURE__ */ jsx2(Text2, { size: "small", className: "text-ui-fg-muted", children: t("collections.icon.empty.description") })
    ] }) }),
    /* @__PURE__ */ jsx2(DisplayExtensionZone2, { model: "collection", zone: "icon", data: collection })
  ] });
};

// src/pages/collections/[id]/_components/collection-media-section/collection-media-section.tsx
import { ThumbnailBadge } from "@medusajs/icons";
import { DisplayExtensionZone as DisplayExtensionZone3, ListBadge } from "@mercurjs/dashboard-shared";
import { Container as Container3, Heading as Heading3, Text as Text3, Tooltip } from "@medusajs/ui";
import { useTranslation as useTranslation3 } from "react-i18next";
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
var CollectionMediaSection = ({
  collection
}) => {
  const { t } = useTranslation3();
  const gallery = getCollectionGallery(collection.media_images);
  return /* @__PURE__ */ jsxs3(Container3, { className: "divide-y p-0", "data-testid": "collection-media-section", children: [
    /* @__PURE__ */ jsx3("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx3(Heading3, { children: t("collections.media.label") }) }),
    /* @__PURE__ */ jsx3("div", { className: "px-6 py-4", children: gallery.length > 0 ? /* @__PURE__ */ jsx3("div", { className: "grid grid-cols-[repeat(auto-fill,minmax(96px,1fr))] gap-4", children: gallery.map((image) => /* @__PURE__ */ jsxs3(
      "div",
      {
        className: "shadow-elevation-card-rest relative aspect-square size-full overflow-hidden rounded-[8px]",
        "data-testid": `collection-media-section-item-${image.id}`,
        children: [
          /* @__PURE__ */ jsx3(
            "img",
            {
              src: image.url,
              alt: "",
              className: "size-full object-cover object-center"
            }
          ),
          (image.is_thumbnail || image.is_banner) && /* @__PURE__ */ jsxs3("div", { className: "absolute left-2 top-2 flex items-center gap-x-1", children: [
            image.is_thumbnail && /* @__PURE__ */ jsx3(Tooltip, { content: t("collections.media.thumbnail"), children: /* @__PURE__ */ jsx3(ThumbnailBadge, {}) }),
            image.is_banner && /* @__PURE__ */ jsx3(Tooltip, { content: t("collections.media.banner"), children: /* @__PURE__ */ jsx3(ListBadge, {}) })
          ] })
        ]
      },
      image.id
    )) }) : /* @__PURE__ */ jsxs3("div", { className: "flex flex-col items-center justify-center gap-y-1 py-8", children: [
      /* @__PURE__ */ jsx3(
        Text3,
        {
          size: "small",
          leading: "compact",
          weight: "plus",
          className: "text-ui-fg-subtle",
          children: t("collections.media.empty.header")
        }
      ),
      /* @__PURE__ */ jsx3(Text3, { size: "small", className: "text-ui-fg-muted", children: t("collections.media.empty.description") })
    ] }) }),
    /* @__PURE__ */ jsx3(DisplayExtensionZone3, { model: "collection", zone: "media", data: collection })
  ] });
};

// src/pages/collections/[id]/_components/collection-product-section/collection-product-section.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { Checkbox, Container as Container4, Heading as Heading4, toast, usePrompt } from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation4 } from "react-i18next";
import { DisplayExtensionZone as DisplayExtensionZone4 } from "@mercurjs/dashboard-shared";
import { keepPreviousData } from "@tanstack/react-query";
import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
var PAGE_SIZE = 10;
var CollectionProductSection = ({
  collection
}) => {
  const { t } = useTranslation4();
  const { searchParams, raw } = useProductTableQuery({
    pageSize: PAGE_SIZE
  });
  const { products, count, isLoading, isError, error } = useProducts(
    {
      ...searchParams,
      collection_id: collection.id
    },
    { placeholderData: keepPreviousData }
  );
  const filters = useProductTableFilters(["collections"]);
  const columns = useColumns();
  const { table } = useDataTable({
    data: products ?? [],
    columns,
    getRowId: (row) => row.id,
    count,
    enablePagination: true,
    enableRowSelection: true,
    pageSize: PAGE_SIZE,
    meta: {
      collectionId: collection.id
    }
  });
  const prompt = usePrompt();
  const { mutateAsync } = useUpdateCollectionProducts(collection.id);
  const handleRemove = async (selection) => {
    const ids = Object.keys(selection);
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("collections.removeProductsWarning", {
        count: ids.length
      }),
      confirmText: t("actions.remove"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(
      {
        remove: ids
      },
      {
        onSuccess: () => {
          toast.success(
            t("collections.products.remove.successToast", {
              count: ids.length
            })
          );
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  };
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs4(Container4, { className: "divide-y p-0", children: [
    /* @__PURE__ */ jsx4("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx4(Heading4, { level: "h2", children: t("products.domain") }) }),
    /* @__PURE__ */ jsx4(
      _DataTable,
      {
        table,
        columns,
        search: true,
        pagination: true,
        pageSize: PAGE_SIZE,
        navigateTo: ({ original }) => `/products/${original.id}`,
        count,
        filters,
        isLoading,
        orderBy: [
          { key: "title", label: t("fields.title") },
          {
            key: "created_at",
            label: t("fields.createdAt")
          },
          {
            key: "updated_at",
            label: t("fields.updatedAt")
          }
        ],
        queryObject: raw,
        commands: [
          {
            action: handleRemove,
            label: t("actions.remove"),
            shortcut: "r"
          }
        ],
        noRecords: {
          message: t("collections.products.list.noRecordsMessage")
        }
      }
    ),
    /* @__PURE__ */ jsx4(DisplayExtensionZone4, { model: "collection", zone: "products", data: collection })
  ] });
};
var ProductActions = ({
  product,
  collectionId
}) => {
  const { t } = useTranslation4();
  const prompt = usePrompt();
  const { mutateAsync } = useUpdateCollectionProducts(collectionId);
  const handleRemove = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("collections.removeSingleProductWarning", {
        title: product.title
      }),
      confirmText: t("actions.remove"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await mutateAsync(
      {
        remove: [product.id]
      },
      {
        onSuccess: () => {
          toast.success(
            t("collections.products.remove.successToast", {
              count: 1
            })
          );
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  };
  return /* @__PURE__ */ jsx4(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx4(PencilSquare, {}),
              label: t("actions.edit"),
              to: `/products/${product.id}/edit`
            }
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx4(Trash, {}),
              label: t("actions.remove"),
              onClick: handleRemove
            }
          ]
        }
      ]
    }
  );
};
var columnHelper = createColumnHelper();
var useColumns = () => {
  const columns = useProductTableColumns();
  return useMemo(
    () => [
      columnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx4(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx4(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...columns,
      columnHelper.display({
        id: "actions",
        cell: ({ row, table }) => {
          const { collectionId } = table.options.meta;
          return /* @__PURE__ */ jsx4(
            ProductActions,
            {
              product: row.original,
              collectionId
            }
          );
        }
      })
    ],
    [columns]
  );
};

// src/pages/collections/[id]/collection-detail-page.tsx
import { Fragment, jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const initialData = useLoaderData();
  const { id } = useParams();
  const { collection, isLoading, isError, error } = useCollection(
    id,
    useLinkQuery("collection"),
    {
      initialData
    }
  );
  if (isLoading || !collection) {
    return /* @__PURE__ */ jsx5(SingleColumnPageSkeleton, { sections: 4 });
  }
  if (isError) throw error;
  return /* @__PURE__ */ jsx5(Fragment, { children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx5(SingleColumnPage, { data: collection, children: /* @__PURE__ */ jsxs5(WidgetZone, { id: "collections.detail.main", data: collection, children: [
    /* @__PURE__ */ jsx5(CollectionGeneralSection, { collection }),
    /* @__PURE__ */ jsx5(CollectionMediaSection, { collection }),
    /* @__PURE__ */ jsx5(CollectionIconSection, { collection }),
    /* @__PURE__ */ jsx5(CollectionProductSection, { collection })
  ] }) }) });
};
var CollectionDetailPage = Object.assign(Root, {
  GeneralSection: CollectionGeneralSection,
  MediaSection: CollectionMediaSection,
  IconSection: CollectionIconSection,
  ProductSection: CollectionProductSection
});
export {
  CollectionDetailPage,
  loader
};
