/*
 * 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 { Popover } from "@base-ui/react/popover";
import { CaretDown, CaretUp, WarningSign } from "@blueprintjs/icons";
import classnames from "classnames";
import React from "react";
import { ActionButton } from "../../base-components/action-button/ActionButton.js";
import { SkeletonBar } from "../../base-components/skeleton/SkeletonBar.js";
import { backgroundFromColors } from "../utils/cbacPickerUtils.js";
import { formatCbacError } from "../utils/errorMessages.js";
import { BaseCbacBanner } from "./BaseCbacBanner.js";
import styles from "./BaseCbacBannerPopover.module.css";
export function BaseCbacBannerPopover({
  classificationString,
  textColor,
  backgroundColors,
  appliedMarkings,
  onEditClick,
  description,
  isLoading,
  error,
  onRetry,
  onDismiss,
  warnings,
  className
}) {
  const [open, setOpen] = React.useState(false);
  const handleEditClick = React.useCallback(() => {
    setOpen(false);
    onEditClick();
  }, [onEditClick]);
  const pillStyle = React.useMemo(() => ({
    color: textColor,
    background: backgroundFromColors(backgroundColors)
  }), [textColor, backgroundColors]);
  const showSkeleton = isLoading === true && appliedMarkings.length === 0;
  return /*#__PURE__*/React.createElement(Popover.Root, {
    open: open,
    onOpenChange: setOpen
  }, /*#__PURE__*/React.createElement(Popover.Trigger, {
    nativeButton: false,
    render: /*#__PURE__*/React.createElement("div", {
      className: styles.bannerTriggerWrapper
    }, /*#__PURE__*/React.createElement(BaseCbacBanner, {
      classificationString: classificationString,
      textColor: textColor,
      backgroundColors: backgroundColors,
      onDismiss: onDismiss,
      className: className
    }), /*#__PURE__*/React.createElement("span", {
      className: classnames(styles.caretIcon, open && styles.caretIconVisible)
    }, open ? /*#__PURE__*/React.createElement(CaretUp, {
      size: 12
    }) : /*#__PURE__*/React.createElement(CaretDown, {
      size: 12
    })))
  }), /*#__PURE__*/React.createElement(Popover.Portal, null, /*#__PURE__*/React.createElement(Popover.Positioner, {
    side: "bottom",
    align: "center"
  }, /*#__PURE__*/React.createElement(Popover.Popup, {
    className: styles.popover
  }, /*#__PURE__*/React.createElement(PopoverContent, {
    showSkeleton: showSkeleton,
    error: error,
    onRetry: onRetry,
    pillStyle: pillStyle,
    classificationString: classificationString,
    description: description,
    appliedMarkings: appliedMarkings,
    warnings: warnings,
    handleEditClick: handleEditClick
  })))));
}
const PopoverContent = /*#__PURE__*/React.memo(function ({
  showSkeleton,
  error,
  onRetry,
  pillStyle,
  classificationString,
  description,
  appliedMarkings,
  warnings,
  handleEditClick
}) {
  const hasMarkings = appliedMarkings.length > 0;
  const hasWarnings = warnings !== undefined && warnings.length > 0;
  if (showSkeleton) {
    return /*#__PURE__*/React.createElement(PopoverSkeleton, null);
  }
  if (error !== undefined) {
    const errorMessage = formatCbacError(error);
    return /*#__PURE__*/React.createElement("div", {
      className: styles.errorContainer
    }, /*#__PURE__*/React.createElement("p", {
      className: styles.errorMessage
    }, errorMessage.title), errorMessage.remediation && /*#__PURE__*/React.createElement("p", {
      className: styles.errorRemediation
    }, errorMessage.remediation), onRetry !== undefined && /*#__PURE__*/React.createElement(ActionButton, {
      variant: "secondary",
      onClick: onRetry
    }, "Retry"));
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: styles.pill,
    style: pillStyle,
    title: classificationString
  }, classificationString), description !== undefined && /*#__PURE__*/React.createElement("p", {
    className: styles.description
  }, description), /*#__PURE__*/React.createElement("p", {
    className: styles.sectionLabel
  }, "Applied markings"), hasMarkings ? appliedMarkings.map(group => /*#__PURE__*/React.createElement("div", {
    key: group.categoryName,
    className: styles.markingGroup
  }, /*#__PURE__*/React.createElement("p", {
    className: styles.markingCategoryName
  }, group.categoryName), group.markingNames.map((name, i) => /*#__PURE__*/React.createElement("p", {
    key: `${name}-${i}`,
    className: styles.markingName
  }, name)))) : /*#__PURE__*/React.createElement("p", {
    className: styles.noMarkings
  }, "No markings applied"), hasWarnings && warnings !== undefined && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("hr", {
    className: styles.divider
  }), /*#__PURE__*/React.createElement("div", {
    className: styles.warningsSection
  }, /*#__PURE__*/React.createElement("p", {
    className: styles.warningsHeader
  }, "Warnings and Notices"), warnings.map((warning, i) => /*#__PURE__*/React.createElement("div", {
    key: `warning-${i}`,
    className: styles.warningCallout
  }, /*#__PURE__*/React.createElement(WarningSign, {
    className: styles.warningIcon,
    size: 14
  }), /*#__PURE__*/React.createElement("p", {
    className: styles.warningText
  }, warning))))), /*#__PURE__*/React.createElement("hr", {
    className: styles.divider
  }), /*#__PURE__*/React.createElement(ActionButton, {
    variant: "primary",
    className: styles.editButton,
    onClick: handleEditClick
  }, hasMarkings ? "Edit classification" : "Set classification"));
});
function PopoverSkeleton() {
  return /*#__PURE__*/React.createElement("div", {
    className: styles.skeletonContainer
  }, /*#__PURE__*/React.createElement(SkeletonBar, {
    className: styles.skeletonPill
  }), /*#__PURE__*/React.createElement(SkeletonBar, {
    className: styles.skeletonLine
  }), /*#__PURE__*/React.createElement(SkeletonBar, {
    className: styles.skeletonLineNarrow
  }));
}
//# sourceMappingURL=BaseCbacBannerPopover.js.map