// Helper functions!

// Replace `$search` with `$replace` in `$string`
// Credit to Hugo Giraudel: http://sassmeister.com/gist/1b4f2da5527830088e4d
//
// @param {String} $string       - Initial string
// @param {String} $search       - Substring to replace
// @param {String} $replace ('') - New value
//
// @return {String} - Updated string
@function str-replace($string, $search, $replace: "") {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;
}


// URL-encode a given string.
// @param {String} $string - Initial string
// @return {String} - URL-encoded string
@function url-encode($string) {
  $entities: (
    "%": "%25", // Must be first to prevent re-replacing escaped
    " ": "%20",   "!": "%21",   "\"": "%22",  "#": "%23",
    "$": "%24",   "&": "%26",   "'": "%27",   "(": "%28",
    ")": "%29",   "*": "%2A",   "+": "%2B",   ",": "%2C",
    "\/": "%2F",   ":": "%3A",   ";": "%3B",   "<": "%3C",
    "=": "%3D",   ">": "%3E",   "?": "%3F",   "@": "%40",
    "\\": "%5C"
  );

  // Fix issue that only seems to be happening in Libsass...
  $string: " " + $string + " ";

  @each $entity, $replacement in $entities {
    $string: str-replace($string, $entity, $replacement);
  }

  @return quote($string);
}


// Helper to generate Neue URLs in client app stylesheets
// @NOTE See `neue-build.scss` for $neue-path.
@function neue-asset-url($file) {
  @return url("#{$neue-path}#{$file}");
}


// Helper to generate URLs in client app stylesheets
// @NOTE See `neue-build.scss` for $asset-path.
@function asset-url($file) {
  @return url("#{$asset-path}#{$file}");
}


// Helper to use data URLs in stylesheets
@function data-url($data) {
  @return url("#{$data}");
}
