'use strict';

var chunkZMAWPOG5_cjs = require('./chunk-ZMAWPOG5.cjs');
var chunkC6IPNPMZ_cjs = require('./chunk-C6IPNPMZ.cjs');
var chunkHNBSGCSZ_cjs = require('./chunk-HNBSGCSZ.cjs');
var chunk7LXS66DW_cjs = require('./chunk-7LXS66DW.cjs');
var chunkQP2LHBCZ_cjs = require('./chunk-QP2LHBCZ.cjs');
var chunkOWBDRWL6_cjs = require('./chunk-OWBDRWL6.cjs');
var platformApis = require('@osdk/react/platform-apis');
var React = require('react');
var popover = require('@base-ui/react/popover');
var icons = require('@blueprintjs/icons');
var classnames = require('classnames');
var tooltip = require('@base-ui/react/tooltip');
var button = require('@base-ui/react/button');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

// src/cbac-picker/utils/errorMessages.ts
function isApiErrorLike(error) {
  return "statusCode" in error && typeof error.statusCode === "number";
}
function formatByStatusCode(error) {
  const statusCode = error.statusCode;
  if (statusCode === 401) {
    return {
      title: "Session expired",
      remediation: "Sign in again to continue."
    };
  }
  if (statusCode === 403) {
    return {
      title: "Permission denied",
      remediation: "Contact your administrator for permission."
    };
  }
  if (statusCode === 404) {
    return {
      title: "Not found",
      remediation: "The requested classification data could not be found."
    };
  }
  if (statusCode !== void 0 && statusCode >= 500) {
    return {
      title: "Server error",
      remediation: "Something went wrong. Try again later."
    };
  }
  if (typeof error.errorDescription === "string" && error.errorDescription.length > 0) {
    return {
      title: error.errorDescription,
      remediation: ""
    };
  }
  return {
    title: error.message || "Something went wrong",
    remediation: ""
  };
}
function formatCbacError(error) {
  if (error instanceof AggregateError && error.errors.length > 0) {
    const first = error.errors[0];
    if (first instanceof Error) {
      return formatCbacError(first);
    }
  }
  if (isApiErrorLike(error)) {
    return formatByStatusCode(error);
  }
  if (error.message.toLowerCase().includes("network") || error.message.toLowerCase().includes("fetch")) {
    return {
      title: "Network error",
      remediation: "Check your connection and try again."
    };
  }
  return {
    title: error.message || "Something went wrong",
    remediation: ""
  };
}

// src/cbac-picker/base/BaseCbacBannerPopover.module.css
var BaseCbacBannerPopover_default = {};

