/*
 * Copyright (c) 2023, NVIDIA CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef COMMON_RAFT_METRIC_H
#define COMMON_RAFT_METRIC_H

#include <algorithm>
#include <string>
#include <unordered_map>

#include "fmt/format.h"
#include "knowhere/comp/index_param.h"
#include "knowhere/expected.h"
#include "raft/distance/distance_types.hpp"

namespace knowhere {

inline expected<raft::distance::DistanceType>
Str2RaftMetricType(std::string metric) {
    static const std::unordered_map<std::string, raft::distance::DistanceType> metric_map = {
        {metric::L2, raft::distance::DistanceType::L2Expanded},
        {metric::IP, raft::distance::DistanceType::InnerProduct},
        {metric::HAMMING, raft::distance::DistanceType::HammingUnexpanded},
        {metric::JACCARD, raft::distance::DistanceType::JaccardExpanded},
    };

    std::transform(metric.begin(), metric.end(), metric.begin(), toupper);
    auto it = metric_map.find(metric);
    if (it == metric_map.end())
        return expected<raft::distance::DistanceType>::Err(Status::invalid_metric_type,
                                                           fmt::format("unsupported metric type: {}", metric));
    return it->second;
}

}  // namespace knowhere

#endif /* RAFT_METRIC_H */
