/**
 * TypedFetch - Type System and Core Types
 */

import type { TypeDescriptor } from './type-descriptor.js'

// Advanced TypeScript utilities for runtime type inference
export type InferFromJSON<T> = T extends string
  ? string
  : T extends number
  ? number
  : T extends boolean
  ? boolean
  : T extends null
  ? null
  : T extends Array<infer U>
  ? Array<InferFromJSON<U>>
  : T extends Record<string, any>
  ? { [K in keyof T]: InferFromJSON<T[K]> }
  : unknown

export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}

// Runtime type storage for discovered APIs
export interface TypeRegistryEntry {
  request?: TypeDescriptor
  response?: TypeDescriptor
  method: string
  lastSeen: number
  samples: unknown[]
}

export interface TypeRegistry {
  [endpoint: string]: TypeRegistryEntry
}

// Enhanced error types
export interface TypedError extends Error {
  type: 'network' | 'http' | 'timeout' | 'circuit' | 'offline'
  status?: number
  retryable: boolean
  retryAfter?: number
  suggestions: string[]
  debug: () => void
}