module DEBUGGER__::UI_DAP

def dap_setup bytes

def dap_setup bytes
  CONFIG.set_config no_color: true
  @seq = 0
  @send_lock = Mutex.new
  case self
  when UI_UnixDomainServer
    # If the user specified a mapping, respect it, otherwise, make sure that no mapping is used
    UI_DAP.local_fs_map_set CONFIG[:local_fs_map] || true
  when UI_TcpServer
    # TODO: loopback address can be used to connect other FS env, like Docker containers
    # UI_DAP.local_fs_set if @local_addr.ipv4_loopback? || @local_addr.ipv6_loopback?
  end
  show_protocol :>, bytes
  req = JSON.load(bytes)
  # capability
  send_response(req,
         ## Supported
         supportsConfigurationDoneRequest: true,
         supportsFunctionBreakpoints: true,
         supportsConditionalBreakpoints: true,
         supportTerminateDebuggee: true,
         supportsTerminateRequest: true,
         exceptionBreakpointFilters: [
           {
             filter: 'any',
             label: 'rescue any exception',
             supportsCondition: true,
             #conditionDescription: '',
           },
           {
             filter: 'RuntimeError',
             label: 'rescue RuntimeError',
             supportsCondition: true,
             #conditionDescription: '',
           },
         ],
         supportsExceptionFilterOptions: true,
         supportsStepBack: true,
         supportsEvaluateForHovers: true,
         supportsCompletionsRequest: true,
         ## Will be supported
         # supportsExceptionOptions: true,
         # supportsHitConditionalBreakpoints:
         # supportsSetVariable: true,
         # supportSuspendDebuggee:
         # supportsLogPoints:
         # supportsLoadedSourcesRequest:
         # supportsDataBreakpoints:
         # supportsBreakpointLocationsRequest:
         ## Possible?
         # supportsRestartFrame:
         # completionTriggerCharacters:
         # supportsModulesRequest:
         # additionalModuleColumns:
         # supportedChecksumAlgorithms:
         # supportsRestartRequest:
         # supportsValueFormattingOptions:
         # supportsExceptionInfoRequest:
         # supportsDelayedStackTraceLoading:
         # supportsTerminateThreadsRequest:
         # supportsSetExpression:
         # supportsClipboardContext:
         ## Never
         # supportsGotoTargetsRequest:
         # supportsStepInTargetsRequest:
         # supportsReadMemoryRequest:
         # supportsDisassembleRequest:
         # supportsCancelRequest:
         # supportsSteppingGranularity:
         # supportsInstructionBreakpoints:
  )
  send_event 'initialized'
  puts <<~WELCOME
    Ruby REPL: You can run any Ruby expression here.
    Note that output to the STDOUT/ERR printed on the TERMINAL.
    [experimental]
      `,COMMAND` runs `COMMAND` debug command (ex: `,info`).
      `,help` to list all debug commands.
  WELCOME
end