import {
  ATTRIBUTE_DETAIL_FIELDS
} from "./chunk-LO7XX5M7.js";
import {
  TextCell
} from "./chunk-JHIMVLB2.js";
import {
  SectionRow
} from "./chunk-3XPXUIK3.js";
import "./chunk-PHOCVOZ5.js";
import "./chunk-E74OUNA3.js";
import "./chunk-FQERBIVS.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-KYPPQGGE.js";
import "./chunk-ZMC7BKLT.js";
import "./chunk-KAZ5BYTQ.js";
import {
  NoRecords
} from "./chunk-634XVU3D.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  SingleColumnPageSkeleton
} from "./chunk-INNFZYX2.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import "./chunk-LAVHRER2.js";
import "./chunk-Y7QKP6QU.js";
import "./chunk-HKDFWQ6Q.js";
import "./chunk-SESTTUVM.js";
import "./chunk-UZWFZMOX.js";
import "./chunk-XQNVC4Q7.js";
import "./chunk-PBQ5M44A.js";
import "./chunk-OH4AABUT.js";
import "./chunk-YKDEE7ZL.js";
import "./chunk-AEMDNTW5.js";
import "./chunk-LYD3C73Z.js";
import {
  attributesQueryKeys,
  useAttribute,
  useDeleteAttribute,
  useUpdateAttribute
} from "./chunk-7O5NDICV.js";
import "./chunk-46M463BR.js";
import "./chunk-YFRC6GEN.js";
import "./chunk-N6SI4IIT.js";
import "./chunk-JPNPTILP.js";
import "./chunk-DAXXLQKB.js";
import "./chunk-OHOWEMHO.js";
import "./chunk-GKGLVDDV.js";
import "./chunk-WJJXUXSO.js";
import "./chunk-LAPEZATU.js";
import "./chunk-DBNPVU3L.js";
import "./chunk-U7WZYWGA.js";
import "./chunk-ZN7LWXVC.js";
import "./chunk-VYMODH6P.js";
import "./chunk-LEG67A2R.js";
import "./chunk-PRBE5CM3.js";
import "./chunk-5TRY5IMZ.js";
import "./chunk-TAKEQAZZ.js";
import "./chunk-B6FG7A6W.js";
import "./chunk-EPHVGD5Z.js";
import "./chunk-ESPLVOFC.js";
import "./chunk-FXWKGNRV.js";
import "./chunk-VDKOJ5WD.js";
import "./chunk-DL2ZIXBO.js";
import "./chunk-Z2QQZOG3.js";
import "./chunk-5CK6XEFV.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/attributes/attribute-detail/breadcrumb.tsx
import { jsx } from "react/jsx-runtime";
var AttributeDetailBreadcrumb = (props) => {
  const { id } = props.params || {};
  const { attribute } = useAttribute(id, void 0, {
    initialData: props.data,
    enabled: Boolean(id)
  });
  if (!attribute) {
    return null;
  }
  return /* @__PURE__ */ jsx("span", { children: attribute.name });
};

// src/pages/attributes/attribute-detail/loader.ts
var attributeDetailQuery = (id) => ({
  queryKey: attributesQueryKeys.detail(id),
  queryFn: async () => sdk.admin.attributes.$id.query({ $id: id, fields: ATTRIBUTE_DETAIL_FIELDS })
});
var attributeDetailLoader = async ({ params }) => {
  const id = params.id;
  const query = attributeDetailQuery(id);
  return queryClient.ensureQueryData(query);
};

// src/pages/attributes/attribute-detail/attribute-detail.tsx
import { Children } from "react";
import { useLoaderData, useParams } from "react-router-dom";

