import { maxBy } from './array.js';
export function findAppropriateThumbnail(images, wantedWidth, ratio) {
    if (!images)
        return null;
    return findAppropriateImage(images.filter(img => img.aspectRatio === ratio), wantedWidth);
}
export function findAppropriateImage(images, wantedWidth) {
    if (!wantedWidth)
        throw new Error('Invalid width to find appropriate image');
    if (!images || images.length === 0)
        return undefined;
    let candidate;
    for (const img of images) {
        if (img.width >= wantedWidth && (!candidate || img.width < candidate.width)) {
            candidate = img;
        }
    }
    return candidate || maxBy(images, 'width');
}
export function guessAspectRatio(width, height) {
    const ratio = width / height;
    if (ratio >= 1.77)
        return '16:9';
    if (ratio >= 1.6)
        return '16:10';
    if (ratio >= 1.33)
        return '4:3';
    if (ratio >= 1.25)
        return '5:4';
    return '1:1';
}
//# sourceMappingURL=image.js.map