/**
 * Test setup configuration for the UI component library.
 * This file is imported by all test files to ensure consistent test environment.
 */

// // Global test setup
// before(() => {
//   // Set up global test configuration
//   // eslint-disable-next-line no-console
//   console.log('Setting up test environment...')
// })

// beforeEach(() => {
//   // Clean up DOM before each test
//   document.body.innerHTML = ''

//   // Reset any global state
//   if (window.localStorage) {
//     window.localStorage.clear()
//   }
//   if (window.sessionStorage) {
//     window.sessionStorage.clear()
//   }
// })

afterEach(() => {
  // Clean up after each test
  // document.body.innerHTML = ''

  // Clear any pending timers
  const highestTimeoutId = window.setTimeout(() => {}, 0)
  for (let i = 0; i < highestTimeoutId; i++) {
    window.clearTimeout(i)
  }

  // Clear any pending intervals
  const highestIntervalId = window.setInterval(() => {}, 9999)
  for (let i = 0; i < highestIntervalId; i++) {
    window.clearInterval(i)
  }
})

// after(() => {
//   // eslint-disable-next-line no-console
//   console.log('Test environment cleanup complete.')
// })

// Global test utilities
declare global {
  interface Window {
    testUtils: {
      waitForElement: (selector: string, timeout?: number) => Promise<Element>
      waitForCondition: (condition: () => boolean, timeout?: number) => Promise<void>
    }
  }
}

// Add test utilities to global scope
window.testUtils = {
  /**
   * Wait for an element to appear in the DOM
   */
  async waitForElement(selector: string, timeout = 5000): Promise<Element> {
    return new Promise((resolve, reject) => {
      const startTime = Date.now()

      function checkElement() {
        const element = document.querySelector(selector)
        if (element) {
          resolve(element)
          return
        }

        if (Date.now() - startTime > timeout) {
          reject(new Error(`Element with selector "${selector}" not found within ${timeout}ms`))
          return
        }

        setTimeout(checkElement, 50)
      }

      checkElement()
    })
  },

  /**
   * Wait for a condition to be true
   */
  async waitForCondition(condition: () => boolean, timeout = 5000): Promise<void> {
    return new Promise((resolve, reject) => {
      const startTime = Date.now()

      function checkCondition() {
        try {
          if (condition()) {
            resolve()
            return
          }
        } catch {
          // Condition threw an error, continue waiting
        }

        if (Date.now() - startTime > timeout) {
          reject(new Error(`Condition not met within ${timeout}ms`))
          return
        }

        setTimeout(checkCondition, 50)
      }

      checkCondition()
    })
  },
}

// Export setup for explicit imports
export {}
