import { useEffect } from 'react'
import GeschossSettingsDialog from './components/GeschossSettingsDialog'
import { notifyReady } from './lib/rhinoBridge'

function bridgeSend(type, payload = {}) {
  if (!window.RHINO_MODE) { console.log('[Bridge] →', type, payload); return }
  const json = JSON.stringify({ type, payload })
  document.title = 'RHINOMSG::' + json
}

export default function GeschossSettingsApp() {
  const initial = (typeof window !== 'undefined' && window.PANEL_PARAMS) || {}

  useEffect(() => {
    notifyReady()
    const blockContext = (ev) => ev.preventDefault()
    document.addEventListener('contextmenu', blockContext)
    return () => document.removeEventListener('contextmenu', blockContext)
  }, [])

  if (!initial || typeof initial !== 'object') {
    return <div style={{ padding: 20, color: 'var(--text-muted)' }}>Keine Daten</div>
  }

  return (
    <GeschossSettingsDialog
      embedded
      geschoss={initial}
      onSave={(updated) => bridgeSend('SAVE', updated)}
      onClose={() => bridgeSend('CANCEL', {})}
    />
  )
}
