@use "sass:color";
@use "sass:list";
@use "sass:map";
@use "sass:math";
@use "sass:meta";
@use "sass:string";
@use "./variables" as var;

/// Encodes a color for use in data-uri
/// @param {String} $string - The color to encode
/// @return {String} - Encoded color
@function encodecolor($string) {
  @if meta.type-of($string) == "color" {
    $hex: string.slice(color.ie-hex-str($string), 4);
    $string: string.unquote("#{$hex}");
  }
  $string: "%23" + $string;
  @return $string;
}

/// Replacing values from list.
/// @param {List} $list
/// @param {Value} $old-value
/// @param {Value} $new-value
/// @param {Boolean} $recursive [false]
/// @return {List} - list with replaced values
@function replace($list, $old-value, $new-value, $recursive: false) {
  $result: ();
  @for $i from 1 through list.length($list) {
    @if meta.type-of(list.nth($list, $i)) == "list" and $recursive {
      $result: list.append(
        $result,
        replace(list.nth($list, $i), $old-value, $new-value, $recursive)
      );
    }
    @else {
      @if list.nth($list, $i) == $old-value {
        $result: list.append($result, $new-value);
      }
      @else {
        $result: list.append($result, list.nth($list, $i));
      }
    }
  }
  @return $result;
}

/// Replaces a boolean list template with two values.
/// @param {List} $tpl - A boolean list template
/// @param {Color} $color-1 - Color to replace 1 from template
/// @param {Color} $color-0 [transparent] - Color to replace 0 from template
/// @return {List} - New list with replaced values
@function set-border-color($tpl, $color-1, $color-0: transparent) {
  $result: replace($tpl, 0, $color-0);
  $result: replace($result, 1, $color-1);
  @return $result;
}
