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

#include "core/capture/insert_plan.h"

namespace reasampler::capture {

namespace {

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

// Tempo-conform bits, verbatim from the header doc-comment.
constexpr int kMatchTempo1x   = 8;
constexpr int kMatchTempoHalf = 16;
constexpr int kMatchTempoDbl  = 32;
constexpr int kDontPreservePitch = 64;

}  // namespace

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

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

    // Reached only when a tempo bit is set (None already returned above).
    if (!opts.preservePitch)
        mode |= kDontPreservePitch;

    return mode;
}

} // namespace reasampler::capture
