%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

{mapping, "trust_store.providers.$name", "rabbitmq_trust_store.providers", [
    {datatype, atom}
]}.

{translation, "rabbitmq_trust_store.providers",
fun(Conf) ->
    Settings = cuttlefish_variable:filter_by_prefix("trust_store.providers", Conf),
    ProviderModule = fun
        (file)      -> rabbit_trust_store_file_provider;
        (http)      -> rabbit_trust_store_http_provider;
        (Other) when is_atom(Other) -> Other;
        (_) -> cuttlefish:invalid("Unknown/unsupported trust store provider")
    end,

    Settings = cuttlefish_variable:filter_by_prefix("trust_store.providers", Conf),
    [ ProviderModule(V) || {_, V} <- Settings ]
end}.


{mapping, "trust_store.refresh_interval", "rabbitmq_trust_store.refresh_interval", [
    {datatype, integer}
]}.

{mapping, "trust_store.directory", "rabbitmq_trust_store.directory", [
    {datatype, string}
]}.

{mapping, "trust_store.url", "rabbitmq_trust_store.url", [
    {datatype, string}
]}.

{mapping, "trust_store.https_request_timeout", "rabbitmq_trust_store.https_request_timeout", [
    {datatype, {integer, non_negative}}
]}.

{translation, "rabbitmq_trust_store.https_request_timeout",
fun(Conf) ->
    case cuttlefish:conf_get("trust_store.https_request_timeout", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

{include_partial, {rabbit, "ssl_options"},
    [{prefix, "trust_store.ssl_options"},
     {app_prefix, "rabbitmq_trust_store.ssl_options"},
     {disable_with, none}]}.

%% `key.*` is kept inline so the no-match branch stays the atom
%% `undefined`, matching the historical (and buggy) translation shape.

{mapping, "trust_store.ssl_options.key.RSAPrivateKey",
    "rabbitmq_trust_store.ssl_options.key",
    [{datatype, string}]}.

{mapping, "trust_store.ssl_options.key.DSAPrivateKey",
    "rabbitmq_trust_store.ssl_options.key",
    [{datatype, string}]}.

{mapping, "trust_store.ssl_options.key.PrivateKeyInfo",
    "rabbitmq_trust_store.ssl_options.key",
    [{datatype, string}]}.

{translation, "rabbitmq_trust_store.ssl_options.key",
fun(Conf) ->
    case cuttlefish_variable:filter_by_prefix(
           "trust_store.ssl_options.key", Conf) of
        [{[_,_,Key], Val}|_] -> {list_to_atom(Key), list_to_binary(Val)};
        _ -> undefined
    end
end}.

%% trust_store additions on top of the shared partial.

{mapping, "trust_store.ssl_options.client_renegotiation",
    "rabbitmq_trust_store.ssl_options.client_renegotiation",
    [{datatype, boolean}]}.

{mapping, "trust_store.ssl_options.honor_cipher_order",
    "rabbitmq_trust_store.ssl_options.honor_cipher_order",
    [{datatype, boolean}]}.
