import React from 'react'
import { Box, Text } from 'ink'
import { type Address } from 'viem'
import { Surface } from '../../../ui/Surface.js'
import { Select } from '../../../ui/Select.js'
import { TextInput } from '../../../ui/TextInput.js'
import { Spinner } from '../../../ui/Spinner.js'
import { theme } from '../../../ui/theme.js'
import {
normalizeEthDomain,
sanitizeSubdomainPrefix,
} from '../../ens/ensLookup.js'
import { isRootEthName } from '../../ens/ensAutomation.js'
import type { Erc8004RegistryConfig } from '../../registry/erc8004.js'
import {
type CustodyMode,
} from '../custody/state.js'
import { shortAddress } from '../shared/model/format.js'
import {
footerHint,
} from './EnsEditShared.js'
import {
EnsSetupBlockedScreen,
EnsSetupReviewScreen,
} from './EnsEditReviewScreens.js'
import { EscCancel } from './EnsEditRunners.js'
import type {
EnsEditProps,
EnsPhase,
} from './types.js'
import type { AgentReconciliation } from '../shared/reconciliation/index.js'
type AdvancedScreenProps = {
phase: EnsPhase
identity: EnsEditProps['identity']
ownerAddress: Address
agentId: EnsEditProps['identity']['agentId']
reconciliation: AgentReconciliation
savedSubdomainLabel: string
agentNameSuggestion: string
currentEnsName: string
savedCustodyMode: CustodyMode | undefined
registry: Erc8004RegistryConfig
setPhase: (phase: EnsPhase) => void
runDiscovery: (mode?: 'simple' | 'advanced') => void
runAdvancedSubdomainCheck: (rootName: string, label: string) => void
onEnsSetup: EnsEditProps['onEnsSetup']
onEnsLink: EnsEditProps['onEnsLink']
}
export function renderAdvancedEnsPhase({
phase,
identity,
ownerAddress,
agentId,
reconciliation,
savedSubdomainLabel,
agentNameSuggestion,
currentEnsName,
savedCustodyMode,
registry,
setPhase,
runDiscovery,
runAdvancedSubdomainCheck,
onEnsSetup,
onEnsLink,
}: AdvancedScreenProps): React.ReactNode | null {
if (phase.kind === 'advanced-root-check') {
return (
Reading from Ethereum mainnet...
)
}
if (phase.kind === 'advanced-subdomain') {
const rootName = phase.rootName
return (
{phase.error ? (
{phase.error}
) : null}
{
const trimmed = value.trim()
const label = sanitizeSubdomainPrefix(trimmed)
if (!label) return 'Enter a subdomain label'
if (trimmed.includes('.')) return 'Enter only the subdomain label'
if (label !== trimmed.toLowerCase()) return 'Use lowercase letters, numbers, and hyphens only'
return null
}}
onSubmit={value => runAdvancedSubdomainCheck(rootName, sanitizeSubdomainPrefix(value))}
onCancel={() => setPhase({ kind: 'pick-parent', mode: 'advanced' })}
/>
)
}
if (phase.kind === 'advanced-subdomain-check') {
return (
setPhase({ kind: 'advanced-subdomain', rootName: phase.rootName, label: phase.label })} />
)
}
if (phase.kind === 'advanced-review') {
return (
{
if (phase.setup.txCount > 0) {
onEnsSetup(phase.setup)
return
}
onEnsLink(phase.setup.fullName, {
mode: 'advanced',
ownerAddress: phase.setup.ownerAddress,
})
}}
onBack={() => setPhase({ kind: 'advanced-subdomain', rootName: phase.setup.rootName, label: phase.setup.label })}
/>
)
}
if (phase.kind === 'advanced-manual') {
return (
runAdvancedSubdomainCheck(phase.fallback.rootName, phase.fallback.label)}
onBack={() => setPhase({ kind: 'advanced-subdomain', rootName: phase.fallback.rootName, label: phase.fallback.label })}
/>
)
}
return null
}