/*
 * 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 { Button } from "@base-ui/react/button";
import { Input } from "@base-ui/react/input";
import { ChevronLeft, ChevronRight, Download, FloppyDisk, Highlight, Menu, Minus, Plus, RotatePage, Search, ZoomToFit } from "@blueprintjs/icons";
import classnames from "classnames";
import React, { useCallback, useEffect, useState } from "react";
import { MAX_SCALE, MIN_SCALE } from "../constants.js";
import styles from "./PdfViewerToolbar.module.css";
export function PdfViewerToolbar({
  currentPage,
  numPages,
  scale,
  autoSize,
  sidebarOpen,
  onPageChange,
  onZoomIn,
  onZoomOut,
  onAutoSizeToggle,
  onSearchOpen,
  onSidebarToggle,
  onDownload,
  enableDownload,
  onRotateLeft,
  onRotateRight,
  enableHighlight,
  highlightModeActive,
  onHighlightToggle,
  enableFormSave = false,
  onFormSave
}) {
  const [pageInputValue, setPageInputValue] = useState(String(currentPage));

  // Sync input display when currentPage changes from scrolling
  useEffect(function () {
    setPageInputValue(String(currentPage));
  }, [currentPage]);
  const handlePrevPage = useCallback(() => {
    if (currentPage > 1) {
      const newPage = currentPage - 1;
      onPageChange(newPage);
      setPageInputValue(String(newPage));
    }
  }, [currentPage, onPageChange]);
  const handleNextPage = useCallback(() => {
    if (currentPage < numPages) {
      const newPage = currentPage + 1;
      onPageChange(newPage);
      setPageInputValue(String(newPage));
    }
  }, [currentPage, numPages, onPageChange]);
  const handlePageInputChange = useCallback(e => {
    setPageInputValue(e.target.value);
  }, []);
  const handlePageInputKeyDown = useCallback(e => {
    if (e.key === "Enter") {
      const page = parseInt(pageInputValue, 10);
      if (!isNaN(page) && page >= 1 && page <= numPages) {
        onPageChange(page);
      } else {
        setPageInputValue(String(currentPage));
      }
    }
  }, [pageInputValue, numPages, onPageChange, currentPage]);
  const handlePageInputBlur = useCallback(() => {
    setPageInputValue(String(currentPage));
  }, [currentPage]);
  const scalePercent = `${Math.round(scale * 100)}%`;
  return /*#__PURE__*/React.createElement("div", {
    className: styles.toolbar
  }, /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onSidebarToggle,
    "aria-label": sidebarOpen ? "Close sidebar" : "Open sidebar",
    title: sidebarOpen ? "Close sidebar" : "Open sidebar",
    "aria-pressed": sidebarOpen,
    type: "button"
  }, /*#__PURE__*/React.createElement(Menu, {
    size: 16
  })), /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement("div", {
    className: styles.toolbarGroup
  }, /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: handlePrevPage,
    disabled: currentPage <= 1,
    "aria-label": "Previous page",
    title: "Previous page",
    type: "button"
  }, /*#__PURE__*/React.createElement(ChevronLeft, {
    size: 16
  })), /*#__PURE__*/React.createElement(Input, {
    className: styles.pageInput,
    type: "text",
    value: pageInputValue,
    onChange: handlePageInputChange,
    onKeyDown: handlePageInputKeyDown,
    onBlur: handlePageInputBlur,
    "aria-label": "Page number"
  }), /*#__PURE__*/React.createElement("span", {
    className: styles.pageCount
  }, "of ", numPages), /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: handleNextPage,
    disabled: currentPage >= numPages,
    "aria-label": "Next page",
    title: "Next page",
    type: "button"
  }, /*#__PURE__*/React.createElement(ChevronRight, {
    size: 16
  }))), /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement("div", {
    className: styles.toolbarGroup
  }, /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onZoomOut,
    disabled: scale <= MIN_SCALE,
    "aria-label": "Zoom out",
    title: "Zoom out",
    type: "button"
  }, /*#__PURE__*/React.createElement(Minus, {
    size: 16
  })), /*#__PURE__*/React.createElement("span", {
    className: styles.scaleDisplay
  }, scalePercent), /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onZoomIn,
    disabled: scale >= MAX_SCALE,
    "aria-label": "Zoom in",
    title: "Zoom in",
    type: "button"
  }, /*#__PURE__*/React.createElement(Plus, {
    size: 16
  })), /*#__PURE__*/React.createElement(Button, {
    className: classnames(styles.toolbarButton, autoSize && styles.toolbarButtonActive),
    onClick: onAutoSizeToggle,
    "aria-label": autoSize ? "Disable fit to width" : "Fit to width",
    title: autoSize ? "Disable fit to width" : "Fit to width",
    "aria-pressed": autoSize,
    type: "button"
  }, /*#__PURE__*/React.createElement(ZoomToFit, {
    size: 16
  }))), /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement("div", {
    className: styles.toolbarGroup
  }, /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onRotateLeft,
    "aria-label": "Rotate left",
    title: "Rotate left",
    type: "button"
  }, /*#__PURE__*/React.createElement(RotatePage, {
    size: 16
  })), /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onRotateRight,
    "aria-label": "Rotate right",
    title: "Rotate right",
    type: "button"
  }, /*#__PURE__*/React.createElement(RotatePage, {
    size: 16,
    style: {
      transform: "scaleX(-1)"
    }
  }))), enableHighlight && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement(Button, {
    className: classnames(styles.toolbarButton, highlightModeActive && styles.toolbarButtonActive),
    onClick: onHighlightToggle,
    "aria-label": highlightModeActive ? "Disable highlight mode" : "Enable highlight mode",
    title: highlightModeActive ? "Disable highlight mode" : "Enable highlight mode",
    "aria-pressed": highlightModeActive,
    type: "button"
  }, /*#__PURE__*/React.createElement(Highlight, {
    size: 16
  }))), /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onSearchOpen,
    "aria-label": "Search",
    title: "Search",
    type: "button"
  }, /*#__PURE__*/React.createElement(Search, {
    size: 16
  })), enableDownload && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onDownload,
    "aria-label": "Download",
    title: "Download",
    type: "button"
  }, /*#__PURE__*/React.createElement(Download, {
    size: 16
  }))), enableFormSave && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: styles.separator
  }), /*#__PURE__*/React.createElement(Button, {
    className: styles.toolbarButton,
    onClick: onFormSave,
    "aria-label": "Save form",
    title: "Save form",
    type: "button"
  }, /*#__PURE__*/React.createElement(FloppyDisk, {
    size: 16
  }))));
}
//# sourceMappingURL=PdfViewerToolbar.js.map