import { useEffect } from 'react'
import type React from 'react'

/**
 * Full-viewport overlay style shared by the admin and login mounts so the CMS
 * UI renders above any consumer page chrome, with its own stacking context.
 */
export const ISOLATION_STYLE: React.CSSProperties = {
  position: 'fixed',
  inset: '0',
  zIndex: 50,
  overflow: 'auto',
  isolation: 'isolate',
}

/**
 * The admin (and its login screens) are private application UI and must never
 * be indexed. Ensure a `noindex, nofollow` robots meta tag is present while
 * mounted, as defense-in-depth alongside the server `robots.txt` Disallow
 * rule. This covers every consumer install (the admin page is typically a
 * client component that can't export Next.js route metadata).
 */
export function useRobotsNoIndex(): void {
  useEffect(() => {
    if (typeof document === 'undefined') return
    const prev = document.querySelector<HTMLMetaElement>('meta[name="robots"]')
    const prevContent = prev?.getAttribute('content') ?? null
    const meta = prev ?? document.createElement('meta')
    meta.setAttribute('name', 'robots')
    meta.setAttribute('content', 'noindex, nofollow')
    if (!prev) document.head.appendChild(meta)
    return () => {
      if (prevContent === null) {
        meta.remove()
      } else {
        meta.setAttribute('content', prevContent)
      }
    }
  }, [])
}