// src/cbac-picker/base/BaseCbacBannerPopover.tsx
function BaseCbacBannerPopover({
  classificationString,
  textColor,
  backgroundColors,
  appliedMarkings,
  onEditClick,
  description,
  isLoading,
  error,
  onRetry,
  onDismiss,
  warnings,
  className
}) {
  const [open, setOpen] = React__default.default.useState(false);
  const handleEditClick = React__default.default.useCallback(() => {
    setOpen(false);
    onEditClick();
  }, [onEditClick]);
  const pillStyle = React__default.default.useMemo(() => ({
    color: textColor,
    background: chunkZMAWPOG5_cjs.backgroundFromColors(backgroundColors)
  }), [textColor, backgroundColors]);
  const showSkeleton = isLoading === true && appliedMarkings.length === 0;
  return /* @__PURE__ */ React__default.default.createElement(popover.Popover.Root, {
    open,
    onOpenChange: setOpen
  }, /* @__PURE__ */ React__default.default.createElement(popover.Popover.Trigger, {
    nativeButton: false,
    render: /* @__PURE__ */ React__default.default.createElement("div", {
      className: BaseCbacBannerPopover_default.bannerTriggerWrapper
    }, /* @__PURE__ */ React__default.default.createElement(chunkZMAWPOG5_cjs.BaseCbacBanner, {
      classificationString,
      textColor,
      backgroundColors,
      onDismiss,
      className
    }), /* @__PURE__ */ React__default.default.createElement("span", {
      className: classnames__default.default(BaseCbacBannerPopover_default.caretIcon, open && BaseCbacBannerPopover_default.caretIconVisible)
    }, open ? /* @__PURE__ */ React__default.default.createElement(icons.CaretUp, {
      size: 12
    }) : /* @__PURE__ */ React__default.default.createElement(icons.CaretDown, {
      size: 12
    })))
  }), /* @__PURE__ */ React__default.default.createElement(popover.Popover.Portal, null, /* @__PURE__ */ React__default.default.createElement(popover.Popover.Positioner, {
    side: "bottom",
    align: "center"
  }, /* @__PURE__ */ React__default.default.createElement(popover.Popover.Popup, {
    className: BaseCbacBannerPopover_default.popover
  }, /* @__PURE__ */ React__default.default.createElement(PopoverContent, {
    showSkeleton,
    error,
    onRetry,
    pillStyle,
    classificationString,
    description,
    appliedMarkings,
    warnings,
    handleEditClick
  })))));
}
var PopoverContent = /* @__PURE__ */ React__default.default.memo(function PopoverContent2({
  showSkeleton,
  error,
  onRetry,
  pillStyle,
  classificationString,
  description,
  appliedMarkings,
  warnings,
  handleEditClick
}) {
  const hasMarkings = appliedMarkings.length > 0;
  const hasWarnings = warnings !== void 0 && warnings.length > 0;
  if (showSkeleton) {
    return /* @__PURE__ */ React__default.default.createElement(PopoverSkeleton, null);
  }
  if (error !== void 0) {
    const errorMessage = formatCbacError(error);
    return /* @__PURE__ */ React__default.default.createElement("div", {
      className: BaseCbacBannerPopover_default.errorContainer
    }, /* @__PURE__ */ React__default.default.createElement("p", {
      className: BaseCbacBannerPopover_default.errorMessage
    }, errorMessage.title), errorMessage.remediation && /* @__PURE__ */ React__default.default.createElement("p", {
      className: BaseCbacBannerPopover_default.errorRemediation
    }, errorMessage.remediation), onRetry !== void 0 && /* @__PURE__ */ React__default.default.createElement(chunkQP2LHBCZ_cjs.ActionButton, {
      variant: "secondary",
      onClick: onRetry
    }, "Retry"));
  }
  return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseCbacBannerPopover_default.pill,
    style: pillStyle,
    title: classificationString
  }, classificationString), description !== void 0 && /* @__PURE__ */ React__default.default.createElement("p", {
    className: BaseCbacBannerPopover_default.description
  }, description), /* @__PURE__ */ React__default.default.createElement("p", {
    className: BaseCbacBannerPopover_default.sectionLabel
  }, "Applied markings"), hasMarkings ? appliedMarkings.map((group) => /* @__PURE__ */ React__default.default.createElement("div", {
    key: group.categoryName,
    className: BaseCbacBannerPopover_default.markingGroup
  }, /* @__PURE__ */ React__default.default.createElement("p", {
    className: BaseCbacBannerPopover_default.markingCategoryName
  }, group.categoryName), group.markingNames.map((name, i) => /* @__PURE__ */ React__default.default.createElement("p", {
    key: `${name}-${i}`,
    className: BaseCbacBannerPopover_default.markingName
  }, name)))) : /* @__PURE__ */ React__default.default.createElement("p", {
    className: BaseCbacBannerPopover_default.noMarkings
  }, "No markings applied"), hasWarnings && warnings !== void 0 && /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("hr", {
    className: BaseCbacBannerPopover_default.divider
  }), /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseCbacBannerPopover_default.warningsSection
  }, /* @__PURE__ */ React__default.default.createElement("p", {
    className: BaseCbacBannerPopover_default.warningsHeader
  }, "Warnings and Notices"), warnings.map((warning, i) => /* @__PURE__ */ React__default.default.createElement("div", {
    key: `warning-${i}`,
    className: BaseCbacBannerPopover_default.warningCallout
  }, /* @__PURE__ */ React__default.default.createElement(icons.WarningSign, {
    className: BaseCbacBannerPopover_default.warningIcon,
    size: 14
  }), /* @__PURE__ */ React__default.default.createElement("p", {
    className: BaseCbacBannerPopover_default.warningText
  }, warning))))), /* @__PURE__ */ React__default.default.createElement("hr", {
    className: BaseCbacBannerPopover_default.divider
  }), /* @__PURE__ */ React__default.default.createElement(chunkQP2LHBCZ_cjs.ActionButton, {
    variant: "primary",
    className: BaseCbacBannerPopover_default.editButton,
    onClick: handleEditClick
  }, hasMarkings ? "Edit classification" : "Set classification"));
});
function PopoverSkeleton() {
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseCbacBannerPopover_default.skeletonContainer
  }, /* @__PURE__ */ React__default.default.createElement(chunk7LXS66DW_cjs.SkeletonBar, {
    className: BaseCbacBannerPopover_default.skeletonPill
  }), /* @__PURE__ */ React__default.default.createElement(chunk7LXS66DW_cjs.SkeletonBar, {
    className: BaseCbacBannerPopover_default.skeletonLine
  }), /* @__PURE__ */ React__default.default.createElement(chunk7LXS66DW_cjs.SkeletonBar, {
    className: BaseCbacBannerPopover_default.skeletonLineNarrow
  }));
}

// src/cbac-picker/base/selectionStateHelpers.ts
function isDisallowed(state) {
  return state === "DISALLOWED" || state === "IMPLIED_DISALLOWED";
}
function isImplied(state) {
  return state === "IMPLIED" || state === "IMPLIED_DISALLOWED";
}
function getDisplayLabel(label, state) {
  return isImplied(state) ? `(${label})` : label;
}
function getTooltipText(state) {
  if (state === "DISALLOWED") {
    return "This marking is not allowed with the current selection";
  }
  if (state === "IMPLIED_DISALLOWED") {
    return "This marking is implied but not allowed";
  }
  return void 0;
}

// src/cbac-picker/base/MarkingButton.module.css
var MarkingButton_default = {};

