// spline_edit.cpp — see spline_edit.h. Pure decision logic; no host types.

#include "core/instrument/ui/spline_edit.h"

namespace reasampler::instrument::ui {

SplineEdit resolveSplineEdit(const VelocityCurve& curve, const VelocityCurve::Box& box,
                             SplineGesture gesture, int x, int y) {
    if (box.width <= 0 || box.height <= 1) return {};
    const int idx = curve.pointAtPixel(box, x, y);
    switch (gesture) {
        case SplineGesture::kRight:
            return idx >= 0 ? SplineEdit{SplineEditKind::kDelete, idx} : SplineEdit{};
        case SplineGesture::kControlLeft:
            return idx >= 0 ? SplineEdit{SplineEditKind::kToggleHard, idx} : SplineEdit{};
        case SplineGesture::kLeft:
            break;
    }
    if (idx >= 0) return {SplineEditKind::kGrab, idx};
    const bool inBox = (x >= box.left && x < box.left + box.width && y >= box.top &&
                        y < box.top + box.height);
    return inBox ? SplineEdit{SplineEditKind::kAdd, -1} : SplineEdit{};
}

VelocityCurve::Box splineOverlayBox(const OverlayArea& area) {
    return VelocityCurve::Box{area.rect.x, area.rect.y, area.rect.width, area.rect.height};
}

WaveformClaimant resolveWaveformClaim(const WaveformClaim& node, const WaveformClaim& tab,
                                      const WaveformClaim& marker, SplineGesture gesture) {
    if (gesture == SplineGesture::kControlLeft && node.hit) return WaveformClaimant::kNode;
    if (node.hit && (!tab.hit || node.area <= tab.area) && (!marker.hit || node.area <= marker.area))
        return WaveformClaimant::kNode;
    if (tab.hit && (!marker.hit || tab.area <= marker.area)) return WaveformClaimant::kTab;
    if (marker.hit) return WaveformClaimant::kMarker;
    return WaveformClaimant::kNone;
}

}  // namespace reasampler::instrument::ui
