/*
 * Copyright 2025 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 { Menu } from "@base-ui/react/menu";
import { ChevronDown, Pin, Remove, Settings, Sort, SortAlphabetical, SortAlphabeticalDesc, Unpin, VerticalDistribution } from "@blueprintjs/icons";
import classNames from "classnames";
import React, { useCallback, useState } from "react";
import { usePortalContainer } from "../shared/PortalContainerContext.js";
import { useObjectTableLabels } from "./ObjectTableLabels.js";
import { TableHeaderContent } from "./TableHeaderContent.js";
import styles from "./TableHeaderWithPopover.module.css";
function HeaderMenuItem({
  onClick,
  icon: Icon,
  label,
  active = false
}) {
  return /*#__PURE__*/React.createElement(Menu.Item, {
    closeOnClick: true,
    className: classNames(styles.osdkCenterContainer, styles.osdkContentGap, styles.osdkHeaderMenuItem, active && styles.osdkHeaderActiveMenuItem),
    onClick: onClick
  }, /*#__PURE__*/React.createElement(Icon, {
    className: styles.osdkHeaderIcon
  }), /*#__PURE__*/React.createElement("span", null, label));
}
export function TableHeaderWithPopover({
  header,
  table,
  isColumnPinned,
  onResetSize,
  columnOptions,
  featureFlags,
  onOpenColumnConfig,
  onOpenMultiSort,
  onColumnHeaderClick
}) {
  const portalContainer = usePortalContainer();
  const labels = useObjectTableLabels();
  const {
    showSortingItems = false,
    showPinningItems = false,
    showResizeItem = false,
    showConfigItem = false
  } = featureFlags ?? {};
  const {
    setColumnPinning,
    setSorting
  } = table;
  const currentSorting = table.getState().sorting;
  const [isOpen, setIsOpen] = useState(false);
  const handlePinLeft = useCallback(() => {
    setColumnPinning(prev => {
      return {
        left: [...(prev.left || []), header.column.id],
        right: prev.right?.filter(id => id !== header.column.id) || []
      };
    });
  }, [header.column.id, setColumnPinning]);
  const handleUnpin = useCallback(() => {
    setColumnPinning(prev => {
      return {
        left: prev.left?.filter(id => id !== header.column.id) || [],
        right: prev.right?.filter(id => id !== header.column.id) || []
      };
    });
  }, [header.column.id, setColumnPinning]);
  const handleSortAscending = useCallback(() => {
    header.column.toggleSorting(false);
    setSorting?.([{
      id: header.column.id,
      desc: false
    }]);
  }, [header.column, setSorting]);
  const handleSortDescending = useCallback(() => {
    header.column.toggleSorting(true);
    setSorting?.([{
      id: header.column.id,
      desc: true
    }]);
  }, [header.column, setSorting]);
  const handleClearAllSorts = useCallback(() => {
    header.column.clearSorting();
    setSorting?.([]);
  }, [header.column, setSorting]);
  const handleResetSize = useCallback(() => {
    header.column.resetSize();
    if (onResetSize) {
      onResetSize();
    }
  }, [header.column, onResetSize]);
  const handleInteraction = useCallback(e => {
    e.preventDefault();
    setIsOpen(prev => !prev);
  }, []);
  const handleHeaderClick = useCallback(() => {
    onColumnHeaderClick?.(header.column.id);
  }, [header.column.id, onColumnHeaderClick]);
  const handleOpenColumnConfig = useCallback(() => {
    onOpenColumnConfig?.();
    setIsOpen(false);
  }, [onOpenColumnConfig]);
  const handleOpenMultiSort = useCallback(() => {
    onOpenMultiSort?.();
    setIsOpen(false);
  }, [onOpenMultiSort]);
  const isSorted = header.column.getIsSorted();
  const isSortable = header.column.getCanSort();
  const sortIndex = currentSorting?.findIndex(s => s.id === header.column.id) ?? -1;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Menu.Root, {
    open: isOpen,
    onOpenChange: setIsOpen,
    modal: false
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames(styles.osdkCenterContainer, styles.osdkContentGap, styles.osdkHeaderContainer),
    onContextMenu: handleInteraction
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames(styles.osdkCenterContainer, styles.osdkContentGap, styles.osdkHeaderContentLeft, onColumnHeaderClick && styles.osdkHeaderContentLeftClickable),
    onClick: onColumnHeaderClick ? handleHeaderClick : undefined
  }, isColumnPinned && /*#__PURE__*/React.createElement(Pin, {
    className: styles.osdkHeaderIcon
  }), /*#__PURE__*/React.createElement(TableHeaderContent, {
    header: header
  })), /*#__PURE__*/React.createElement("div", {
    className: classNames(styles.osdkCenterContainer, styles.osdkContentGap, styles.osdkHeaderContentRight)
  }, isSorted && /*#__PURE__*/React.createElement("div", {
    className: styles.osdkCenterContainer
  }, isSorted === "asc" ? /*#__PURE__*/React.createElement(SortAlphabetical, {
    className: styles.osdkHeaderIcon
  }) : /*#__PURE__*/React.createElement(SortAlphabeticalDesc, {
    className: styles.osdkHeaderIcon
  }), currentSorting.length > 1 && sortIndex >= 0 && /*#__PURE__*/React.createElement("span", {
    className: styles.sortIndex
  }, sortIndex + 1)), (showPinningItems || showSortingItems && isSortable || showResizeItem || showConfigItem) && /*#__PURE__*/React.createElement(Menu.Trigger, {
    "aria-label": labels.headerMenuAriaLabel(header.column.id),
    className: classNames(styles.osdkCenterContainer, styles.osdkHeaderPopoverTrigger)
  }, /*#__PURE__*/React.createElement(ChevronDown, {
    className: styles.osdkHeaderIcon
  }))), /*#__PURE__*/React.createElement(Menu.Portal, {
    container: portalContainer
  }, /*#__PURE__*/React.createElement(Menu.Positioner, {
    sideOffset: 4
  }, /*#__PURE__*/React.createElement(Menu.Popup, {
    className: styles.osdkHeaderPopup
  }, showPinningItems && !isColumnPinned && /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handlePinLeft,
    icon: Pin,
    label: labels.headerMenuPinColumn
  }), showPinningItems && isColumnPinned && /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleUnpin,
    icon: Unpin,
    label: labels.headerMenuUnpinColumn,
    active: true
  }), showSortingItems && isSortable && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleSortAscending,
    icon: SortAlphabetical,
    label: labels.headerMenuSortAscending,
    active: isSorted === "asc"
  }), /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleSortDescending,
    icon: SortAlphabeticalDesc,
    label: labels.headerMenuSortDescending,
    active: isSorted === "desc"
  }), columnOptions?.some(col => col.canSort) && /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleOpenMultiSort,
    icon: Sort,
    label: labels.headerMenuSortOnMultipleColumns
  })), showSortingItems && !!currentSorting?.length && /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleClearAllSorts,
    icon: Remove,
    label: labels.headerMenuClearAllSorts
  }), showResizeItem && /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleResetSize,
    icon: VerticalDistribution,
    label: labels.headerMenuResetColumnSize
  }), showConfigItem && /*#__PURE__*/React.createElement(HeaderMenuItem, {
    onClick: handleOpenColumnConfig,
    icon: Settings,
    label: labels.headerMenuConfigureColumns
  })))))));
}
//# sourceMappingURL=TableHeaderWithPopover.js.map