// src/cbac-picker/base/MarkingButton.tsx
var selectionStateClassMap = {
  SELECTED: MarkingButton_default.selected,
  IMPLIED: MarkingButton_default.implied,
  DISALLOWED: MarkingButton_default.disallowed,
  IMPLIED_DISALLOWED: MarkingButton_default.impliedDisallowed,
  NONE: void 0
};
function getSelectionHint(state) {
  switch (state) {
    case "SELECTED":
      return "Click to deselect";
    case "IMPLIED":
      return "Implied by another marking";
    case "DISALLOWED":
    case "IMPLIED_DISALLOWED":
      return "Not available with current selection. Deselect your current choice first.";
    case "NONE":
      return void 0;
  }
}
var MarkingButton = /* @__PURE__ */ React__default.default.memo(function MarkingButton2({
  id,
  label,
  description,
  selectionState,
  onToggle,
  disabled
}) {
  const handleToggle = React__default.default.useCallback(() => {
    onToggle(id);
  }, [onToggle, id]);
  const hasDescription = description !== void 0 && description.length > 0;
  const isButtonDisabled = disabled ?? isDisallowed(selectionState);
  const showTooltip = hasDescription || isDisallowed(selectionState);
  const hint = getSelectionHint(selectionState);
  const button$1 = /* @__PURE__ */ React__default.default.createElement(button.Button, {
    className: classnames__default.default(MarkingButton_default.markingButton, selectionStateClassMap[selectionState]),
    onClick: isButtonDisabled ? void 0 : handleToggle,
    disabled: showTooltip ? void 0 : isButtonDisabled,
    "aria-disabled": showTooltip ? isButtonDisabled : void 0,
    "aria-pressed": selectionState === "SELECTED" || selectionState === "IMPLIED",
    title: showTooltip ? void 0 : label
  }, getDisplayLabel(label, selectionState));
  if (!showTooltip) {
    return button$1;
  }
  return /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Root, null, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Trigger, {
    render: button$1
  }), /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Portal, null, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Positioner, {
    sideOffset: 8
  }, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Popup, {
    className: MarkingButton_default.tooltip
  }, /* @__PURE__ */ React__default.default.createElement("p", {
    className: MarkingButton_default.tooltipTitle
  }, label), hasDescription && /* @__PURE__ */ React__default.default.createElement("p", {
    className: MarkingButton_default.tooltipDescription
  }, description), hint != null && /* @__PURE__ */ React__default.default.createElement("p", {
    className: MarkingButton_default.tooltipHint
  }, hint)))));
});

// src/cbac-picker/base/OverflowItem.module.css
var OverflowItem_default = {};

// src/cbac-picker/base/OverflowItem.tsx
var OverflowItem = /* @__PURE__ */ React__default.default.memo(function OverflowItem2({
  id,
  label,
  description,
  selectionState,
  disabled,
  onToggle
}) {
  const handleClick = React__default.default.useCallback(() => {
    onToggle(id);
  }, [onToggle, id]);
  const disallowed = isDisallowed(selectionState);
  const isSelected = selectionState === "SELECTED";
  const implied = isImplied(selectionState);
  const tooltipText = getTooltipText(selectionState);
  const hasDescription = description !== void 0 && description.length > 0;
  const showTooltip = hasDescription || tooltipText != null;
  const isItemDisabled = disabled ?? disallowed;
  const button$1 = /* @__PURE__ */ React__default.default.createElement(button.Button, {
    className: classnames__default.default(OverflowItem_default.overflowItem, (isSelected || implied) && OverflowItem_default.overflowItemSelected, disallowed && OverflowItem_default.overflowItemDisabled),
    onClick: isItemDisabled ? void 0 : handleClick,
    disabled: showTooltip ? void 0 : isItemDisabled,
    "aria-disabled": showTooltip ? isItemDisabled : void 0,
    "aria-pressed": isSelected || implied
  }, getDisplayLabel(label, selectionState));
  if (!showTooltip) {
    return button$1;
  }
  return /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Root, null, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Trigger, {
    render: button$1
  }), /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Portal, null, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Positioner, {
    side: "right",
    sideOffset: 4
  }, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Popup, {
    className: OverflowItem_default.tooltip
  }, hasDescription && /* @__PURE__ */ React__default.default.createElement("p", {
    className: OverflowItem_default.tooltipDescription
  }, description), tooltipText != null && /* @__PURE__ */ React__default.default.createElement("p", {
    className: OverflowItem_default.tooltipHint
  }, tooltipText)))));
});

// src/cbac-picker/base/OverflowButton.module.css
var OverflowButton_default = {};

// src/cbac-picker/base/OverflowButton.tsx
function OverflowButton({
  overflowMarkings,
  hasActiveOverflow,
  onMarkingToggle
}) {
  const [open, setOpen] = React__default.default.useState(false);
  return /* @__PURE__ */ React__default.default.createElement(popover.Popover.Root, {
    open,
    onOpenChange: setOpen
  }, /* @__PURE__ */ React__default.default.createElement(popover.Popover.Trigger, {
    render: /* @__PURE__ */ React__default.default.createElement(button.Button, {
      className: classnames__default.default(OverflowButton_default.moreButton, hasActiveOverflow && OverflowButton_default.moreButtonActive)
    }, "+", overflowMarkings.length, " more")
  }), /* @__PURE__ */ React__default.default.createElement(popover.Popover.Portal, null, /* @__PURE__ */ React__default.default.createElement(popover.Popover.Positioner, {
    side: "bottom",
    align: "start"
  }, /* @__PURE__ */ React__default.default.createElement(popover.Popover.Popup, {
    className: OverflowButton_default.overflowList
  }, overflowMarkings.map((marking) => /* @__PURE__ */ React__default.default.createElement(OverflowItem, {
    key: marking.id,
    id: marking.id,
    label: marking.label,
    description: marking.description,
    selectionState: marking.selectionState,
    disabled: marking.disabled,
    onToggle: onMarkingToggle
  }))))));
}

// src/cbac-picker/base/CategoryMarkingGroup.module.css
var CategoryMarkingGroup_default = {};

