//
// Copyright 2020 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

@use 'sass:list';
@use 'sass:meta';
@use 'sass:string';

/// Replaces all name instances in the provided string with values from the
/// provided replacement Map whose keys correspond to the name instances.
/// Returns a new string with the replacements applied.
///
/// @example
///   replace-string('calc(x + y)', (x: 16px, y: 50%)); // 'calc(16px + 50%)'
///
/// @example
///   $foo: custom-properties.create-var(custom-properties.create(--foo, 8px));
///   replace-string('calc(foo)', (foo: $foo)); // 'calc(var(--foo, 8px))';
///
/// @access private
/// @param {String} $string - The string to make replacements on.
/// @param {Map} $replace-map - A Map of name/value replacements. The keys of
///     the Map are names that will be replaced in the string with the keys'
///     respective values.
/// @return {String} The string with replacements made, if any.
@function replace-string($string, $replace-map) {
  @if meta.type-of($replace-map) != 'map' {
    @error 'mdc-theme: Invalid replacement #{$replace-map}. Must be a Map.';
  }

  @each $name, $replacement in $replace-map {
    // Since the replacement values may contain the name themselves (such as
    // a custom property: name "foo" and value "var(--foo)"), gather the indices
    // first before replacing.
    $indices: ();
    $substring: $string;
    $prev-index: null;
    $index: string.index($substring, $name);
    @while $index {
      $substring: string.slice($substring, $index + string.length($name));

      @if $prev-index {
        // Add previous index's value to switch from "substring index" to
        // absolute string index. Also add length - 1 since slice removes
        // the entire word as well as lists being 1 indexed
        $index: $index + $prev-index + string.length($name) - 1;
      }

      // Use list.join() to "prepend" the index to the start of the list. This
      // allows us to iterate "backwards" later.
      $indices: list.join($index, $indices);
      $prev-index: $index;
      $index: string.index($substring, $name);
    }

    // Since we "prepended" the indices, the list is sorted backwards, with the
    // last index first. Replacing values last index to first index removes the
    // need for us to adjust the indices as we replace them.
    @each $index in $indices {
      $before: string.slice($string, 1, $index - 1);
      $after: string.slice($string, $index + string.length($name));
      $string: $before + $replacement + $after;
    }
  }

  @return $string;
}

/// Replaces all value instances in the provided list with values from the
/// provided replacement Map whose keys correspond to the name instances.
/// Returns a new list with the replacements applied.
///
/// @example
///   replace-list(0 value, (value: 16px)); // (0 16px)
///
/// @see {mixin} replace-string
///
/// @access private
/// @param {List} $list - The list to make replacements on.
/// @param {Map} $replace-map - A Map of name/value replacements. The keys of
///     the Map are names that will be replaced in the list with the keys'
///     respective values.
///     property value replacements instead of the `var()` declaration.
/// @return {List} A new list with replacements made, if any.
@function replace-list($list, $replace-map) {
  $separator: list.separator($list);
  $replaced-list: ();
  @each $value in $list {
    @if meta.type-of($value) == 'string' {
      $replaced-list: list.append(
        $replaced-list,
        replace-string($value, $replace-map),
        $separator
      );
    } @else if meta.type-of($value) == 'list' {
      $replaced-list: list.append(
        $replaced-list,
        replace-list($value, $replace-map),
        $separator
      );
    } @else {
      $replaced-list: list.append($replaced-list, $value, $separator);
    }
  }

  @return $replaced-list;
}
