// Code generated by ogen, DO NOT EDIT.

package api

import (
	"context"
	"net/http"
	"strings"

	"github.com/go-faster/errors"

	"github.com/ogen-go/ogen/ogenerrors"
)

// SecurityHandler is handler for security parameters.
type SecurityHandler interface {
	// HandleHeaderApiKey handles HeaderApiKey security.
	HandleHeaderApiKey(ctx context.Context, operationName OperationName, t HeaderApiKey) (context.Context, error)
}

func findAuthorization(h http.Header, prefix string) (string, bool) {
	v, ok := h["Authorization"]
	if !ok {
		return "", false
	}
	for _, vv := range v {
		scheme, value, ok := strings.Cut(vv, " ")
		if !ok || !strings.EqualFold(scheme, prefix) {
			continue
		}
		return value, true
	}
	return "", false
}

var operationRolesHeaderApiKey = map[string][]string{
	APIV2BufferGoodsTaskGetOperation:           []string{},
	APIV2BufferTasksGetOperation:               []string{},
	APIV2HistoryGoodsTaskGetOperation:          []string{},
	APIV2HistoryTasksGetOperation:              []string{},
	APIV2ListGoodsFilterGetOperation:           []string{},
	APIV2ListGoodsSizeNmGetOperation:           []string{},
	APIV2QuarantineGoodsGetOperation:           []string{},
	APIV2UploadTaskClubDiscountPostOperation:   []string{},
	APIV2UploadTaskPostOperation:               []string{},
	APIV2UploadTaskSizePostOperation:           []string{},
	APIV3OfficesGetOperation:                   []string{},
	APIV3StocksWarehouseIdDeleteOperation:      []string{},
	APIV3StocksWarehouseIdPostOperation:        []string{},
	APIV3StocksWarehouseIdPutOperation:         []string{},
	APIV3WarehousesGetOperation:                []string{},
	APIV3WarehousesPostOperation:               []string{},
	APIV3WarehousesWarehouseIdDeleteOperation:  []string{},
	APIV3WarehousesWarehouseIdPutOperation:     []string{},
	ContentV2BarcodesPostOperation:             []string{},
	ContentV2CardsDeleteTrashPostOperation:     []string{},
	ContentV2CardsErrorListGetOperation:        []string{},
	ContentV2CardsLimitsGetOperation:           []string{},
	ContentV2CardsRecoverPostOperation:         []string{},
	ContentV2CardsUpdatePostOperation:          []string{},
	ContentV2CardsUploadAddPostOperation:       []string{},
	ContentV2CardsUploadPostOperation:          []string{},
	ContentV2DirectoryColorsGetOperation:       []string{},
	ContentV2DirectoryCountriesGetOperation:    []string{},
	ContentV2DirectoryKindsGetOperation:        []string{},
	ContentV2DirectorySeasonsGetOperation:      []string{},
	ContentV2DirectoryTnvedGetOperation:        []string{},
	ContentV2DirectoryVatGetOperation:          []string{},
	ContentV2GetCardsListPostOperation:         []string{},
	ContentV2GetCardsTrashPostOperation:        []string{},
	ContentV2ObjectAllGetOperation:             []string{},
	ContentV2ObjectCharcsSubjectIdGetOperation: []string{},
	ContentV2ObjectParentAllGetOperation:       []string{},
	ContentV2TagNomenclatureLinkPostOperation:  []string{},
	ContentV2TagsGetOperation:                  []string{},
	ContentV3MediaFilePostOperation:            []string{},
	ContentV3MediaSavePostOperation:            []string{},
}

func (s *Server) securityHeaderApiKey(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
	var t HeaderApiKey
	const parameterName = "Authorization"
	value := req.Header.Get(parameterName)
	if value == "" {
		return ctx, false, nil
	}
	t.APIKey = value
	t.Roles = operationRolesHeaderApiKey[operationName]
	rctx, err := s.sec.HandleHeaderApiKey(ctx, operationName, t)
	if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
		return nil, false, nil
	} else if err != nil {
		return nil, false, err
	}
	return rctx, true, err
}

// SecuritySource is provider of security values (tokens, passwords, etc.).
type SecuritySource interface {
	// HeaderApiKey provides HeaderApiKey security value.
	HeaderApiKey(ctx context.Context, operationName OperationName, client *Client) (HeaderApiKey, error)
}

func (s *Client) securityHeaderApiKey(ctx context.Context, operationName OperationName, req *http.Request) error {
	t, err := s.sec.HeaderApiKey(ctx, operationName, s)
	if err != nil {
		return errors.Wrap(err, "security source \"HeaderApiKey\"")
	}
	req.Header.Set("Authorization", t.APIKey)
	return nil
}
