'use strict';

var index = require('./index-efca863c.js');
require('./_commonjsHelpers-06173234.js');

// Sanity image auto formating

function urlFor(_ref) {
  var client = _ref.client,
      source = _ref.source,
      _ref$imageWidth = _ref.imageWidth,
      imageWidth = _ref$imageWidth === undefined ? null : _ref$imageWidth,
      _ref$imageHeight = _ref.imageHeight,
      imageHeight = _ref$imageHeight === undefined ? null : _ref$imageHeight,
      _ref$imageMaxHeight = _ref.imageMaxHeight,
      imageMaxHeight = _ref$imageMaxHeight === undefined ? null : _ref$imageMaxHeight,
      _ref$imageFit = _ref.imageFit,
      imageFit = _ref$imageFit === undefined ? 'crop' : _ref$imageFit;


  // when imageFit value = null and it's specifically provided, it's value not gets defaulted to 'crop'
  if (!imageFit) {
    imageFit = 'crop';
  }

  //checks for old usage so it wont break view pages
  //All view pages are using  urlFor(client, thumbnail.asset).url()
  //will need to update to urlFor({client, source: thumbnail.asset})
  //All framework components are using new format
  if (arguments[1] && arguments[1]._ref) {
    return index.imageUrlBuilder(client).image(source).auto('format');
  }

  if (imageWidth && imageHeight) {
    imageMaxHeight = imageHeight;
    imageHeight = null;
  }

  var builder = index.imageUrlBuilder(client);
  var url = builder.image(source);
  if (imageWidth) url = url.width(imageWidth);
  if (imageHeight) url = url.height(imageHeight);
  if (imageMaxHeight) url = url.maxHeight(imageMaxHeight);
  if (imageFit) url = url.fit(imageFit);
  return url.auto('format').url();
}

module.exports = urlFor;
