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 { Spinner } from '../../../ui/Spinner.js'
import { theme } from '../../../ui/theme.js'
import type { BrowserWalletReady } from '../../wallet/browserWallet.js'
import {
type CustodyMode,
} from '../custody/state.js'
import { shortAddress } from '../shared/model/format.js'
import {
emptyAgentEnsRecords,
recordsHaveCurrentValues,
unlinkEnsLinkOptions,
} from './editCopy.js'
import {
EnsSetupRow,
footerHint,
} from './EnsEditShared.js'
import { UnlinkEnsReviewScreen } from './EnsEditReviewScreens.js'
import {
DeleteSubdomainTxRunner,
EscCancel,
} from './EnsEditRunners.js'
import type {
EnsEditProps,
EnsPhase,
} from './types.js'
type MaintenanceScreenProps = {
phase: EnsPhase
identity: EnsEditProps['identity']
currentEnsName: string
currentEnsCanDelete: boolean
savedCustodyMode: CustodyMode | undefined
savedOwnerAddress: string
savedOperator: string
registryNetworkLabel: string
validationError: string | null
ownerAddress: Address
operatorWalletSession: BrowserWalletReady | null
setOperatorWalletSession: (session: BrowserWalletReady | null) => void
setPhase: (phase: EnsPhase) => void
runDiscovery: () => void
runUnlinkEnsLoading: (fullName: string) => void
runDeleteSubdomainPreflight: (fullName: string) => void
onBack: () => void
onEnsUnlink: EnsEditProps['onEnsUnlink']
onEnsRecordsUpdate: EnsEditProps['onEnsRecordsUpdate']
}
export function renderEnsMaintenancePhase({
phase,
identity,
currentEnsName,
currentEnsCanDelete,
savedCustodyMode,
savedOwnerAddress,
savedOperator,
registryNetworkLabel,
validationError,
ownerAddress,
operatorWalletSession,
setOperatorWalletSession,
setPhase,
runDiscovery,
runUnlinkEnsLoading,
runDeleteSubdomainPreflight,
onBack,
onEnsUnlink,
onEnsRecordsUpdate,
}: MaintenanceScreenProps): React.ReactNode | null {
if (phase.kind === 'mode-select') {
type EnsAction = 'link' | 'unlink' | 'back'
const isAdvanced = savedCustodyMode === 'advanced'
const multiNeedsCustodySetup = isAdvanced && !savedOwnerAddress
const subtitle = currentEnsName
? `This agent resolves at ${currentEnsName}.`
: 'Assign a name so others can find this agent.'
const linkHint = multiNeedsCustodySetup
? 'Set Advanced custody first via Custody Mode'
: isAdvanced
? 'Root → Name → Apply'
: 'Root → Name → Apply'
const options: Array<{ value: EnsAction; role?: 'section' | 'utility'; label: string; hint?: string; disabled?: boolean }> = []
if (currentEnsName) {
options.push({ value: 'unlink', label: 'Unlink Name' })
} else {
options.push({
value: 'link',
label: 'Set Up Name',
hint: linkHint,
disabled: multiNeedsCustodySetup,
})
}
options.push({ value: 'back', label: 'Back', role: 'utility' })
return (
{validationError ? {validationError} : null}
)
}
if (phase.kind === 'unlink-loading') {
return (
setPhase({ kind: 'mode-select' })} />
)
}
if (phase.kind === 'unlink-review') {
const options = unlinkEnsLinkOptions(savedCustodyMode, savedOwnerAddress)
return (
{
if (recordsHaveCurrentValues(phase.recordsDiff)) {
onEnsRecordsUpdate(phase.fullName, emptyAgentEnsRecords(), options, true, phase.currentRecords)
return
}
onEnsUnlink()
}}
onBack={() => setPhase({ kind: 'mode-select' })}
/>
)
}
if (phase.kind === 'delete-subdomain-preflight') {
return (
setPhase({ kind: 'mode-select' })} />
)
}
if (phase.kind === 'delete-subdomain-blocked') {
return (
{phase.reason}
)
}
if (phase.kind === 'delete-subdomain-confirm') {
const plan = phase.plan
return (
)
}
if (phase.kind === 'delete-subdomain-tx') {
return (
{
onEnsUnlink()
setPhase({ kind: 'delete-subdomain-done', fullName: phase.plan.fullName })
}}
onError={msg => setPhase({ kind: 'delete-subdomain-blocked', fullName: phase.plan.fullName, reason: msg })}
/>
)
}
if (phase.kind === 'delete-subdomain-done') {
return (
)
}
return null
}