/// Replace `$search` with `$replace` in `$string`
/// @author Hugo Giraudel
@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;
}

/// @author Hugo Giraudel
/// Taken from https://css-tricks.com/snippets/sass/power-function/
@function pow($number, $exponent) {
  $value: 1;

  @if $exponent > 0 {
    @for $i from 1 through round($exponent) {
      $value: $value * $number;
    }
  }

  @return $value;
}

/// Checks Luminance of Given Color
/// @author Hugo Giraudel
/// Taken from https://css-tricks.com/snippets/sass/luminance-color-function/
@function luminance($color) {
  $colors: (
    'red': red($color),
    'green': green($color),
    'blue': blue($color)
  );

  @each $name, $value in $colors {
    $adjusted: 0;
    $value: $value / 255;

    @if $value < 0.03928 {
      $value: $value / 12.92;
    } @else {
      $value: ($value + .055) / 1.055;
      $value: pow($value, 2.4);
    }

    $colors: map-merge($colors, ($name: $value));
  }

  @return (map-get($colors, 'red') * .2126) + (map-get($colors, 'green') * .7152) + (map-get($colors, 'blue') * .0722);
}

/// Returns a List with all List item from a given index from a List
/// @author Sarsa Murmu
/// @param {list} $given-list - A List with Nested Lists.
/// @param {number} $it-index - Index of the item to get.
/// @return {list} - List of all Items Found.
/// @example Usage
/// $list: (
///   ('mobile', null, $tablet - 1px),
///   ('touch', null, $desktop - 1px),
///   ('tablet', $tablet, $desktop - 1px),
///   ('desktop', $desktop, null)
/// );
///
/// @debug getListItems($list, 1); -> ('mobile', 'touch', 'tablet', 'desktop')
@function getListItems($given-list, $it-index) {
  $list: () !default;

  @for $index from 1 through length($given-list) {
    $item-list: nth($given-list, $index);

    @if length($item-list) == 0 {} @else {
      $item: nth($item-list, $it-index);
      $list: append($list, $item, $separator: comma);
    }
  }

  @return $list;
}


@function getClassList($list, $selector, $replace, $prefix, $suffix, $seperator: '') {
  $classlist: $prefix;

  @for $i from 1 through length($list) {
    $name: nth($list, $i);

    $classname: str-replace($selector, $replace, $name);

    @if $i < length($list) {
      $classlist: $classlist + $classname + $seperator;
    } @else if $i == length($list) {
      $classlist: $classlist + $classname + $suffix;
    }
  }

  @return $classlist;
}

@function textForBg($color) {
  $lightness: luminance($color);

  @if $lightness > 0.5 {
    @return $text-dark;
  } @else {
    @return $white;
  }
}

@function getColor($name) {
  @for $i from 1 through length($colors) {
    $currentList: nth($colors, $i);

    @if index($currentList, $name) {
      @return nth($currentList, 2);
    }
  }

  @return $name;
}
