import { Rectangle } from "pixi.js";

export function getIntersection(a, b, out = new Rectangle()) {
    const x1 = Math.max(a.x, b.x);
    const y1 = Math.max(a.y, b.y);
    const x2 = Math.min(a.x + a.width, b.x + b.width);
    const y2 = Math.min(a.y + a.height, b.y + b.height);

    if (x2 <= x1 || y2 <= y1) {
        // no overlap → empty rect
        out.x = 0;
        out.y = 0;
        out.width = 0;
        out.height = 0;
        return out;
    }

    out.x = x1;
    out.y = y1;
    out.width = x2 - x1;
    out.height = y2 - y1;

    return out;
}

export function getRandomInt(max) {
    return Math.floor(Math.random() * max);
}

export function scaleSpriteKeepRatio(sprite, newWidth) {
    const ratio = newWidth / sprite.width;
    sprite.height = sprite.height * ratio;
    sprite.width = newWidth;
}
