@function svg-url($svg) {
  //
  //  Add missing namespace
  //
  @if not str-index($svg,xmlns) {
    $svg: str-replace($svg, "<svg", "<svg xmlns=\"http://www.w3.org/2000/svg\"");
  }
  //
  //  Chunk up string in order to avoid
  //  "stack level too deep" error
  //
  $encoded: "";
  $slice: 2000;
  $index: 0;
  $loops: ceil(str-length($svg) / $slice);
  @for $i from 1 through $loops {
    $chunk: str-slice($svg, $index, $index + $slice - 1);
    //
    //   Encode
    //
    $chunk: str-replace($chunk, "\"", "\"");
    $chunk: str-replace($chunk, "%", "%25");
    $chunk: str-replace($chunk, "#", "%23");
    $chunk: str-replace($chunk, "{", "%7B");
    $chunk: str-replace($chunk, "}", "%7D");
    $chunk: str-replace($chunk, "<", "%3C");
    $chunk: str-replace($chunk, ">", "%3E");

    //
    //    The maybe list
    //
    //    Keep size and compile time down
    //    ... only add on documented fail
    //
    //  $chunk: str-replace($chunk, "&", "%26");
    //  $chunk: str-replace($chunk, "|", "%7C");
    //  $chunk: str-replace($chunk, "[", "%5B");
    //  $chunk: str-replace($chunk, "]", "%5D");
    //  $chunk: str-replace($chunk, "^", "%5E");
    //  $chunk: str-replace($chunk, "`", "%60");
    //  $chunk: str-replace($chunk, ";", "%3B");
    //  $chunk: str-replace($chunk, "?", "%3F");
    //  $chunk: str-replace($chunk, ":", "%3A");
    //  $chunk: str-replace($chunk, "@", "%40");
    //  $chunk: str-replace($chunk, "=", "%3D");

    $encoded: #{$encoded}#{$chunk};
    $index: $index + $slice;
  }
  @return url("data:image/svg+xml,#{$encoded}");
}

//  Background svg mixin
@mixin background-svg($svg) {
  background-image: svg-url($svg);
}

//  Helper function to replace characters in a string
@function str-replace($string, $search, $replace: "") {
  $index: str-index($string, $search);
  @return if($index,
  str-slice($string, 1, $index - 1) + $replace +
  str-replace(str-slice($string, $index +
  str-length($search)), $search, $replace),
  $string);
}

// Chevron helper
@mixin lbcamden-chevron($colour, $rotation: 0, $stroke: 1, $size: 3) {
  width: $size * 3 + px;
  height: $size * 3 + px;

  @include govuk-media-query($from: tablet) {
    width: $size * 4 + px;
    height: $size * 4 + px;
  }

  transform: rotate($rotation + deg);
  border: none;
  background: chevron-stroke-width($stroke, $colour);
  background-repeat: no-repeat;
  background-size: contain;
}

@function chevron-stroke-width($stroke, $colour) {
  @if $stroke == 1 {
    @return svg-url("<svg width='56' height='56' xmlns='http://www.w3.org/2000/svg'><path d='M43.3796317,16.0659773 L19.6760056,39.0442854 C18.0556176,40.6143387 15.4293191,40.6143387 13.8096716,39.0442854 C12.1893207,37.4742322 12.1893207,34.9276768 13.8096716,33.3576235 L34.5808233,13.2218814 L13.8096716,-6.91377078 C12.1893207,-8.48391407 12.1893207,-11.0304559 13.8096716,-12.6005317 C15.4293739,-14.1707222 18.0556633,-14.1707222 19.6760056,-12.6005317 L43.3796317,10.3778304 C44.999334,11.9480187 44.999334,14.494484 43.3796317,16.0645823 L43.3796317,16.0659773 Z' fill='" + $colour + "' transform='translate(0, 14.000000)' /></svg>");
  }
  @if $stroke == 2 {
    @return svg-url("<svg width='56' height='56' xmlns='http://www.w3.org/2000/svg'><path d='M29.9246628,14.0000793 L13.6376822,30.8037018 L13.6370331,30.8037018 C12.2065197,32.3317866 11.6630217,34.525565 12.2078253,36.5744415 C12.7526308,38.622868 14.3041325,40.2235387 16.2898102,40.785596 C18.2754742,41.3476533 20.4017828,40.786946 21.8829714,39.3113769 L42.2928914,18.2538494 C43.3856457,17.1250998 44,15.59544 44,14.0000793 C44,12.4047187 43.3856,10.8750139 42.2928914,9.74630924 L21.8829714,-11.3110383 C20.4016914,-12.7869283 18.2754742,-13.3476769 16.2898102,-12.7855917 C14.3041325,-12.2235061 12.7526445,-10.6227859 12.2078253,-8.57412088 C11.6630052,-6.52545137 12.2065165,-4.33171797 13.6370331,-2.80354319 L29.9246628,14.0000793 Z' fill='" + $colour + "' transform='translate(0, 14.000000)' /></svg>");
  }
  @if $stroke == 3 {
    @return svg-url("<svg width='14' height='14' xmlns='http://www.w3.org/2000/svg'><path d='M0 1.741A1 1 0 0 1 1.504.877l9.015 5.26a1 1 0 0 1 0 1.727l-9.015 5.259A1 1 0 0 1 0 12.259V1.741Z' fill='" + $colour + "' /></svg>");
  }
}
