import "../../chunks/chunk.QU3DSPNU.js";
import { html, render } from "lit";
import { page, userEvent } from "@vitest/browser/context";
async function fixture(template) {
  const container = document.createElement("div");
  document.body.appendChild(container);
  render(template, container);
  await new Promise((resolve) => setTimeout(resolve, 0));
  await Promise.resolve();
  const element = container.firstElementChild;
  return element;
}
function aTimeout(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitUntil(condition, message, options = {}) {
  const { interval = 50, timeout: timeout2 = 1e3 } = options;
  const startTime = Date.now();
  while (Date.now() - startTime <= timeout2) {
    const result = await condition();
    if (result) return;
    await new Promise((resolve) => setTimeout(resolve, interval));
  }
  throw new Error(message || `waitUntil timed out after ${timeout2}ms`);
}
async function clickOnElement(el, _position = "center", _offsetX = 0, _offsetY = 0) {
  const rect = el.getBoundingClientRect();
  const position = getClickPosition(rect, _position, _offsetX, _offsetY);
  try {
    await withTimeout(userEvent.click(el, { position }), 1e3);
  } catch (error) {
    await withTimeout(userEvent.click(el, { position, force: true }), 1e3);
  }
}
async function withTimeout(promise, ms) {
  let timeoutId;
  const timeout2 = new Promise((_, reject) => {
    timeoutId = setTimeout(() => {
      reject(new Error(`click timed out after ${ms}ms`));
    }, ms);
  });
  try {
    return await Promise.race([promise, timeout2]);
  } finally {
    if (timeoutId) clearTimeout(timeoutId);
  }
}
function getClickPosition(rect, position, offsetX, offsetY) {
  const centerX = rect.width / 2;
  const centerY = rect.height / 2;
  let x = centerX;
  let y = centerY;
  switch (position) {
    case "top":
      y = 1;
      break;
    case "bottom":
      y = Math.max(1, rect.height - 1);
      break;
    case "left":
      x = 1;
      break;
    case "right":
      x = Math.max(1, rect.width - 1);
      break;
    case "center":
    default:
      break;
  }
  return {
    x: Math.max(1, x + offsetX),
    y: Math.max(1, y + offsetY)
  };
}
async function moveMouseOnElement(el, _position = "center", _offsetX = 0, _offsetY = 0) {
  await userEvent.hover(el);
}
async function dragElement(source, deltaX = 0, deltaY = 0) {
  const rect = source.getBoundingClientRect();
  const targetX = rect.left + rect.width / 2 + deltaX;
  const targetY = rect.top + rect.height / 2 + deltaY;
  await userEvent.hover(source);
  const event = new MouseEvent("mousedown", {
    bubbles: true,
    cancelable: true,
    clientX: rect.left + rect.width / 2,
    clientY: rect.top + rect.height / 2
  });
  source.dispatchEvent(event);
  const moveEvent = new MouseEvent("mousemove", {
    bubbles: true,
    cancelable: true,
    clientX: targetX,
    clientY: targetY
  });
  source.dispatchEvent(moveEvent);
  const upEvent = new MouseEvent("mouseup", {
    bubbles: true,
    cancelable: true,
    clientX: targetX,
    clientY: targetY
  });
  source.dispatchEvent(upEvent);
}
function isWebKit() {
  return navigator.userAgent.toLowerCase().indexOf("safari") > -1 && navigator.userAgent.toLowerCase().indexOf("chrome") < 0;
}
async function tabNext(options) {
  if (isWebKit()) {
    if (options?.shift) {
      await userEvent.keyboard("{Alt>}{Shift>}{Tab}{/Shift}{/Alt}");
      return;
    }
    await userEvent.keyboard("{Alt>}{Tab}{/Alt}");
    return;
  }
  await userEvent.tab(options);
}
function isChromium() {
  return navigator.userAgent.toLowerCase().indexOf("chrome") > -1;
}
function isFirefox() {
  return navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
}
function onlyDate(date) {
  if (!date) return void 0;
  return date.toISOString().split("T")[0];
}
function timeout(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
function conditionToBeTrue(condition, timeoutMs = 1e3) {
  return new Promise((resolve, reject) => {
    const startTime = Date.now();
    const check = () => {
      if (condition()) {
        resolve();
      } else if (Date.now() - startTime > timeoutMs) {
        reject(new Error("Condition was not met within timeout"));
      } else {
        requestAnimationFrame(check);
      }
    };
    check();
  });
}
async function setViewportSize(width, height) {
  const originalSize = {
    width: window.innerWidth,
    height: window.innerHeight
  };
  if (page) {
    await page.viewport(width, height);
  }
  return async () => {
    if (page) {
      await page.viewport(originalSize.width, originalSize.height);
    }
  };
}
export {
  aTimeout,
  clickOnElement,
  conditionToBeTrue,
  dragElement,
  fixture,
  html,
  isChromium,
  isFirefox,
  isWebKit,
  moveMouseOnElement,
  onlyDate,
  setViewportSize,
  tabNext,
  timeout,
  waitUntil
};
