/*
 * 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 classnames from "classnames";
import React, { useCallback, useMemo } from "react";
import { PdfViewerSidebarHeader } from "./PdfViewerSidebarHeader.js";
import styles from "./PdfViewerOutlineSidebar.module.css.js";
const INDENT_PER_DEPTH = 16;
export function PdfViewerOutlineSidebar({
  outlineItems,
  currentPage,
  onItemClick,
  sidebarMode,
  onSidebarModeChange,
  outlineIcons
}) {
  const activeIndex = useMemo(() => {
    return computeActiveIndex(outlineItems, currentPage);
  }, [outlineItems, currentPage]);
  if (outlineItems.length === 0) {
    return /*#__PURE__*/React.createElement("div", {
      className: styles.sidebar
    }, /*#__PURE__*/React.createElement(PdfViewerSidebarHeader, {
      sidebarMode: sidebarMode,
      onSidebarModeChange: onSidebarModeChange
    }), /*#__PURE__*/React.createElement("div", {
      className: styles.emptyState
    }, /*#__PURE__*/React.createElement("div", {
      className: styles.emptyStateText
    }, "No outline available")));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: styles.sidebar
  }, /*#__PURE__*/React.createElement(PdfViewerSidebarHeader, {
    sidebarMode: sidebarMode,
    onSidebarModeChange: onSidebarModeChange
  }), /*#__PURE__*/React.createElement("div", {
    className: styles.scrollContainer
  }, outlineItems.map((item, index) => /*#__PURE__*/React.createElement(PdfViewerOutlineItem, {
    key: index,
    item: item,
    isActive: index === activeIndex,
    onClick: onItemClick,
    iconComponent: outlineIcons?.[item.depth]
  }))));
}
function PdfViewerOutlineItem({
  item,
  isActive,
  onClick,
  iconComponent: IconComponent
}) {
  const handleClick = useCallback(() => {
    onClick(item.pageNumber);
  }, [onClick, item.pageNumber]);
  const handleKeyDown = useCallback(e => {
    if (e.key === "Enter" || e.key === " ") {
      e.preventDefault();
      onClick(item.pageNumber);
    }
  }, [onClick, item.pageNumber]);
  const itemClassName = classnames(styles.outlineItem, isActive && styles.outlineItemActive, item.bold && styles.outlineItemBold, item.italic && styles.outlineItemItalic);
  const paddingLeft = INDENT_PER_DEPTH * item.depth + 12;
  return /*#__PURE__*/React.createElement("div", {
    className: itemClassName,
    onClick: handleClick,
    onKeyDown: handleKeyDown,
    role: "button",
    tabIndex: 0,
    "aria-label": item.title,
    "aria-current": isActive ? "location" : undefined,
    style: {
      paddingLeft
    }
  }, IconComponent != null && /*#__PURE__*/React.createElement("span", {
    className: styles.outlineItemIcon
  }, /*#__PURE__*/React.createElement(IconComponent, null)), /*#__PURE__*/React.createElement("span", {
    className: styles.outlineItemTitle
  }, item.title));
}
function computeActiveIndex(items, currentPage) {
  const idx = items.findIndex(item => item.pageNumber > currentPage);
  return idx === -1 ? items.length - 1 : idx - 1;
}
//# sourceMappingURL=PdfViewerOutlineSidebar.js.map