//////////////////////////////
// Color Palette
//
// Gets the specified color from the specified color palette
//
// Usage:
//
// background: color('blue', 80); // #1d3649
// background: color('blue');     // #4178be
//
// background: color('blue', 80, $alpha: 0.5); // rgba(29, 54, 73, 0.5)
// background: color('blue', $alpha: 0.5); // rgba(65, 120, 190, 0.5)
//
//////////////////////////////
@function color($palette, $grade: 0, $alpha: 1) {
  // Because it's spelled gr(a|e)y and we've got spaces
  @if $palette == 'grey' {
    $palette: 'gray';
  }
  @else if $palette == 'warm-grey' or $palette == 'warm grey' or $palette == 'warm gray' {
    $palette: 'warm-gray';
  }
  @else if $palette == 'cool-grey' or $palette == 'cool grey' or $palette == 'cool gray' {
    $palette: 'cool-gray';
  }
  @else if $palette == 'neutral white' {
    $palette: 'neutral-white';
  }
  @else if $palette == 'cool white' {
    $palette: 'cool-white';
  }
  @else if $palette == 'warm white' {
    $palette: 'warm-white';
  }

  $plt: map-get($__ibm-color-palettes, $palette);

  @if $plt {
    $grd: map-get($plt, $grade);
    @return rgba($grd, $alpha);
  }

  $error-message: 'Color palette "#{$palette}" not found';
  @if not $found-index {
    @if feature-exists(at-error) {
      @error $error-message;
    }
    @else {
      @warn $error-message;
    }
  }

  @return false;
}

//////////////////////////////
// Tint and Shade Functions
//////////////////////////////
$singles: 'neutral-white', 'cool-white', 'warm-white';
// Internal helper: finds the palette and key of a color
@function _ibm-find-color($color) {
  $found-index: false;
  $found-palette: false;
  @each $palette, $vals in $__ibm-color-palettes {
    @if not $found-index {
      @each $key, $clr in $vals {
      	@if $color == $clr and $key != 'core' {
      	  $found-index: $key;
      	  $found-palette: $palette;
      	}
      }
    }
  }

  $error-message: 'Color #{$color} is not an IBM color';

  @if not $found-index {
    @if feature-exists(at-error) {
      @error $error-message;
    }
    @else {
      @warn $error-message;
    }
  }
  @return ('index': $found-index, 'palette': $found-palette);
}

// Internal helper: transforms amount into base 10, rounding
@function _ibm-round-tint-shade($amount) {
  @if $amount < 1 {
    $amount: $amount * 10;
  }

  $return: round($amount);
  $remainder: $return % 10;
  $return: $return - $remainder;
  @if $remainder >= 5 {
    $return: $return + 10;
  }

  @return $return;
}

//////////////////////////////
// Tint an IBM Color
//
// Pass in a color (as retrieved from the `color` function) and how many stops you want to tint (lighten) the color along its palette.
// Pegged to the lightest color in the palette
//////////////////////////////
@function color-tint($color, $amount) {
  $key: _ibm-find-color($color);
  $index: map-get($key, 'index');
  $palette: map-get($key, 'palette');
  $move: _ibm-round-tint-shade($amount);

  @if index($singles, $palette) {
    $index: $index - ($move / 10);
    @if $index < 1 {
      $index: 1;
    }
  }
  @else if $palette == 'black' {
    $index: 100;
  }
  @else if $palette == 'white' {
    $index: 0;
  }
  @else {
    $index: $index - $move;
    @if $index < 10 {
      $index: 1;
    }
  }

  @return color($palette, $index);
}

//////////////////////////////
// Shade an IBM Color
//
// Pass in a color (as retrieved from the `color` function) and how many stops you want to shade (darken) the color along its palette.
// Pegged to the darkest color in the palette
//////////////////////////////
@function color-shade($color, $amount) {
  $key: _ibm-find-color($color);
  $index: map-get($key, 'index');
  $palette: map-get($key, 'palette');
  $move: _ibm-round-tint-shade($amount);

  @if index($singles, $palette) {
    $index: $index - ($move / 10);
    @if $index > 4 {
      $index: 4;
    }
  }
  @else if $palette == 'black' {
    $index: 100;
  }
  @else if $palette == 'white' {
    $index: 0;
  }
  @else {
    $index: $index + $move;
    @if ($index > 90) {
      $index: 90;
    }
  }

  @return color($palette, $index);
}

//////////////////////////////
// Get Colors
//
// Lists out available colors
//////////////////////////////
@function get-colors($palette: null) {
  // Return a list of colors available if nothing is passed in
  @if $palette == null {
    @return map-keys($__ibm-color-palettes);
  }
  // Return the full color map if 'all' is passed in
  @else if $palette == 'all' {
    @return $__ibm-color-palettes;
  }
  // Return just the color map if a specific color is passed in
  @else {
    @return map-get($__ibm-color-palettes, $palette);
  }
}
