// ../../adapters/sharp/src/sharp-encoder.ts
import sharp from "sharp";
function createSharpEncoder() {
  return {
    async encode(input, request) {
      const buffer = Buffer.from(input);
      const metadata = await sharp(buffer).metadata();
      const sourceWidth = metadata.width ?? 0;
      const sourceHeight = metadata.height ?? 0;
      const widths = [.../* @__PURE__ */ new Set([...request.widths, sourceWidth])].filter((width) => width > 0 && width <= sourceWidth).sort((a, b) => a - b);
      const variants = [];
      for (const format of request.formats) {
        for (const width of widths) {
          const bytes = await encodeOne(buffer, width, format);
          variants.push({ format, width, bytes });
        }
      }
      const blur = await sharp(buffer).resize({ width: 16 }).webp({ quality: 40 }).toBuffer();
      const blurDataUri = `data:image/webp;base64,${blur.toString("base64")}`;
      return { sourceWidth, sourceHeight, variants, blurDataUri };
    }
  };
}
function encodeOne(buffer, width, format) {
  const pipeline = sharp(buffer).resize({ width, withoutEnlargement: true });
  switch (format) {
    case "avif":
      return pipeline.avif({ quality: 50 }).toBuffer();
    case "webp":
      return pipeline.webp({ quality: 75 }).toBuffer();
    case "png":
      return pipeline.png().toBuffer();
    case "jpeg":
      return pipeline.jpeg({ quality: 78, mozjpeg: true }).toBuffer();
  }
}
export {
  createSharpEncoder
};