// src/cbac-picker/base/CategoryMarkingGroup.tsx
var GRID_COLUMNS = 4;
var VISIBLE_ROWS = 3;
var DEFAULT_MARKING_STATE = "NONE";
var CategoryMarkingGroup = /* @__PURE__ */ React__default.default.memo(function CategoryMarkingGroup2({
  categoryName,
  categoryDescription,
  markings,
  markingStates,
  readOnly,
  onMarkingToggle
}) {
  const headingId = React__default.default.useId();
  const resolvedMarkings = React__default.default.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 emptyCellCount = (GRID_COLUMNS - gridItemCount % GRID_COLUMNS) % GRID_COLUMNS;
  const hasActiveOverflow = overflowMarkings.some((m) => m.selectionState === "SELECTED" || isImplied(m.selectionState));
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: CategoryMarkingGroup_default.categoryGroup,
    role: "group",
    "aria-labelledby": headingId
  }, /* @__PURE__ */ React__default.default.createElement("div", {
    className: CategoryMarkingGroup_default.categoryHeader
  }, /* @__PURE__ */ React__default.default.createElement("h3", {
    id: headingId,
    className: CategoryMarkingGroup_default.categoryName
  }, categoryName), categoryDescription != null && categoryDescription.length > 0 && /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Root, null, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Trigger, {
    render: /* @__PURE__ */ React__default.default.createElement("span", {
      className: CategoryMarkingGroup_default.infoIcon,
      "aria-label": "Category description"
    }, /* @__PURE__ */ React__default.default.createElement(icons.InfoSign, {
      size: 12
    }))
  }), /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Portal, null, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Positioner, {
    side: "top",
    sideOffset: 4
  }, /* @__PURE__ */ React__default.default.createElement(tooltip.Tooltip.Popup, {
    className: CategoryMarkingGroup_default.infoTooltip
  }, categoryDescription))))), /* @__PURE__ */ React__default.default.createElement("div", {
    className: CategoryMarkingGroup_default.markingGrid
  }, visibleMarkings.map((marking) => /* @__PURE__ */ React__default.default.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__default.default.createElement(OverflowButton, {
    overflowMarkings,
    hasActiveOverflow,
    onMarkingToggle
  }), Array.from({
    length: emptyCellCount
  }, (_, i) => /* @__PURE__ */ React__default.default.createElement("div", {
    key: `empty-${i}`,
    className: CategoryMarkingGroup_default.emptyCell
  }))));
});

// src/cbac-picker/base/InfoBanner.module.css
var InfoBanner_default = {};

// src/cbac-picker/base/InfoBanner.tsx
function InfoBanner({
  message,
  className
}) {
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: classnames__default.default(InfoBanner_default.infoBanner, className)
  }, /* @__PURE__ */ React__default.default.createElement(icons.InfoSign, {
    className: InfoBanner_default.icon,
    size: 14
  }), /* @__PURE__ */ React__default.default.createElement("span", null, message));
}

// src/cbac-picker/base/ValidationWarning.module.css
var ValidationWarning_default = {};

// src/cbac-picker/base/ValidationWarning.tsx
function ValidationWarning({
  requiredMarkingGroups,
  className
}) {
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: classnames__default.default(ValidationWarning_default.validationWarning, className)
  }, /* @__PURE__ */ React__default.default.createElement("div", {
    className: ValidationWarning_default.header
  }, /* @__PURE__ */ React__default.default.createElement(icons.WarningSign, {
    className: ValidationWarning_default.icon,
    size: 14
  }), /* @__PURE__ */ React__default.default.createElement("span", null, "To complete a valid classification, at least one marking from each of the following sets needs to be selected:")), /* @__PURE__ */ React__default.default.createElement("ol", {
    className: ValidationWarning_default.groupList
  }, requiredMarkingGroups.map((group, index) => /* @__PURE__ */ React__default.default.createElement("li", {
    key: group.markingNames.join(","),
    className: ValidationWarning_default.groupItem
  }, /* @__PURE__ */ React__default.default.createElement("span", {
    className: ValidationWarning_default.groupNumber
  }, index + 1, "."), group.markingNames.map((name) => /* @__PURE__ */ React__default.default.createElement("span", {
    key: name,
    className: ValidationWarning_default.chip
  }, name))))));
}

// src/cbac-picker/base/BaseCbacPicker.module.css
var BaseCbacPicker_default = {};

