'use strict';

const sharp = require('sharp');
const includes = require('array-includes');

module.exports = transformer;

function transformer(options) {
  let imageStream = sharp();
  const objectHasOwnProperty = Object.prototype.hasOwnProperty;
  const {
    resize,
    size,
    background,
    crop,
    embed,
    max,
    min,
    withoutEnlargement,
    ignoreAspectRatio,
    extract,
    trim,
    flatten,
    extend,
    negate,
    rotate,
    flip,
    flop,
    blur,
    sharpen,
    gamma,
    grayscale,
    greyscale,
    normalize,
    normalise,
    convolve,
    threshold,
    toColourspace,
    toColorspace,
    withMetadata,
    format
  } = options;

  const hasTypeAndOption = typeof format === 'object' &&
    objectHasOwnProperty.call(format, 'type') &&
    objectHasOwnProperty.call(format, 'option');

  if (resize && size) {
    imageStream = imageStream.resize(size.width, size.height, size.option);
  }

  if (background) {
    imageStream = imageStream.background(background);
  }

  if (crop && objectHasOwnProperty.call(sharp.gravity, crop)) {
    imageStream = imageStream.crop(sharp.gravity[crop]);
  }

  if (embed) {
    imageStream = imageStream.embed();
  }

  if (max) {
    imageStream = imageStream.max();
  }

  if (min) {
    imageStream = imageStream.min();
  }

  if (withoutEnlargement) {
    imageStream = imageStream.withoutEnlargement();
  }

  if (ignoreAspectRatio) {
    imageStream = imageStream.ignoreAspectRatio();
  }

  if (extract) {
    imageStream = imageStream.extract(extract);
  }

  if (trim) {
    imageStream = imageStream.trim(parseInt(trim, 10));
  }

  if (flatten) {
    imageStream = imageStream.flatten();
  }

  if (extend) {
    imageStream = imageStream.extend(extend);
  }

  if (negate) {
    imageStream = imageStream.negate();
  }

  if (includes([0, 90, 180, 270], rotate)) {
    imageStream = imageStream.rotate(rotate);
  }

  if (flip) {
    imageStream = imageStream.flip();
  }

  if (flop) {
    imageStream = imageStream.flop();
  }

  if (blur) {
    imageStream = imageStream.blur(blur);
  }

  if (sharpen) {
    imageStream = imageStream.sharpen(sharpen);
  }

  if (gamma) {
    imageStream = imageStream.gamma(gamma);
  }

  if (grayscale || greyscale) {
    imageStream = imageStream.greyscale();
  }

  if (normalize || normalise) {
    imageStream = imageStream.normalise();
  }

  if (convolve) {
    imageStream = imageStream.convolve(convolve);
  }

  if (threshold) {
    imageStream = imageStream.threshold(threshold);
  }

  if (toColourspace || toColorspace) {
    imageStream = imageStream.toColourspace(toColourspace || toColorspace);
  }

  if (withMetadata) {
    imageStream = imageStream.withMetadata(withMetadata);
  }

  if (format) {
    if (hasTypeAndOption) {
      imageStream = imageStream.toFormat(format.type, format.option);
    } else {
      imageStream = imageStream.toFormat(format);
    }
  }

  return imageStream;
}

@@ -141,133 +141,17 @@
 mer(
-options) %7B%0A  let imageStream = sharp();%0A  const objectHasOwnProperty = Object.prototype.hasOwnProperty;%0A  const %7B%0A   
+%7B
  resize,
 %0A   
@@ -150,22 +150,14 @@
 ize,
-%0A   
  size,
-%0A   
  bac
@@ -168,97 +168,40 @@
 und,
-%0A   
  crop,
-%0A   
  embed,
-%0A   
  max,
-%0A   
  min,
-%0A    withoutEnlargement,%0A    ignoreAspectRatio,%0A  
+ format,%0A
   ex
@@ -210,22 +210,14 @@
 act,
-%0A   
  trim,
-%0A   
  fla
@@ -225,20 +225,16 @@
 ten,
-%0A   
  extend,
 %0A   
@@ -233,20 +233,16 @@
 end,
-%0A   
  negate,
 %0A   
@@ -241,20 +241,16 @@
 ate,
-%0A   
  rotate,
 %0A   
@@ -249,42 +249,26 @@
 ate,
-%0A   
  flip,
-%0A   
  flop,
-%0A   
  blur,
-%0A   
  sha
@@ -276,20 +276,16 @@
 pen,
-%0A   
  gamma,%0A
     
@@ -280,18 +280,16 @@
  gamma,%0A
-  
   graysc
@@ -292,20 +292,16 @@
 ayscale,
-%0A   
  greysca
@@ -303,20 +303,16 @@
 eyscale,
-%0A   
  normali
@@ -314,20 +314,16 @@
 rmalize,
-%0A   
  normali
@@ -325,20 +325,16 @@
 rmalise,
-%0A   
  convolv
@@ -335,20 +335,16 @@
 onvolve,
-%0A   
  thresho
@@ -346,20 +346,16 @@
 reshold,
-%0A   
  toColou
@@ -364,18 +364,16 @@
 pace,%0A  
- 
- 
 toColors
@@ -381,53 +381,158 @@
 ace,
-%0A    withMetadata,%0A    format%0A  %7D = options;%0A
+ ignoreAspectRatio, withMetadata, withoutEnlargement %7D) %7B%0A  let imageStream = sharp();%0A  const objectHasOwnProperty = Object.prototype.hasOwnProperty;
 %0A  c

refactor code transformer