package main

func generateHandlersConsumer() {
	generateToFile(
		headerHandlersConsuming, templateFuncHandlersConsuming, "internal/api/consuming_gen.go",
		nil,
		[]string{
			"Publish", "Broadcast", "Subscribe", "Unsubscribe", "Disconnect", "HistoryRemove", "Refresh",
		},
	)
}

var headerHandlersConsuming = `// Code generated by internal/gen/api/main.go. DO NOT EDIT.

package api

import (
	"context"

	. "github.com/centrifugal/centrifugo/v6/internal/apiproto"

	"go.opentelemetry.io/otel/codes"
	"go.opentelemetry.io/otel/trace"
)
`

var templateFuncHandlersConsuming = `
func (h *ConsumingHandler) handle{{ .RequestCapitalized }}(ctx context.Context, data []byte) (*{{ .RequestCapitalized }}Result, error) {
	req, err := requestDecoder.Decode{{ .RequestCapitalized }}(data)
	if err != nil {
		return nil, ErrInvalidData
	}
	resp := h.api.{{ .RequestCapitalized }}(ctx, req)
	if h.config.UseOpenTelemetry && resp.Error != nil {
		span := trace.SpanFromContext(ctx)
		span.SetStatus(codes.Error, resp.Error.Error())
	}
	if resp.Error != nil {
		incError(h.api.config.Protocol, "{{ .RequestSnake }}", resp.Error.Code)
		return nil, resp.Error
	}
	return resp.Result, nil
}
`
