/**
 * Shared test utilities for theme-engine tests.
 *
 * Provides common DOM reset, setup, and teardown helpers used across
 * theme-studio-review.test.ts and engine.test.ts.
 */

import { _resetCache, applyTheme, teardownTheme } from './engine'
import { defaultTheme } from './presets'

/** Strip all theme-related attributes and content from the document. */
export function resetDom(): void {
  document.head.replaceChildren()
  document.body.replaceChildren()
  document.documentElement.removeAttribute('data-theme')
  document.documentElement.removeAttribute('data-accent')
  document.documentElement.removeAttribute('data-density')
  document.documentElement.removeAttribute('data-icon-set')
  document.documentElement.removeAttribute('style')
}

/** Clear storage, reset engine cache, reset DOM, optionally apply the default theme. */
export function setupThemeTest(applyDefault = true): void {
  localStorage.clear()
  teardownTheme()
  _resetCache()
  resetDom()
  if (applyDefault) applyTheme(defaultTheme())
}

/** Disconnect observer, reset cache, clear storage, reset DOM. */
export function teardownThemeTest(): void {
  teardownTheme()
  _resetCache()
  localStorage.clear()
  resetDom()
}