// src/cbac-picker/base/BaseCbacPicker.tsx
function BaseCbacPicker({
  categories,
  markingStates,
  banner,
  onMarkingToggle,
  onDismissBanner,
  showInfoBanner,
  requiredMarkingGroups,
  isValid,
  readOnly,
  isLoading,
  error,
  validationCallouts,
  className
}) {
  const errorMessage = error != null ? formatCbacError(error) : void 0;
  const showInitialLoading = isLoading === true && categories.length === 0;
  const showValidationWarning = isValid === false && requiredMarkingGroups != null && requiredMarkingGroups.length > 0;
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: classnames__default.default(BaseCbacPicker_default.picker, className)
  }, showInfoBanner === true && /* @__PURE__ */ React__default.default.createElement(InfoBanner, {
    message: "Implied markings are (in parentheses)."
  }), banner != null && /* @__PURE__ */ React__default.default.createElement(chunkZMAWPOG5_cjs.BaseCbacBanner, {
    classificationString: banner.classificationString,
    textColor: banner.textColor,
    backgroundColors: banner.backgroundColors,
    onDismiss: onDismissBanner,
    className: BaseCbacPicker_default.innerBanner
  }), errorMessage !== void 0 ? /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseCbacPicker_default.statusMessage,
    role: "alert"
  }, /* @__PURE__ */ React__default.default.createElement("p", null, errorMessage.title), errorMessage.remediation && /* @__PURE__ */ React__default.default.createElement("p", null, errorMessage.remediation)) : showInitialLoading ? /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseCbacPicker_default.statusMessage,
    role: "status",
    "aria-live": "polite"
  }, "Loading...") : /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseCbacPicker_default.categoriesContainer
  }, categories.map((group) => /* @__PURE__ */ React__default.default.createElement(CategoryMarkingGroup, {
    key: group.category.id,
    categoryName: group.category.name,
    categoryDescription: group.category.description,
    markings: group.markings,
    markingStates,
    readOnly,
    onMarkingToggle
  }))), validationCallouts, showValidationWarning && /* @__PURE__ */ React__default.default.createElement(ValidationWarning, {
    requiredMarkingGroups
  }));
}

// src/cbac-picker/base/CbacPickerDialogFooter.module.css
var CbacPickerDialogFooter_default = {};

// src/cbac-picker/base/CbacPickerDialogFooter.tsx
function CbacPickerDialogFooter({
  onCancel,
  onConfirm,
  submitDisabledReason
}) {
  const isSubmitDisabled = submitDisabledReason != null;
  const submitButton = /* @__PURE__ */ React__default.default.createElement(chunkQP2LHBCZ_cjs.ActionButton, {
    variant: "primary",
    onClick: onConfirm,
    disabled: isSubmitDisabled
  }, "Set classification");
  return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement(chunkQP2LHBCZ_cjs.ActionButton, {
    variant: "secondary",
    onClick: onCancel
  }, "Cancel"), isSubmitDisabled ? /* @__PURE__ */ React__default.default.createElement(chunkHNBSGCSZ_cjs.Tooltip.Root, null, /* @__PURE__ */ React__default.default.createElement(chunkHNBSGCSZ_cjs.Tooltip.Trigger, {
    render: /* @__PURE__ */ React__default.default.createElement("span", {
      className: CbacPickerDialogFooter_default.tooltipTriggerWrapper
    })
  }, submitButton), /* @__PURE__ */ React__default.default.createElement(chunkHNBSGCSZ_cjs.Tooltip.Portal, null, /* @__PURE__ */ React__default.default.createElement(chunkHNBSGCSZ_cjs.Tooltip.Positioner, {
    side: "top"
  }, /* @__PURE__ */ React__default.default.createElement(chunkHNBSGCSZ_cjs.Tooltip.Popup, null, submitDisabledReason, /* @__PURE__ */ React__default.default.createElement(chunkHNBSGCSZ_cjs.Tooltip.Arrow, null))))) : submitButton);
}

// src/cbac-picker/base/BaseCbacPickerDialog.tsx
function _extends() {
  return _extends = Object.assign ? Object.assign.bind() : function(n) {
    for (var e = 1; e < arguments.length; e++) {
      var t = arguments[e];
      for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
    }
    return n;
  }, _extends.apply(null, arguments);
}
function BaseCbacPickerDialog({
  isOpen,
  onOpenChange,
  onConfirm,
  onCancel,
  title = "Select classification",
  submitDisabledReason,
  ...pickerProps
}) {
  const handleOpenChange = React__default.default.useCallback((open) => {
    if (!open) {
      onCancel();
    }
    onOpenChange(open);
  }, [onCancel, onOpenChange]);
  return /* @__PURE__ */ React__default.default.createElement(chunkC6IPNPMZ_cjs.Dialog, {
    isOpen,
    onOpenChange: handleOpenChange,
    title,
    footer: /* @__PURE__ */ React__default.default.createElement(CbacPickerDialogFooter, {
      onCancel,
      onConfirm,
      submitDisabledReason
    }),
    disablePointerDismissal: true
  }, /* @__PURE__ */ React__default.default.createElement(BaseCbacPicker, _extends({}, pickerProps, {
    showInfoBanner: pickerProps.showInfoBanner ?? true
  })));
}

