#include <softbus_registry/PointTableEntry.h>

namespace softbus::registry {

namespace {

std::string replaceToken(std::string pattern, const std::string& token, const std::string& value)
{
    const auto pos = pattern.find(token);
    if (pos == std::string::npos) {
        return pattern;
    }
    pattern.replace(pos, token.size(), value);
    return pattern;
}

} // namespace

std::string TopicRules::formatData(const std::string& deviceId, const std::string& pointId) const
{
    std::string topic = data;
    topic = replaceToken(topic, "{deviceId}", deviceId);
    return replaceToken(topic, "{pointId}", pointId);
}

std::string TopicRules::formatCommand(const std::string& deviceId, const std::string& pointId) const
{
    std::string topic = command;
    topic = replaceToken(topic, "{deviceId}", deviceId);
    return replaceToken(topic, "{pointId}", pointId);
}

std::string TopicRules::formatAck(const std::string& deviceId, const std::string& pointId) const
{
    std::string topic = ack;
    topic = replaceToken(topic, "{deviceId}", deviceId);
    return replaceToken(topic, "{pointId}", pointId);
}

std::string TopicRules::formatHeartbeat(const std::string& deviceId) const
{
    return replaceToken(heartbeat, "{deviceId}", deviceId);
}

} // namespace softbus::registry
