/**
 * ABAP template: `<cls>.clas.locals_def.abap`.
 *
 * Declares four local classes bundled inside the generated implementation
 * class. The locals wrap kernel APIs only — they do NOT reference any
 * Z-prefixed objects.
 *
 * Design note on the exception class name:
 *   `lcl_http->fetch` is intentionally generic and propagates only kernel
 *   exceptions (`cx_web_http_client_error`, `cx_http_dest_provider_error`).
 *   Mapping HTTP status codes to the generated ZCX is the responsibility of
 *   the per-operation method, not the local HTTP class. The `exceptionClass`
 *   name is accepted in the signature for symmetry with Wave 2, but is
 *   currently not referenced in the emitted source.
 */
export interface LocalClassesNames {
  exceptionClass: string;
  localJsonClass: string;
  localJsonParserClass: string;
  localResponseClass: string;
  localHttpClass: string;
}

export function buildLocalsDef(names: LocalClassesNames): string {
  const {
    localJsonClass: JSON_CLS,
    localJsonParserClass: JSON_PARSER,
    localResponseClass: RESPONSE,
    localHttpClass: HTTP,
  } = names;

  // The exceptionClass name is intentionally NOT rendered into the source.
  // Per-operation methods (generated by Wave 2) are responsible for mapping
  // HTTP status codes to the custom exception class; these locals only
  // propagate kernel exceptions.
  void names.exceptionClass;

  return `* Generated by @abapify/openai-codegen - local class declarations.
* ZERO Z-dependencies: these locals wrap SAP kernel APIs only.
* Per-operation methods map HTTP status codes to the custom exception.

CLASS ${HTTP} DEFINITION DEFERRED.
CLASS ${RESPONSE} DEFINITION DEFERRED.
CLASS ${JSON_PARSER} DEFINITION DEFERRED.
CLASS ${JSON_CLS} DEFINITION DEFERRED.

CLASS ${HTTP} DEFINITION FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    TYPES: BEGIN OF kv,
             name  TYPE string,
             value TYPE string,
           END OF kv.
    TYPES kvs TYPE STANDARD TABLE OF kv WITH DEFAULT KEY.

    METHODS constructor
      IMPORTING destination TYPE string
                server      TYPE string OPTIONAL.

    METHODS fetch
      IMPORTING method          TYPE string
                path            TYPE string
                query           TYPE kvs     OPTIONAL
                headers         TYPE kvs     OPTIONAL
                body            TYPE string  OPTIONAL
                binary          TYPE xstring OPTIONAL
      RETURNING VALUE(response) TYPE REF TO ${RESPONSE}
      RAISING   cx_web_http_client_error
                cx_http_dest_provider_error.

  PRIVATE SECTION.
    DATA destination TYPE string.
    DATA server      TYPE string.

    METHODS build_query_string
      IMPORTING query         TYPE kvs
      RETURNING VALUE(result) TYPE string.
ENDCLASS.

CLASS ${RESPONSE} DEFINITION FINAL CREATE PRIVATE FRIENDS ${HTTP}.
  PUBLIC SECTION.
    METHODS status  RETURNING VALUE(result) TYPE i.
    METHODS body    RETURNING VALUE(result) TYPE xstring.
    METHODS text    RETURNING VALUE(result) TYPE string.
    METHODS header
      IMPORTING name          TYPE string
      RETURNING VALUE(result) TYPE string.
    METHODS headers RETURNING VALUE(result) TYPE ${HTTP}=>kvs.

  PRIVATE SECTION.
    METHODS constructor
      IMPORTING status_code TYPE i
                body_bytes  TYPE xstring
                body_text   TYPE string
                header_list TYPE ${HTTP}=>kvs.

    DATA status_code TYPE i.
    DATA body_bytes  TYPE xstring.
    DATA body_text   TYPE string.
    DATA header_list TYPE ${HTTP}=>kvs.
ENDCLASS.

CLASS ${JSON_PARSER} DEFINITION FINAL CREATE PRIVATE FRIENDS ${JSON_CLS}.
  PUBLIC SECTION.
    METHODS to
      IMPORTING target TYPE REF TO data.

  PRIVATE SECTION.
    METHODS constructor
      IMPORTING json_text TYPE string.

    DATA json_text TYPE string.
ENDCLASS.

CLASS ${JSON_CLS} DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS stringify
      IMPORTING data          TYPE any
      RETURNING VALUE(result) TYPE string.

    CLASS-METHODS render
      IMPORTING data          TYPE any
      RETURNING VALUE(result) TYPE xstring.

    CLASS-METHODS parse
      IMPORTING source        TYPE any
      RETURNING VALUE(result) TYPE REF TO ${JSON_PARSER}.
ENDCLASS.
`;
}
