"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 global_hover_preview_exports = {};
__export(global_hover_preview_exports, {
  default: () => global_hover_preview_default
});
module.exports = __toCommonJS(global_hover_preview_exports);
var import_buffer = __toESM(require("buffer"));
var import_browser = __toESM(require("process/browser"));
var import_console_browserify = __toESM(require("console-browserify"));
var buffer = {
  Buffer: import_buffer.default
};
var import_jsx_runtime = (
  // biome-ignore lint/a11y/useAltText: <explanation>
  require("react/jsx-runtime")
);
var import_react = require("react");
var import_global_hover_preview = require("./global-hover-preview.css");
var import_store = require("./store");
const size = 400;
const GlobalHoverPreview = () => {
  var _a;
  const wrapperRef = (0, import_react.useRef)(null);
  const hoverTask = (0, import_store.useExecutionDump)((store) => store.hoverTask);
  const activeTask = (0, import_store.useExecutionDump)((store) => store.activeTask);
  const hoverTimestamp = (0, import_store.useExecutionDump)((store) => store.hoverTimestamp);
  const hoverPreviewConfig = (0, import_store.useExecutionDump)(
    (store) => store.hoverPreviewConfig
  );
  const [imageW, setImageW] = (0, import_react.useState)(size);
  const [imageH, setImageH] = (0, import_react.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__ */ (0, import_jsx_runtime.jsx)(
    "div",
    {
      className: "global-hover-preview",
      style: { left, top },
      ref: wrapperRef,
      children: (images == null ? void 0 : images.length) ? /* @__PURE__ */ (0, import_jsx_runtime.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;
