// insert_plan.cpp — see insert_plan.h. Pure InsertMedia mode-bit arithmetic.

#include "insert_plan.h"

namespace reasampler {

namespace {

// Base target bits (mode&3). We use only 0 (current track) and 1 (new track).
constexpr int kBaseCurrentTrack = 0;  // add to current track
constexpr int kBaseNewTrack     = 1;  // add new track

// Tempo-conform bits, verbatim from the header doc-comment.
constexpr int kMatchTempo1x   = 8;    // &8:  try to match tempo 1x
constexpr int kMatchTempoHalf = 16;   // &16: try to match tempo 0.5x
constexpr int kMatchTempoDbl  = 32;   // &32: try to match tempo 2x
constexpr int kDontPreservePitch = 64; // &64: don't preserve pitch when matching tempo

}  // namespace

int computeInsertMode(const InsertOptions& opts) {
    int mode = opts.target == InsertTarget::NewTrack ? kBaseNewTrack
                                                     : kBaseCurrentTrack;

    switch (opts.conform) {
        case TempoConform::None:
            // No tempo bits: native length, no stretch. (Also never &4.)
            return mode;
        case TempoConform::Ratio1x:
            mode |= kMatchTempo1x;
            break;
        case TempoConform::RatioHalf:
            mode |= kMatchTempoHalf;
            break;
        case TempoConform::RatioDouble:
            mode |= kMatchTempoDbl;
            break;
    }

    // Tempo bits are set (conform != None). Add the pitch-shift bit only when the
    // caller asked NOT to preserve pitch. When conform == None we already returned
    // above, so this can never fire without a tempo bit present.
    if (!opts.preservePitch)
        mode |= kDontPreservePitch;

    return mode;
}

} // namespace reasampler
