// lane_keys implementation — pure string convention, no REAPER. See lane_keys.h.

#include "lane_keys.h"

#include <cstring>

namespace reasampler {

namespace {
// Does `s` start with the managed-lane prefix?
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;  // manual/unnamed ⇒ no key
    // The durable name IS the key (stable across ordinal renumber). Keeping the full
    // prefixed name — rather than stripping to the mode id — means the key is globally
    // unambiguous and the ownership index's mode field remains the single 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;  // manual/unnamed ⇒ no mode
    const std::size_t n = std::strlen(kManagedLanePrefix);
    if (laneName.size() == n) return std::nullopt;  // prefix only, no mode suffix (illegal)
    return laneName.substr(n);
}

bool isOnManualLane(bool isFixedLaneTrack, const std::string& laneName) {
    // On a normal (non-fixed-lane) track there is no concept of a manual lane; the
    // item follows the normal auto-tag rule.
    if (!isFixedLaneTrack) return false;
    // On a fixed-lane track: a managed lane (prefixed) is NOT manual; everything else
    // — including the empty/unnamed lane that REAPER creates by default — IS manual
    // (user-minted, off-limits to auto-tag and to the lane-drive path).
    return !hasManagedPrefix(laneName);
}

}  // namespace reasampler
