"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var text_selection_toolbar_exports = {};
__export(text_selection_toolbar_exports, {
  TextSelectionToolbar: () => TextSelectionToolbar
});
module.exports = __toCommonJS(text_selection_toolbar_exports);
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_viewer_inline_toolbar_scss = __toESM(require("./statics/styles/viewer-inline-toolbar-scss.js"));
var import_text_selection_listener = require("./text-selection-listener.js");
class TextSelectionToolbar extends import_react.default.Component {
  constructor(props) {
    super(props);
    this.addTextSelectionListener = (container) => {
      if (container && !this.removeTextSelectionListener) {
        this.removeTextSelectionListener = (0, import_text_selection_listener.addTextSelectionListener)(container, this.setSelectedText);
      }
    };
    this.setSelectedText = import_lodash.default.debounce(
      (selectedText, selectedTextPosition) => this.setState({ selectedText, selectedTextPosition }),
      50
    );
    this.state = { selectedText: "" };
  }
  componentDidMount() {
    this.addTextSelectionListener(this.props.container);
  }
  componentWillUnmount() {
    if (this.removeTextSelectionListener) {
      this.removeTextSelectionListener();
    }
  }
  componentWillReceiveProps(nextPros) {
    this.addTextSelectionListener(nextPros.container);
  }
  render() {
    const { selectedText, selectedTextPosition } = this.state;
    const { container, children } = this.props;
    if (!selectedText || !selectedTextPosition) {
      return null;
    }
    const { left } = container.getBoundingClientRect();
    const topOffset = 5;
    const containerOffset = this.props.container.getBoundingClientRect().top;
    const style = {
      top: selectedTextPosition.y - containerOffset - topOffset - window.scrollY,
      left: selectedTextPosition.x - left
    };
    return /* @__PURE__ */ import_react.default.createElement("div", { className: import_viewer_inline_toolbar_scss.default.toolbar, style }, children(selectedText));
  }
}
//# sourceMappingURL=text-selection-toolbar.js.map
