import { c as _c } from 'react/compiler-runtime'
import * as React from 'react'
import { useEffect } from 'react'
import { useNotifications } from 'src/context/notifications.js'
import { getIsRemoteMode } from '../../bootstrap/state.js'
import { Text } from '../../ink.js'
import type { MCPServerConnection } from '../../services/mcp/types.js'

type Props = {
  mcpClients?: MCPServerConnection[]
}

const EMPTY_MCP_CLIENTS: MCPServerConnection[] = []

export function useMcpConnectivityStatus(t0: Props) {
  const $ = _c(4)
  const { mcpClients: t1 } = t0
  const mcpClients = t1 === undefined ? EMPTY_MCP_CLIENTS : t1
  const { addNotification } = useNotifications()
  let t2
  let t3
  if ($[0] !== addNotification || $[1] !== mcpClients) {
    t2 = () => {
      if (getIsRemoteMode()) {
        return
      }
      const failedLocalClients = mcpClients.filter(_tempFailed)
      const needsAuthLocalServers = mcpClients.filter(_tempNeedsAuth)
      if (
        failedLocalClients.length === 0 &&
        needsAuthLocalServers.length === 0
      ) {
        return
      }
      if (failedLocalClients.length > 0) {
        addNotification({
          key: 'mcp-failed',
          jsx: (
            <>
              <Text color="error">
                {failedLocalClients.length} MCP{' '}
                {failedLocalClients.length === 1 ? 'server' : 'servers'} failed
              </Text>
              <Text dimColor={true}> · /mcp</Text>
            </>
          ),
          priority: 'medium',
        })
      }
      if (needsAuthLocalServers.length > 0) {
        addNotification({
          key: 'mcp-needs-auth',
          jsx: (
            <>
              <Text color="warning">
                {needsAuthLocalServers.length} MCP{' '}
                {needsAuthLocalServers.length === 1
                  ? 'server needs'
                  : 'servers need'}{' '}
                auth
              </Text>
              <Text dimColor={true}> · /mcp</Text>
            </>
          ),
          priority: 'medium',
        })
      }
    }
    t3 = [addNotification, mcpClients]
    $[0] = addNotification
    $[1] = mcpClients
    $[2] = t2
    $[3] = t3
  } else {
    t2 = $[2]
    t3 = $[3]
  }
  useEffect(t2, t3)
}

function _tempNeedsAuth(client: MCPServerConnection) {
  return client.type === 'needs-auth'
}

function _tempFailed(client: MCPServerConnection) {
  return (
    client.type === 'failed' &&
    client.config.type !== 'sse-ide' &&
    client.config.type !== 'ws-ide'
  )
}
