export default defineNuxtPlugin(() => {
  // This plugin runs only on the client side to handle form data hydration
  const { $formStore } = useNuxtApp()
  
  // Ensure form store data is properly initialized on client hydration
  if (process.client) {
    // Add any additional client-side form data initialization logic here
    console.log('[FormHydration] Client-side form data hydration plugin loaded')
    
    // Handle any form data that needs to be restored after hydration
    const restoreFormData = () => {
      try {
        // Check if there's any persisted form data that needs restoration
        const persistedData = sessionStorage.getItem('form-builder-data')
        if (persistedData && $formStore) {
          const parsed = JSON.parse(persistedData)
          if (parsed.previewFormData) {
            $formStore.updatePreviewFormData(parsed.previewFormData)
            console.log('[FormHydration] Restored form data from session storage')
          }
        }
      } catch (error) {
        console.warn('[FormHydration] Failed to restore form data:', error)
      }
    }
    
    // Restore data after page load
    window.addEventListener('load', restoreFormData)
    
    // Clean up
    return {
      provide: {
        formHydration: {
          restoreFormData
        }
      }
    }
  }
})
