// @import '../../base/_generic/until/math';

/// Returns the color value for a given color name and group.
///
/// @param {String} $hue - The color's hue.
/// @param {Number} $range - The darkness/lightness of the color. Defaults to base. The higher the number, the darker the color.
/// @return {Color} The color value.

@function color($hue, $range: 600) {
  $fetched-color: map-get(map-get($color-palette-data, $hue), $range);

  @if map-has-key($color-palette-data, $fetched-color) {
    $fetched-color: map-get(map-get($color-palette-data, $fetched-color), $range);
  }

  @if type-of($fetched-color) == color {
    @return $fetched-color;
  } @else if $fetched-color == null {
    $fetched-based-color: map-get(map-get($color-palette-data, $hue), 500);

    @if $range <= 100 {
      $fetched-color: lighten($fetched-based-color, 40%); 
      @return $fetched-color;
    } @else if $range == 200 {
      $fetched-color: lighten($fetched-based-color, 30%); 
      @return $fetched-color;
    } @else if $range == 300 {
      $fetched-color: lighten($fetched-based-color, 20%); 
      @return $fetched-color;
    } @else if $range == 400 {
      $fetched-color: lighten($fetched-based-color, 10%); 
      @return $fetched-color;
    } @else if $range == 600 {
      $fetched-color: darken($fetched-based-color, 10%); 
      @return $fetched-color;
    } @else if $range == 700 {
      $fetched-color: darken($fetched-based-color, 20%);
      @return $fetched-color;
    } @else if $range == 800 {
      $fetched-color: darken($fetched-based-color, 30%); 
      @return $fetched-color;
    } @else if $range >= 900 {
      $fetched-color: darken($fetched-based-color, 40%); 
      @return $fetched-color;
    } @else {
      @error 'The `#{$range}` could not be found. Make sure you have specify a range between 000 and 1000.';
    }
  } @else {
    @error 'Color `#{$hue} - #{$range}` not found. Available colors: #{$color-palette-data}';
  }
}


/// Returns a foreground color based on a given background-color in accordance with accessibility standards.
///
/// @param {Color} $background-color - The background color where the foreground will sit.
/// @param {Color} $dark-color - The dark color you wish to display if there is enough contrast.
/// @param {Color} $light-color - The light color you wish to display if there is enough contrast.
/// @return {Color} The HEX color of the foreground.
@function color-contrast($background-color, $dark-color: color(neutral, 900), $light-color: color(neutral, 000)) {
  @if $background-color == null {
    @return null;
  } @else {
    // Based on the algorithm recommended by W3: https://www.w3.org/TR/AERT/#color-contrast
    $yiq: (red($background-color) * 299 + green($background-color) * 587 + blue($background-color) * 114) / 1000;
    @return if($yiq >= 128, $dark-color, $light-color);
  }
}




$contrast-warnings: true !default;

////
/// @group functions
////

/// Checks the luminance of `$color`.
///
/// @param {Color} $color - Color to check the luminance of.
///
/// @returns {Number} The luminance of `$color`.
@function color-luminance($color) {
  // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
  // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
  $rgba: red($color), green($color), blue($color);
  $rgba2: ();

  @for $i from 1 through 3 {
    $rgb: nth($rgba, $i);
    $rgb: $rgb / 255;

    $rgb: if($rgb < 0.03928, $rgb / 12.92, pow(($rgb + 0.055) / 1.055, 2.4));

    $rgba2: append($rgba2, $rgb);
  }

  @return 0.2126 * nth($rgba2, 1) + 0.7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3);
}

/// Checks the contrast ratio of two colors.
///
/// @param {Color} $color1 - First color to compare.
/// @param {Color} $color2 - Second color to compare.
///
/// @returns {Number} The contrast ratio of the compared colors.
@function color-contrast2($color1, $color2) {
  // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
  // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
  $luminance1: color-luminance($color1) + 0.05;
  $luminance2: color-luminance($color2) + 0.05;
  $ratio: $luminance1 / $luminance2;

  @if $luminance2 > $luminance1 {
    $ratio: 1 / $ratio;
  }

  $ratio: round($ratio * 10) / 10;

  @return $ratio;
}

/// Checks the luminance of `$base`, and returns the color from `$colors` (list of colors) that has the most contrast.
///
/// @param {Color} $base - Color to check luminance.
/// @param {List} $colors [($white, $black)] - Colors to compare.
/// @param {Number} $tolerance [$global-color-pick-contrast-tolerance] - Contrast tolerance.
///
/// @returns {Color} the color from `$colors` (list of colors) that has the most contrast.
@function color-pick-contrast($base, $colors: ($white, $black), $tolerance: $global-color-pick-contrast-tolerance) {
  $contrast: color-contrast($base, nth($colors, 1));
  $best: nth($colors, 1);

  @for $i from 2 through length($colors) {
    $current-contrast: color-contrast($base, nth($colors, $i));
    @if ($current-contrast - $contrast > $tolerance) {
      $contrast: color-contrast($base, nth($colors, $i));
      $best: nth($colors, $i);
    }
  }

  @if ($contrast-warnings and $contrast < 3) {
    @warn "Contrast ratio of #{$best} on #{$base} is pretty bad, just #{$contrast}";
  }

  @return $best;
}

/// Scales a color to be darker if it's light, or lighter if it's dark. Use this function to tint a color appropriate to its lightness.
///
/// @param {Color} $color - Color to scale.
/// @param {Percentage} $scale [5%] - Amount to scale up or down.
/// @param {Percentage} $threshold [40%] - Threshold of lightness to check against.
///
/// @returns {Color} A scaled color.
@function smart-scale($color, $scale: 5%, $threshold: 40%) {
  @if lightness($color) > $threshold {
    $scale: -$scale;
  }
  @return scale-color($color, $lightness: $scale);
}

/// Get color from $color-palette-data
///
/// @param {key} color key from $color-palette-data
///
/// @returns {Color} color from $color-palette-data
@function get-color($key) {
  @if map-has-key($color-palette-data, $key) {
    @return map-get($color-palette-data, $key);
  }
  @else {
    @error 'given $key is not available in $color-palette-data';
  }
}
