"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var render_sketch_exports = {};
__export(render_sketch_exports, {
  findOverlayContainer: () => findOverlayContainer,
  renderAnnotationOverlay: () => renderAnnotationOverlay,
  stableSeedFor: () => stableSeedFor
});
module.exports = __toCommonJS(render_sketch_exports);
var import_defaults = require("./defaults.js");
var import_overlay_handle = require("./overlay-handle.js");
const OVERLAY_STYLE = "position: absolute; left: 0; top: 0; width: 0; height: 0; pointer-events: none;";
const POSITIONED = /* @__PURE__ */ new Set(["relative", "absolute", "fixed", "sticky"]);
const stableSeedFor = (input) => {
  let hash = 5381;
  for (let i = 0; i < input.length; i++) {
    hash = (hash << 5) + hash + input.charCodeAt(i) | 0;
  }
  return Math.abs(hash) % 2 ** 31;
};
const ensurePositioned = (element) => {
  const computedStyle = window.getComputedStyle(element);
  if (!POSITIONED.has(computedStyle.position)) {
    element.style.position = "relative";
  }
  if (computedStyle.zIndex === "auto") {
    element.style.zIndex = "0";
  }
};
const INLINE_DISPLAY_VALUES = /* @__PURE__ */ new Set(["inline", "contents"]);
const findOverlayContainer = (target) => {
  let current = target.parentElement;
  while (current && INLINE_DISPLAY_VALUES.has(window.getComputedStyle(current).display)) {
    current = current.parentElement;
  }
  return current;
};
const toRectArray = (list) => {
  if (Array.isArray(list)) {
    return list;
  }
  const out = [];
  for (let i = 0; i < list.length; i++) {
    out.push(list[i]);
  }
  return out;
};
const resolveRects = (target, getRects) => getRects ? toRectArray(getRects()) : toRectArray(target.getClientRects());
const renderAnnotationOverlay = (target, opts) => {
  if (typeof window === "undefined") {
    return null;
  }
  if (!target) {
    return null;
  }
  if (!target.parentElement) {
    return null;
  }
  const style = opts.style ?? import_defaults.DEFAULT_STYLE;
  const color = opts.color ?? import_defaults.DEFAULT_COLOR;
  const animate = opts.animate ?? import_defaults.DEFAULT_ANIMATE;
  const seed = opts.seed ?? stableSeedFor(target.textContent ?? "");
  const parent = findOverlayContainer(target);
  if (!parent) {
    return null;
  }
  ensurePositioned(parent);
  const overlay = document.createElement("div");
  overlay.style.cssText = OVERLAY_STYLE;
  overlay.style.zIndex = (0, import_defaults.getOverlayZIndex)(style);
  parent.appendChild(overlay);
  const rects = resolveRects(target, opts.getRects);
  const handle = (0, import_overlay_handle.createOverlayHandle)(overlay);
  const baseRemove = handle.remove;
  handle.remove = () => {
    baseRemove();
    overlay.remove();
  };
  handle.redraw({
    style,
    color,
    seed,
    animate,
    rects,
    container: parent,
    onRendered: opts.onRendered
  });
  return handle;
};
//# sourceMappingURL=render-sketch.js.map
