'use strict';

var UTIF = require('../../../utif2/UTIF.js');

const MIME_TYPE = "image/tiff";
var tiff = (() => ({
  mime: {
    [MIME_TYPE]: ["tiff", "tif"]
  },
  constants: {
    MIME_TIFF: MIME_TYPE
  },
  decoders: {
    [MIME_TYPE]: data => {
      const ifds = UTIF.decode(data);
      const page = ifds[0];
      ifds.forEach(ifd => {
        UTIF.decodeImage(data, ifd);
      });
      const rgba = UTIF.toRGBA8(page);
      return {
        data: Buffer.from(rgba),
        width: page.t256[0],
        height: page.t257[0]
      };
    }
  },
  encoders: {
    [MIME_TYPE]: image => {
      const tiff = UTIF.encodeImage(image.bitmap.data, image.bitmap.width, image.bitmap.height);
      return Buffer.from(tiff);
    }
  }
}));

module.exports = tiff;
