(function initVisaFlowXTimers(global) {
  "use strict";

  function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, Math.max(0, Number(ms) || 0)));
  }

  function debounce(fn, waitMs = 150) {
    let timer = null;
    return function debounced(...args) {
      if (timer) clearTimeout(timer);
      timer = setTimeout(() => {
        timer = null;
        fn.apply(this, args);
      }, waitMs);
    };
  }

  function formatDuration(ms) {
    const totalSeconds = Math.max(0, Math.ceil(Number(ms) / 1000));
    const hours = Math.floor(totalSeconds / 3600);
    const minutes = Math.floor((totalSeconds % 3600) / 60);
    const seconds = totalSeconds % 60;
    if (hours) return `${hours}h ${minutes}m ${seconds}s`;
    if (minutes) return `${minutes}m ${seconds}s`;
    return `${seconds}s`;
  }

  const Timers = Object.freeze({
    debounce,
    formatDuration,
    sleep
  });

  global.VisaFlowX = Object.assign(global.VisaFlowX || {}, { Timers });

  if (typeof module !== "undefined" && module.exports) {
    module.exports = Timers;
  }
})(typeof globalThis !== "undefined" ? globalThis : this);
