/**
 * Telemetry for plugin/marketplace fetches - DISABLED.
 */

export type PluginFetchSource =
  | 'install_counts'
  | 'marketplace_clone'
  | 'marketplace_pull'
  | 'marketplace_url'
  | 'plugin_clone'
  | 'mcpb'

export type PluginFetchOutcome = 'success' | 'failure' | 'cache_hit'

/**
 * Extract hostname from a URL or git spec - DISABLED.
 */
function extractHost(_urlOrSpec: string): string {
  return 'unknown'
}

/**
 * True if the URL/spec points at anthropics/claude-plugins-official - DISABLED.
 */
function isOfficialRepo(_urlOrSpec: string): boolean {
  return false
}

export function logPluginFetch(
  _source: PluginFetchSource,
  _urlOrSpec: string | undefined,
  _outcome: PluginFetchOutcome,
  _durationMs: number,
  _errorKind?: string,
): void {
  // Telemetry disabled
}

/**
 * Classify an error into a stable bucket for the error_kind field.
 */
export function classifyFetchError(error: unknown): string {
  const msg = String((error as { message?: unknown })?.message ?? error)
  if (
    /ENOTFOUND|ECONNREFUSED|EAI_AGAIN|Could not resolve host|Connection refused/i.test(
      msg,
    )
  ) {
    return 'dns_or_refused'
  }
  if (/ETIMEDOUT|timed out|timeout/i.test(msg)) return 'timeout'
  if (
    /ECONNRESET|socket hang up|Connection reset by peer|remote end hung up/i.test(
      msg,
    )
  ) {
    return 'conn_reset'
  }
  if (/403|401|authentication|permission denied/i.test(msg)) return 'auth'
  if (/404|not found|repository not found/i.test(msg)) return 'not_found'
  if (/certificate|SSL|TLS|unable to get local issuer/i.test(msg)) return 'tls'
  if (/Invalid response format|Invalid marketplace schema/i.test(msg)) {
    return 'invalid_schema'
  }
  return 'other'
}