// src/cbac-picker/utils/selectionLogic.ts
function toggleMarking(markingId, currentSelection, categories) {
  const isSelected = currentSelection.includes(markingId);
  let ownerGroup;
  for (const group of categories) {
    for (const marking of group.markings) {
      if (marking.id === markingId) {
        ownerGroup = group;
        break;
      }
    }
    if (ownerGroup !== void 0) {
      break;
    }
  }
  if (ownerGroup === void 0) {
    return currentSelection;
  }
  if (isSelected) {
    return currentSelection.filter((id) => id !== markingId);
  }
  if (ownerGroup.category.categoryType === "DISJUNCTIVE") {
    const sameCategoryMarkingIds = new Set(ownerGroup.markings.map((m) => m.id));
    const withoutSameCategory = currentSelection.filter((id) => !sameCategoryMarkingIds.has(id));
    return [...withoutSameCategory, markingId];
  }
  return [...currentSelection, markingId];
}
function computeMarkingStates(selectedIds, impliedIds, disallowedIds) {
  const states = /* @__PURE__ */ new Map();
  const selectedSet = new Set(selectedIds);
  const impliedSet = new Set(impliedIds);
  const disallowedSet = new Set(disallowedIds);
  const allIds = /* @__PURE__ */ new Set([...selectedIds, ...impliedIds, ...disallowedIds]);
  for (const id of allIds) {
    const isImplied2 = impliedSet.has(id);
    const isDisallowed2 = disallowedSet.has(id);
    const isSelected = selectedSet.has(id);
    if (isSelected) {
      states.set(id, "SELECTED");
    } else if (isImplied2 && isDisallowed2) {
      states.set(id, "IMPLIED_DISALLOWED");
    } else if (isImplied2) {
      states.set(id, "IMPLIED");
    } else if (isDisallowed2) {
      states.set(id, "DISALLOWED");
    }
  }
  return states;
}
function groupMarkingsByCategory2(markings, categories) {
  const markingsByCategoryId = /* @__PURE__ */ new Map();
  for (const marking of markings) {
    const existing = markingsByCategoryId.get(marking.categoryId);
    if (existing !== void 0) {
      existing.push(marking);
    } else {
      markingsByCategoryId.set(marking.categoryId, [marking]);
    }
  }
  const groups = [];
  for (const category of categories) {
    const categoryMarkings = markingsByCategoryId.get(category.id);
    if (categoryMarkings !== void 0 && categoryMarkings.length > 0) {
      groups.push({
        category,
        markings: categoryMarkings
      });
    }
  }
  return groups;
}

// src/cbac-picker/useCbacPickerState.ts
var EMPTY_GROUPS = [];
function useStableArray(arr) {
  const ref = React__default.default.useRef(arr);
  if (arr.length !== ref.current.length || arr.some((id, i) => id !== ref.current[i])) {
    ref.current = arr;
  }
  return ref.current;
}
function useCbacPickerState(selectedIds) {
  const stableSelectedIds = useStableArray(selectedIds);
  const {
    categories: rawCategories,
    isLoading: categoriesLoading,
    error: categoriesError,
    refetch: refetchCategories
  } = platformApis.useMarkingCategories();
  const {
    markings: rawMarkings,
    isLoading: markingsLoading,
    error: markingsError,
    refetch: refetchMarkings
  } = platformApis.useMarkings();
  const {
    banner: latestBanner,
    isLoading: bannerLoading,
    error: bannerError,
    refetch: refetchBanner
  } = platformApis.useCbacBanner({
    markingIds: stableSelectedIds
  });
  const bannerRef = React__default.default.useRef(latestBanner);
  if (latestBanner != null) {
    bannerRef.current = latestBanner;
  }
  const banner = latestBanner ?? bannerRef.current;
  const {
    restrictions,
    isLoading: restrictionsLoading,
    error: restrictionsError,
    refetch: refetchRestrictions
  } = platformApis.useCbacMarkingRestrictions({
    markingIds: stableSelectedIds
  });
  const impliedMarkingIds = restrictions?.impliedMarkings ?? chunkZMAWPOG5_cjs.EMPTY_ARRAY;
  const disallowedMarkingIds = restrictions?.disallowedMarkings ?? chunkZMAWPOG5_cjs.EMPTY_ARRAY;
  const requiredMarkingGroups = restrictions?.requiredMarkings ?? EMPTY_GROUPS;
  const isValid = restrictions?.isValid ?? true;
  const userSatisfiesMarkings = restrictions?.userSatisfiesMarkings ?? true;
  const isLoading = categoriesLoading || markingsLoading || restrictionsLoading || bannerLoading;
  const error = React__default.default.useMemo(() => {
    const errors = [categoriesError, markingsError, restrictionsError, bannerError].filter((e) => e != null);
    if (errors.length > 1) {
      return new AggregateError(errors, errors.map((e) => e.message).join("; "));
    }
    return errors[0];
  }, [categoriesError, markingsError, restrictionsError, bannerError]);
  const retry = React__default.default.useCallback(() => {
    refetchCategories();
    refetchMarkings();
    refetchBanner();
    refetchRestrictions();
  }, [refetchCategories, refetchMarkings, refetchBanner, refetchRestrictions]);
  const categoryGroups = React__default.default.useMemo(() => {
    if (rawCategories === void 0 || rawMarkings === void 0) {
      return [];
    }
    return groupMarkingsByCategory2(rawMarkings, rawCategories);
  }, [rawMarkings, rawCategories]);
  const markingStates = React__default.default.useMemo(() => computeMarkingStates(stableSelectedIds, impliedMarkingIds, disallowedMarkingIds), [stableSelectedIds, impliedMarkingIds, disallowedMarkingIds]);
  const resolvedRequiredGroups = React__default.default.useMemo(() => chunkZMAWPOG5_cjs.resolveRequiredGroups(categoryGroups, requiredMarkingGroups), [categoryGroups, requiredMarkingGroups]);
  return React__default.default.useMemo(() => ({
    categoryGroups,
    markingStates,
    banner,
    requiredMarkingGroups: resolvedRequiredGroups,
    isValid,
    userSatisfiesMarkings,
    disallowedMarkingIds,
    isLoading,
    error,
    retry
  }), [categoryGroups, markingStates, banner, resolvedRequiredGroups, isValid, userSatisfiesMarkings, disallowedMarkingIds, isLoading, error, retry]);
}

