import { _ as _slicedToArray } from './slicedToArray-6ebcbfea.js';
import { i as imageUrlBuilder } from './index-3f5c03b2.js';
import './_commonjsHelpers-0c4b6f40.js';
import './web.dom.iterable-ab4dea5c.js';
import './core.get-iterator-method-ea258bb1.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 imageUrlBuilder(client).image(source).auto('format');
  }

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

  var builder = 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);
  url = url.auto('format').url();

  // Appending file name in URL
  var originalFilename = source && source.originalFilename ? source.originalFilename : source && source.asset && source.asset.originalFilename && source.asset.originalFilename;
  if (originalFilename) {
    var _url$split = url.split('?'),
        _url$split2 = _slicedToArray(_url$split, 2),
        path = _url$split2[0],
        queryString = _url$split2[1];

    originalFilename = encodeURIComponent(originalFilename);
    url = queryString ? path + '/' + originalFilename + '?' + queryString : path + '/' + originalFilename;
  }

  return url;
}

export { urlFor as default };
