// overflow_menu — pure implementation. See overflow_menu.h.

#include "core/ui/overflow_menu.h"

namespace reasampler::ui {

int menuButtonReserve(const MenuBarRect& bar, const MenuButtonSpec& spec) {
    if (bar.width <= 0 || bar.height <= 0 || spec.buttonWidth <= 0) return 0;
    // Button width plus a right gap and a matching left gap for breathing room.
    return spec.buttonWidth + 2 * spec.rightInset;
}

MenuButtonRect computeMenuButton(const MenuBarRect& bar, const MenuButtonSpec& spec) {
    MenuButtonRect btn;
    if (bar.width <= 0 || bar.height <= 0 || spec.buttonWidth <= 0) return btn;

    const int right = bar.x + bar.width - spec.rightInset;
    const int left = right - spec.buttonWidth;
    if (left < bar.x + spec.minLeftInset) return btn;  // too narrow — suppress

    int top = bar.y + spec.verticalInset;
    int height = bar.height - 2 * spec.verticalInset;
    if (height <= 0) {
        top = bar.y;
        height = bar.height;
    }

    btn.x = left;
    btn.y = top;
    btn.width = spec.buttonWidth;
    btn.height = height;
    return btn;
}

bool hitTestMenuButton(int px, int py, const MenuButtonRect& button) {
    if (button.empty()) return false;
    return px >= button.x && px < button.x + button.width &&
           py >= button.y && py < button.y + button.height;
}

} // namespace reasampler::ui
