import Buffer from "buffer";
import processBrowser from "process/browser";
import console from "console-browserify";
var buffer = {
  Buffer
};
import { jsx } from "react/jsx-runtime";
import { useRef, useState } from "react";
import "./global-hover-preview.css";
import { useExecutionDump } from "./store";
const size = 400;
const GlobalHoverPreview = () => {
  var _a;
  const wrapperRef = useRef(null);
  const hoverTask = useExecutionDump((store) => store.hoverTask);
  const activeTask = useExecutionDump((store) => store.activeTask);
  const hoverTimestamp = useExecutionDump((store) => store.hoverTimestamp);
  const hoverPreviewConfig = useExecutionDump(
    (store) => store.hoverPreviewConfig
  );
  const [imageW, setImageW] = useState(size);
  const [imageH, setImageH] = useState(size);
  const images = (_a = hoverTask == null ? void 0 : hoverTask.recorder) == null ? void 0 : _a.filter((item) => {
    let valid = Boolean(item.screenshot);
    if (hoverTimestamp) {
      valid = valid && item.ts >= hoverTimestamp;
    }
    return valid;
  }).map((item) => item.screenshot);
  const { x, y } = hoverPreviewConfig || {};
  let left = 0;
  let top = 0;
  const shouldShow = hoverTask !== activeTask && (images == null ? void 0 : images.length) && typeof x !== "undefined" && typeof y !== "undefined";
  if (shouldShow) {
    const { clientWidth, clientHeight } = document.body;
    const widthInPractice = imageW >= imageH ? size : size * (imageW / imageH);
    const heightInPractice = imageW >= imageH ? size * (imageH / imageW) : size;
    left = x + widthInPractice > clientWidth ? clientWidth - widthInPractice : x;
    top = y + heightInPractice > clientHeight ? clientHeight - heightInPractice : y;
  }
  return shouldShow ? /* @__PURE__ */ jsx(
    "div",
    {
      className: "global-hover-preview",
      style: { left, top },
      ref: wrapperRef,
      children: (images == null ? void 0 : images.length) ? (
        // biome-ignore lint/a11y/useAltText: <explanation>
        /* @__PURE__ */ jsx(
          "img",
          {
            src: images[0],
            onLoad: (img) => {
              const imgElement = img.target;
              const width = imgElement.naturalWidth;
              const height = imgElement.naturalHeight;
              setImageW(width);
              setImageH(height);
            }
          }
        )
      ) : null
    }
  ) : null;
};
var global_hover_preview_default = GlobalHoverPreview;
export {
  global_hover_preview_default as default
};