// src/cbac-picker/useCbacSelection.ts
function useCbacSelection(initialMarkingIds) {
  const [selectedIds, setSelectedIds] = React__default.default.useState(initialMarkingIds ?? chunkZMAWPOG5_cjs.EMPTY_ARRAY);
  const [prevInitialIds, setPrevInitialIds] = React__default.default.useState(initialMarkingIds);
  if (initialMarkingIds !== prevInitialIds) {
    setPrevInitialIds(initialMarkingIds);
    setSelectedIds(initialMarkingIds ?? chunkZMAWPOG5_cjs.EMPTY_ARRAY);
  }
  const pickerState = useCbacPickerState(selectedIds);
  const selectedIdsRef = React__default.default.useRef(selectedIds);
  selectedIdsRef.current = selectedIds;
  const categoryGroupsRef = React__default.default.useRef(pickerState.categoryGroups);
  categoryGroupsRef.current = pickerState.categoryGroups;
  const toggle = React__default.default.useCallback((markingId) => {
    setSelectedIds((prev) => toggleMarking(markingId, prev, categoryGroupsRef.current));
  }, []);
  const dismiss = React__default.default.useCallback(() => {
    setSelectedIds(chunkZMAWPOG5_cjs.EMPTY_ARRAY);
  }, []);
  const reset = React__default.default.useCallback(() => {
    setSelectedIds(initialMarkingIds ?? chunkZMAWPOG5_cjs.EMPTY_ARRAY);
  }, [initialMarkingIds]);
  return {
    selectedIds,
    selectedIdsRef,
    setSelectedIds,
    ...pickerState,
    toggle,
    dismiss,
    reset
  };
}

// src/cbac-picker/base/MaxClassificationField.module.css
var MaxClassificationField_default = {};

// src/cbac-picker/base/MaxClassificationField.tsx
function MaxClassificationField({
  classificationString,
  textColor,
  backgroundColors,
  helperText,
  className
}) {
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: classnames__default.default(MaxClassificationField_default.container, className)
  }, /* @__PURE__ */ React__default.default.createElement("p", {
    className: MaxClassificationField_default.label
  }, "Maximum allowed classification"), /* @__PURE__ */ React__default.default.createElement("div", {
    className: MaxClassificationField_default.bannerWrapper
  }, /* @__PURE__ */ React__default.default.createElement(chunkZMAWPOG5_cjs.BaseCbacBanner, {
    classificationString,
    textColor,
    backgroundColors
  })), helperText != null && /* @__PURE__ */ React__default.default.createElement("p", {
    className: MaxClassificationField_default.helperText
  }, helperText));
}

// src/cbac-picker/useConstraintCallout.tsx
function useConstraintCallout(maxClassificationConstraint) {
  const constraintMarkingIds = maxClassificationConstraint?.markingIds ?? chunkZMAWPOG5_cjs.EMPTY_ARRAY;
  const {
    banner: constraintBanner
  } = platformApis.useCbacBanner({
    markingIds: constraintMarkingIds,
    enabled: maxClassificationConstraint != null
  });
  return React__default.default.useMemo(() => {
    if (maxClassificationConstraint == null) {
      return void 0;
    }
    const resolved = chunkZMAWPOG5_cjs.resolveBannerDisplay(constraintBanner);
    return /* @__PURE__ */ React__default.default.createElement(MaxClassificationField, {
      classificationString: maxClassificationConstraint.bannerClassificationString,
      textColor: resolved.textColor,
      backgroundColors: resolved.backgroundColors,
      helperText: maxClassificationConstraint.helperText
    });
  }, [maxClassificationConstraint, constraintBanner]);
}

// src/cbac-picker/utils/validationMessages.ts
function getSubmitDisabledReason(ctx) {
  if (ctx.isValid) {
    return void 0;
  }
  if (ctx.requiredMarkingGroups.length > 0) {
    return "Selected markings do not include all required markings.";
  }
  const selectedSet = new Set(ctx.selectedIds);
  const hasDisallowedSelected = ctx.disallowedMarkingIds.some((id) => selectedSet.has(id));
  if (hasDisallowedSelected) {
    return "Selections include disallowed markings.";
  }
  if (!ctx.userSatisfiesMarkings) {
    return "Invalid configuration of markings. Please ensure that you have permission to use all selected markings.";
  }
  return "Invalid marking selection.";
}

// src/cbac-picker/CbacPickerDialog.tsx
function CbacPickerDialog({
  isOpen,
  onOpenChange,
  onConfirm,
  initialMarkingIds,
  maxClassificationConstraint
}) {
  const {
    selectedIds,
    categoryGroups,
    markingStates,
    banner,
    requiredMarkingGroups,
    isValid,
    userSatisfiesMarkings,
    disallowedMarkingIds,
    isLoading,
    error,
    toggle,
    dismiss,
    reset
  } = useCbacSelection(initialMarkingIds);
  const handleConfirm = React__default.default.useCallback(() => {
    onConfirm(selectedIds);
  }, [onConfirm, selectedIds]);
  const handleCancel = React__default.default.useCallback(() => {
    reset();
    onOpenChange(false);
  }, [reset, onOpenChange]);
  const hasInitialMarkings = initialMarkingIds !== void 0 && initialMarkingIds.length > 0;
  const submitDisabledReason = React__default.default.useMemo(() => getSubmitDisabledReason({
    isValid,
    requiredMarkingGroups,
    selectedIds,
    disallowedMarkingIds,
    userSatisfiesMarkings
  }), [isValid, requiredMarkingGroups, selectedIds, disallowedMarkingIds, userSatisfiesMarkings]);
  const constraintCallout = useConstraintCallout(maxClassificationConstraint);
  return /* @__PURE__ */ React__default.default.createElement(BaseCbacPickerDialog, {
    isOpen,
    onOpenChange,
    onConfirm: handleConfirm,
    onCancel: handleCancel,
    title: hasInitialMarkings ? "Edit classification" : "Add classification",
    categories: categoryGroups,
    markingStates,
    banner,
    onMarkingToggle: toggle,
    onDismissBanner: dismiss,
    requiredMarkingGroups,
    isValid,
    submitDisabledReason,
    validationCallouts: constraintCallout,
    isLoading,
    error
  });
}

