/**
 * Custom hook: persistente Firmenname + Origin-Domain für die
 * ComplianceCheckTab-Form. Priorisierte Werte vor der LLM-basierten
 * extracted_profile-Inferenz.
 */

import { useEffect, useState } from 'react'

const STORAGE_KEY_COMPANY = 'compliance-check-company-name'
const STORAGE_KEY_DOMAIN = 'compliance-check-origin-domain'


function readInitial(key: string): string {
  if (typeof window === 'undefined') return ''
  return localStorage.getItem(key) || ''
}


export function useCompanyOrigin() {
  const [companyName, setCompanyName] = useState<string>(
    () => readInitial(STORAGE_KEY_COMPANY),
  )
  const [originDomain, setOriginDomain] = useState<string>(
    () => readInitial(STORAGE_KEY_DOMAIN),
  )

  useEffect(() => {
    try {
      localStorage.setItem(STORAGE_KEY_COMPANY, companyName)
    } catch { /* quota */ }
  }, [companyName])

  useEffect(() => {
    try {
      localStorage.setItem(STORAGE_KEY_DOMAIN, originDomain)
    } catch { /* quota */ }
  }, [originDomain])

  return { companyName, setCompanyName, originDomain, setOriginDomain }
}
