// See lane_keys.h.

#include "core/view/lane_keys.h"

#include <cstring>

namespace reasampler::view {

namespace {
bool hasManagedPrefix(const std::string& s) {
    const std::size_t n = std::strlen(kManagedLanePrefix);
    return s.size() >= n && s.compare(0, n, kManagedLanePrefix) == 0;
}
}  // namespace

bool isManagedLaneName(const std::string& laneName) {
    return hasManagedPrefix(laneName);
}

std::optional<std::string> managedLaneKey(const std::string& laneName) {
    if (!hasManagedPrefix(laneName)) return std::nullopt;
    // Keep the full prefixed name as the key (not just the mode id) so it stays
    // globally unambiguous; the ownership index's mode field is the sole
    // source of truth for which mode owns the lane.
    return laneName;
}

std::string laneNameForMode(const std::string& modeId) {
    return std::string(kManagedLanePrefix) + modeId;
}

std::optional<std::string> modeIdFromLaneName(const std::string& laneName) {
    if (!hasManagedPrefix(laneName)) return std::nullopt;
    const std::size_t n = std::strlen(kManagedLanePrefix);
    if (laneName.size() == n) return std::nullopt;  // prefix only, no mode suffix
    return laneName.substr(n);
}

bool isOnManualLane(bool isFixedLaneTrack, const std::string& laneName) {
    if (!isFixedLaneTrack) return false;
    return !hasManagedPrefix(laneName);
}

}  // namespace reasampler::view
