import sharp from "sharp";

/**
 * Resizes and crops an image to the specified dimensions.
 *
 * @param {sharp.Sharp} img - The image to be resized and cropped.
 * @param {number} width - The desired width of the output image.
 * @param {number} height - The desired height of the output image.
 * @return {Promise<sharp.Sharp>} A promise that resolves to the resized and cropped image.
 */
const resizeAndCrop = async (
    img: sharp.Sharp,
    width: number,
    height: number
): Promise<sharp.Sharp> => {
    const buffer = await img
        .resize(width, height, {
            fit: "cover", // rellena todo el espacio, recorta si es necesario
            position: "centre", // centra el recorte (opcional, es el valor por defecto)
        })
        .toBuffer();

    return sharp(buffer);
};

export { resizeAndCrop };
