// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@import "@material/theme/mixins";
@import "@material/theme/variables"; // for mdc-theme-prop-value

@mixin mdc-linear-progress-bar-color($color) {
  .mdc-linear-progress__bar-inner {
    @include mdc-theme-prop(background-color, $color);
  }
}

@mixin mdc-linear-progress-buffer-color($color) {
  // We need to escape the '#' character as "%23" for SVG because '#' is a reserved character in URIs.
  $color-value-for-css: mdc-theme-prop-value($color);
  $color-value-for-svg: mdc-linear-progress-str-replace_(#{$color-value-for-css}, "#", "%23");

  .mdc-linear-progress__buffering-dots {
    // SVG is optimized for data URI (https://codepen.io/tigt/post/optimizing-svgs-in-data-uris)
    // stylelint-disable-next-line function-url-quotes
    background-image: url("data:image/svg+xml,%3Csvg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' enable-background='new 0 0 5 2' xml:space='preserve' viewBox='0 0 5 2' preserveAspectRatio='none slice'%3E%3Ccircle cx='1' cy='1' r='1' fill='#{$color-value-for-svg}'/%3E%3C/svg%3E");
  }

  .mdc-linear-progress__buffer {
    background-color: $color-value-for-css;
  }
}

// Based on https://css-tricks.com/snippets/sass/str-replace-function/
@function mdc-linear-progress-str-replace_($string, $search, $replace) {
  $index: str-index($string, $search);

  @if $index {
    $head: str-slice($string, 1, $index - 1);
    $tail: mdc-linear-progress-str-replace_(str-slice($string, $index + str-length($search)), $search, $replace);

    @return $head + $replace + $tail;
  }

  @return $string;
}
