// Import stylesheets
import "./index.css";

// Import utilities
import {
  initSwatches,
  scheduleFallbackReveal,
} from "./utils/mountSwatches.tsx";
import { onDomReady, observeDomChanges, scheduleInit } from "./utils/domReady";

/**
 * Main entry point for B3 Swatches frontend
 */
// Initialize on DOM content loaded or immediately if already loaded.
onDomReady(() => {
  initSwatches();
});

// Schedule fallback reveal — ensures native <select> shows if React fails.
scheduleFallbackReveal();

// Page builders (including Cornerstone) often inject/replace product forms after DOMContentLoaded.
// Observe DOM changes and initialize swatches when new mount points appear.
observeDomChanges({
  onSwatchMountAdded: () => {
    scheduleInit(() => {
      initSwatches();
    });
  },
});

// Defensive: run once on full load too (covers late scripts/images that affect layout timing).
window.addEventListener("load", () => {
  scheduleInit(() => {
    initSwatches();
  });
});
