/*
 * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Tooltip } from "@base-ui/react/tooltip";
import { InfoSign } from "@blueprintjs/icons";
import React from "react";
import { MarkingButton } from "./MarkingButton.js";
import { OverflowButton } from "./OverflowButton.js";
import { isImplied } from "./selectionStateHelpers.js";
import styles from "./CategoryMarkingGroup.module.css.js";
const GRID_COLUMNS = 4;
const VISIBLE_ROWS = 3;
const DEFAULT_MARKING_STATE = "NONE";
export const CategoryMarkingGroup = /*#__PURE__*/React.memo(function ({
  categoryName,
  categoryDescription,
  markings,
  markingStates,
  readOnly,
  onMarkingToggle
}) {
  const headingId = React.useId();
  const resolvedMarkings = React.useMemo(() => markings.map(marking => ({
    id: marking.id,
    label: marking.name,
    description: marking.description,
    selectionState: markingStates.get(marking.id) ?? DEFAULT_MARKING_STATE,
    disabled: readOnly
  })), [markings, markingStates, readOnly]);
  const maxVisible = GRID_COLUMNS * VISIBLE_ROWS;
  const hasOverflow = resolvedMarkings.length > maxVisible;
  const visibleMarkings = hasOverflow ? resolvedMarkings.slice(0, maxVisible - 1) : resolvedMarkings;
  const overflowMarkings = hasOverflow ? resolvedMarkings.slice(maxVisible - 1) : [];
  const gridItemCount = hasOverflow ? visibleMarkings.length + 1 : visibleMarkings.length;
  const hasActiveOverflow = overflowMarkings.some(m => m.selectionState === "SELECTED" || isImplied(m.selectionState));
  return /*#__PURE__*/React.createElement("div", {
    className: styles.categoryGroup,
    role: "group",
    "aria-labelledby": headingId
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.categoryHeader
  }, /*#__PURE__*/React.createElement("h3", {
    id: headingId,
    className: styles.categoryName
  }, categoryName), categoryDescription != null && categoryDescription.length > 0 && /*#__PURE__*/React.createElement(Tooltip.Root, null, /*#__PURE__*/React.createElement(Tooltip.Trigger, {
    render: /*#__PURE__*/React.createElement("span", {
      className: styles.infoIcon,
      "aria-label": "Category description"
    }, /*#__PURE__*/React.createElement(InfoSign, {
      size: 12
    }))
  }), /*#__PURE__*/React.createElement(Tooltip.Portal, null, /*#__PURE__*/React.createElement(Tooltip.Positioner, {
    side: "top",
    sideOffset: 4
  }, /*#__PURE__*/React.createElement(Tooltip.Popup, {
    className: styles.infoTooltip
  }, categoryDescription))))), /*#__PURE__*/React.createElement("div", {
    className: styles.markingGrid
  }, visibleMarkings.map(marking => /*#__PURE__*/React.createElement(MarkingButton, {
    key: marking.id,
    id: marking.id,
    label: marking.label,
    description: marking.description,
    selectionState: marking.selectionState,
    disabled: marking.disabled,
    onToggle: onMarkingToggle
  })), hasOverflow && /*#__PURE__*/React.createElement(OverflowButton, {
    overflowMarkings: overflowMarkings,
    hasActiveOverflow: hasActiveOverflow,
    onMarkingToggle: onMarkingToggle
  }), Array.from({
    length: (GRID_COLUMNS - gridItemCount % GRID_COLUMNS) % GRID_COLUMNS
  }, (_, i) => /*#__PURE__*/React.createElement("div", {
    key: `empty-${i}`,
    className: styles.emptyCell
  }))));
});
//# sourceMappingURL=CategoryMarkingGroup.js.map