// src/pages/attributes/attribute-detail/components/attribute-general-section/attribute-general-section.tsx
import { PencilSquare, Trash } from "@medusajs/icons";
import { Badge, Container, Heading, toast, usePrompt } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
var AttributeGeneralSection = ({
  attribute
}) => {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const prompt = usePrompt();
  const { mutateAsync } = useDeleteAttribute(attribute.id);
  const handleDelete = async () => {
    const confirmed = await prompt({
      title: t("general.areYouSure"),
      description: t("attributes.delete.confirmation", {
        name: attribute.name
      }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!confirmed) {
      return;
    }
    await mutateAsync(void 0, {
      onSuccess: () => {
        toast.success(
          t("attributes.delete.successToast", { name: attribute.name })
        );
        navigate("/settings/attributes", { replace: true });
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  };
  const typeLabel = t(`attributes.type.${attribute.ui_component}`, attribute.ui_component) ?? attribute.ui_component;
  return /* @__PURE__ */ jsxs(
    Container,
    {
      className: "divide-y p-0",
      "data-testid": "attribute-general-section-container",
      children: [
        /* @__PURE__ */ jsxs(
          "div",
          {
            className: "flex items-center justify-between px-6 py-4",
            "data-testid": "attribute-general-section-header",
            children: [
              /* @__PURE__ */ jsx2(Heading, { "data-testid": "attribute-general-section-heading", children: attribute.name }),
              /* @__PURE__ */ jsx2(
                ActionMenu,
                {
                  groups: [
                    {
                      actions: [
                        {
                          icon: /* @__PURE__ */ jsx2(PencilSquare, {}),
                          label: t("actions.edit"),
                          to: "edit"
                        }
                      ]
                    },
                    {
                      actions: [
                        {
                          icon: /* @__PURE__ */ jsx2(Trash, {}),
                          label: t("actions.delete"),
                          onClick: handleDelete
                        }
                      ]
                    }
                  ],
                  "data-testid": "attribute-general-section-action-menu"
                }
              )
            ]
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.handle"),
            value: attribute.handle ? `/${attribute.handle}` : "-",
            "data-testid": "attribute-general-section-handle"
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.description"),
            value: attribute.description || "-",
            "data-testid": "attribute-general-section-description"
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.type"),
            value: typeLabel,
            "data-testid": "attribute-general-section-type"
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.filterable"),
            value: attribute.is_filterable ? t("fields.true") : t("fields.false"),
            "data-testid": "attribute-general-section-filterable"
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.required"),
            value: attribute.is_required ? t("fields.true") : t("fields.false"),
            "data-testid": "attribute-general-section-required"
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.global"),
            value: attribute.product_categories && attribute.product_categories.length > 0 ? t("fields.false") : t("fields.true"),
            "data-testid": "attribute-general-section-global"
          }
        ),
        /* @__PURE__ */ jsx2(
          SectionRow,
          {
            title: t("attributes.fields.category"),
            value: attribute.product_categories && attribute.product_categories.length > 0 ? /* @__PURE__ */ jsx2(
              "div",
              {
                className: "flex flex-wrap gap-1",
                "data-testid": "attribute-general-section-categories",
                children: attribute.product_categories.map((category) => /* @__PURE__ */ jsx2(Badge, { size: "2xsmall", children: category.name }, category.id))
              }
            ) : "-",
            "data-testid": "attribute-general-section-categories-row"
          }
        )
      ]
    }
  );
};

// src/pages/attributes/attribute-detail/components/attribute-possible-values-section/attribute-possible-values-section.tsx
import { PencilSquare as PencilSquare2, Trash as Trash2 } from "@medusajs/icons";
import {
  Button,
  Container as Container2,
  Heading as Heading2,
  Input,
  Prompt,
  toast as toast2,
  usePrompt as usePrompt2
} from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo, useState } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";
import { Link } from "react-router-dom";
import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
var PossibleValueActions = ({
  value,
  attribute
}) => {
  const { t } = useTranslation2();
  const prompt = usePrompt2();
  const { mutateAsync } = useUpdateAttribute(attribute.id);
  const [inUseOpen, setInUseOpen] = useState(false);
  const handleDelete = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("attributes.deletePossibleValue.confirmation", {
        value: value.value
      }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) return;
    const remainingValues = (attribute.possible_values ?? []).filter((pv) => pv.id !== value.id).map((pv) => ({ id: pv.id, value: pv.value, rank: pv.rank }));
    try {
      await mutateAsync({ possible_values: remainingValues });
      toast2.success(
        t("attributes.deletePossibleValue.successToast", {
          value: value.value
        })
      );
    } catch (err) {
      const isInUse = err.message?.includes("can't be deleted");
      if (isInUse) {
        setInUseOpen(true);
      } else {
        toast2.error(err.message);
      }
    }
  };
  return /* @__PURE__ */ jsxs2(Fragment, { children: [
    /* @__PURE__ */ jsx3(
      ActionMenu,
      {
        groups: [
          {
            actions: [
              {
                icon: /* @__PURE__ */ jsx3(PencilSquare2, {}),
                label: t("actions.edit"),
                to: `edit-possible-value?possible_value_id=${value.id}`
              }
            ]
          },
          {
            actions: [
              {
                icon: /* @__PURE__ */ jsx3(Trash2, {}),
                label: t("actions.delete"),
                onClick: handleDelete
              }
            ]
          }
        ]
      }
    ),
    /* @__PURE__ */ jsx3(Prompt, { open: inUseOpen, variant: "confirmation", children: /* @__PURE__ */ jsxs2(Prompt.Content, { children: [
      /* @__PURE__ */ jsxs2(Prompt.Header, { children: [
        /* @__PURE__ */ jsx3(Prompt.Title, { children: t("attributes.deletePossibleValue.title") }),
        /* @__PURE__ */ jsx3(Prompt.Description, { children: t("attributes.deletePossibleValue.inUseMessage", {
          value: value.value
        }) })
      ] }),
      /* @__PURE__ */ jsx3(Prompt.Footer, { children: /* @__PURE__ */ jsx3(Prompt.Action, { onClick: () => setInUseOpen(false), children: t("attributes.delete.gotIt") }) })
    ] }) })
  ] });
};
var columnHelper = createColumnHelper();
var PAGE_SIZE = 10;
var AttributePossibleValuesSection = ({
  attribute
}) => {
  const { t } = useTranslation2();
  const [search, setSearch] = useState("");
  const allValues = attribute.possible_values ?? [];
  const filtered = useMemo(() => {
    if (!search) return allValues;
    const q = search.toLowerCase();
    return allValues.filter((v) => v.value?.toLowerCase().includes(q));
  }, [search, allValues]);
  const columns = useMemo(
    () => [
      columnHelper.accessor("value", {
        header: () => t("fields.name"),
        cell: ({ getValue }) => /* @__PURE__ */ jsx3(TextCell, { text: getValue() })
      }),
      columnHelper.accessor("rank", {
        header: () => t("attributes.fields.rank"),
        cell: ({ getValue }) => /* @__PURE__ */ jsx3(TextCell, { text: String(getValue()) })
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => /* @__PURE__ */ jsx3(PossibleValueActions, { value: row.original, attribute })
      })
    ],
    [t, attribute]
  );
  const { table } = useDataTable({
    data: filtered,
    count: filtered.length,
    columns,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  return /* @__PURE__ */ jsxs2(Container2, { className: "divide-y p-0", children: [
    /* @__PURE__ */ jsxs2("div", { className: "flex items-center justify-between px-6 py-4", children: [
      /* @__PURE__ */ jsx3(Heading2, { level: "h2", children: t("attributes.fields.possibleValues") }),
      /* @__PURE__ */ jsxs2("div", { className: "flex items-center gap-x-2", children: [
        /* @__PURE__ */ jsx3(
          Input,
          {
            size: "small",
            type: "search",
            placeholder: t("general.search"),
            value: search,
            onChange: (e) => setSearch(e.target.value),
            className: "w-[200px]"
          }
        ),
        /* @__PURE__ */ jsx3(Button, { variant: "secondary", size: "small", asChild: true, children: /* @__PURE__ */ jsx3(Link, { to: "edit-ranking", children: t("attributes.possibleValues.editRanking", "Edit ranking") }) }),
        /* @__PURE__ */ jsx3(Button, { variant: "secondary", size: "small", asChild: true, children: /* @__PURE__ */ jsx3(Link, { to: "create-possible-value", children: t("actions.create") }) })
      ] })
    ] }),
    allValues.length > 0 ? /* @__PURE__ */ jsx3(
      _DataTable,
      {
        table,
        columns,
        pageSize: PAGE_SIZE,
        count: filtered.length,
        pagination: true
      }
    ) : /* @__PURE__ */ jsx3(
      NoRecords,
      {
        className: "border-t",
        title: t("attributes.possibleValues.noRecordsTitle", "No created values"),
        message: t("attributes.possibleValues.noRecordsMessage", "Create attribute to organize your products"),
        action: {
          to: "create-possible-value",
          label: t("actions.create")
        }
      }
    )
  ] });
};

// src/pages/attributes/attribute-detail/attribute-detail.tsx
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const { id } = useParams();
  const initialData = useLoaderData();
  const { attribute, isPending, isError, error } = useAttribute(
    id,
    { fields: ATTRIBUTE_DETAIL_FIELDS },
    {
      initialData
    }
  );
  if (isPending || !attribute) {
    return /* @__PURE__ */ jsx4(SingleColumnPageSkeleton, { sections: 2 });
  }
  if (isError) {
    throw error;
  }
  return Children.count(children) > 0 ? /* @__PURE__ */ jsx4(SingleColumnPage, { hasOutlet: true, data: attribute, children }) : /* @__PURE__ */ jsxs3(SingleColumnPage, { hasOutlet: true, data: attribute, children: [
    /* @__PURE__ */ jsx4(AttributeGeneralSection, { attribute }),
    /* @__PURE__ */ jsx4(AttributePossibleValuesSection, { attribute })
  ] });
};
var AttributeDetailPage = Object.assign(Root, {
  GeneralSection: AttributeGeneralSection,
  PossibleValuesSection: AttributePossibleValuesSection
});
export {
  AttributeDetailBreadcrumb as Breadcrumb,
  AttributeDetailPage as Component,
  attributeDetailLoader as loader
};