// src/cbac-picker/CbacBannerPopover.tsx
function CbacBannerPopover({
  markingIds,
  onChange,
  maxClassificationConstraint,
  className
}) {
  const {
    banner,
    isLoading: bannerLoading,
    error: bannerError,
    refetch: refetchBanner
  } = platformApis.useCbacBanner({
    markingIds
  });
  const {
    categories,
    isLoading: categoriesLoading,
    error: categoriesError,
    refetch: refetchCategories
  } = platformApis.useMarkingCategories();
  const {
    markings,
    isLoading: markingsLoading,
    error: markingsError,
    refetch: refetchMarkings
  } = platformApis.useMarkings();
  const [isDialogOpen, setIsDialogOpen] = React__default.default.useState(false);
  const isLoading = bannerLoading || categoriesLoading || markingsLoading;
  const error = bannerError ?? categoriesError ?? markingsError;
  const handleRetry = React__default.default.useCallback(() => {
    refetchBanner();
    refetchCategories();
    refetchMarkings();
  }, [refetchBanner, refetchCategories, refetchMarkings]);
  const appliedMarkings = React__default.default.useMemo(() => chunkZMAWPOG5_cjs.groupMarkingsByCategory(markingIds, categories, markings), [markingIds, categories, markings]);
  const resolved = chunkZMAWPOG5_cjs.resolveBannerDisplay(banner);
  const description = markingIds.length === 0 ? "This data has no classification restrictions." : `This data is classified as: ${resolved.classificationString}`;
  const handleEditClick = React__default.default.useCallback(() => {
    setIsDialogOpen(true);
  }, []);
  const handleConfirm = React__default.default.useCallback((newMarkingIds) => {
    onChange(newMarkingIds);
    setIsDialogOpen(false);
  }, [onChange]);
  return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement(BaseCbacBannerPopover, {
    classificationString: resolved.classificationString,
    textColor: resolved.textColor,
    backgroundColors: resolved.backgroundColors,
    appliedMarkings,
    onEditClick: handleEditClick,
    description,
    isLoading,
    error,
    onRetry: handleRetry,
    className
  }), isDialogOpen && /* @__PURE__ */ React__default.default.createElement(CbacPickerDialog, {
    isOpen: isDialogOpen,
    onOpenChange: setIsDialogOpen,
    onConfirm: handleConfirm,
    initialMarkingIds: markingIds,
    maxClassificationConstraint
  }));
}
function CbacPicker({
  initialMarkingIds,
  onChange,
  maxClassificationConstraint,
  readOnly,
  className
}) {
  const {
    selectedIdsRef,
    setSelectedIds,
    categoryGroups,
    markingStates,
    banner,
    requiredMarkingGroups,
    isValid,
    isLoading,
    error
  } = useCbacSelection(initialMarkingIds);
  const handleMarkingToggle = React__default.default.useCallback((markingId) => {
    if (readOnly) {
      return;
    }
    const newSelection = toggleMarking(markingId, selectedIdsRef.current, categoryGroups);
    setSelectedIds(newSelection);
    onChange(newSelection);
  }, [readOnly, categoryGroups, onChange]);
  const handleDismiss = React__default.default.useCallback(() => {
    setSelectedIds(chunkZMAWPOG5_cjs.EMPTY_ARRAY);
    onChange(chunkZMAWPOG5_cjs.EMPTY_ARRAY);
  }, [onChange]);
  const constraintCallout = useConstraintCallout(maxClassificationConstraint);
  return /* @__PURE__ */ React__default.default.createElement(BaseCbacPicker, {
    categories: categoryGroups,
    markingStates,
    banner,
    onMarkingToggle: handleMarkingToggle,
    onDismissBanner: handleDismiss,
    requiredMarkingGroups,
    isValid,
    readOnly,
    isLoading,
    error,
    validationCallouts: constraintCallout,
    className
  });
}

// src/public/experimental/cbac-picker.ts
var CbacPicker2 = chunkOWBDRWL6_cjs.withOsdkMetrics(CbacPicker, "CbacPicker");

exports.BaseCbacPicker = BaseCbacPicker;
exports.BaseCbacPickerDialog = BaseCbacPickerDialog;
exports.CbacBannerPopover = CbacBannerPopover;
exports.CbacPicker = CbacPicker2;
exports.CbacPickerDialog = CbacPickerDialog;
exports.MaxClassificationField = MaxClassificationField;
exports.computeMarkingStates = computeMarkingStates;
exports.groupMarkingsByCategory = groupMarkingsByCategory2;
exports.toggleMarking = toggleMarking;
//# sourceMappingURL=chunk-AJTZWKW2.cjs.map
//# sourceMappingURL=chunk-AJTZWKW2.cjs.map