// Parse color shift weight by range

$color-shift-weights: () !default;
$color-shift-weights: map-merge(
  (
    -75:  "whiter",
    -50:  "white",
    -33:  "lightest",
    -21:  "lighter",
    -10:  "light",
    -7:   "lighten",
    0:    "",
    8:    "darken",
    10:   "dark",
    13:   "darker",
    16:   "darkest",
    50:   "black"
  ),
  $color-shift-weights
);

@function label-color-weight($min-shift-weight) {
  @each $shift-weight, $label in $color-shift-weights {
    @if $shift-weight >= $min-shift-weight {
      @return $label;
    }
  }
  @return $min-shift-weight;
}

// Lighten a color or get light CSS var
@function lighten-color($color, $weight) {
  @if type-of($color) == color {
    @return lighten($color, $weight);
  } @else {
    @return str-insert($color, -#{label-color-weight(-$weight)}, -2);
  }
}

// Darken a color or get dark CSS var
@function darken-color($color, $weight) {
  @if $weight < 0 {
    @return lighten-color($color, -$weight);
  }
  @if type-of($color) == color {
    @return darken($color, $weight);
  } @else {
    @return str-insert($color, -#{label-color-weight($weight)}, -2);
  }
}

// RGB color opacity with alpha
@function rgba-color($color, $alpha) {
  @if type-of($color) == color {
    @return rgba($color, $alpha);
  } @else {
    @return rgba(#{str-insert($color, -rgb, -2)}, #{$alpha});
  }
}

// Color contrast
@function color-yiq($color, $dark: $yiq-text-dark, $light: $yiq-text-light) {
  @if type-of($color) == color {
    $r: red($color);
    $g: green($color);
    $b: blue($color);
    $yiq: (($r * 299) + ($g * 587) + ($b * 114)) * .001;
    @if ($yiq >= $yiq-contrasted-threshold) {
      @return $dark;
    } @else {
      @return $light;
    }
  } @else {
    @return str-insert($color, -yiq, -2);
  }
}

// Request a theme color level with mix

@function mix-color($color-base, $color, $weight: 50) {
  @if type-of($color-base) == color {
    @if type-of($color) == color {
      @return mix($color-base, $color, $weight);
    }
  }
  @if $color-base == $white or $color-base == $white {
    @return lighten-color($color, calc($weight / 2));
  } @else {
    @return darken-color($color, calc($weight / 2));
  }
}

@function theme-color-level($color-name: "primary", $level: 0) {
  $color: theme-color($color-name);
  $color-base: if($level > 0, $black, $white);
  @return mix-color($color-base, $color, abs($level * $theme-color-interval));
}

// Color system

$white:    #fff !default;
$gray-100: #f8f9fa !default;
$gray-200: #e9ecef !default;
$gray-300: #dee2e6 !default;
$gray-400: #ced4da !default;
$gray-500: #adb5bd !default;
$gray-600: #6c757d !default;
$gray-700: #495057 !default;
$gray-800: #343a40 !default;
$gray-900: #212529 !default;
$black:    #000 !default;

$grays: () !default;
$grays: map-merge(
  (
    "100": $gray-100,
    "200": $gray-200,
    "300": $gray-300,
    "400": $gray-400,
    "500": $gray-500,
    "600": $gray-600,
    "700": $gray-700,
    "800": $gray-800,
    "900": $gray-900
  ),
  $grays
);

$blue:    #007bff !default;
$indigo:  #6610f2 !default;
$purple:  #6f42c1 !default;
$pink:    #e83e8c !default;
$red:     #dc3545 !default;
$orange:  #fd7e14 !default;
$yellow:  #ffc107 !default;
$green:   #28a745 !default;
$teal:    #20c997 !default;
$cyan:    #17a2b8 !default;

$colors: () !default;
$colors: map-merge(
  (
    "blue":       $blue,
    "indigo":     $indigo,
    "purple":     $purple,
    "pink":       $pink,
    "red":        $red,
    "orange":     $orange,
    "yellow":     $yellow,
    "green":      $green,
    "teal":       $teal,
    "cyan":       $cyan,
    "white":      $white,
    "gray":       $gray-600,
    "gray-dark":  $gray-800
  ),
  $colors
);

$primary:       $blue !default;
$secondary:     $gray-600 !default;
$success:       $green !default;
$info:          $cyan !default;
$warning:       $yellow !default;
$danger:        $red !default;
$light:         $gray-100 !default;
$dark:          $gray-800 !default;

$theme-colors: () !default;
$theme-colors: map-merge(
  (
    "primary":    $primary,
    "secondary":  $secondary,
    "success":    $success,
    "info":       $info,
    "warning":    $warning,
    "danger":     $danger,
    "light":      $light,
    "dark":       $dark
  ),
  $theme-colors
);

// Set a specific jump point for requesting color jumps
$theme-color-interval:      8% !default;

// The yiq lightness value that determines when the lightness of color changes from "dark" to "light". Acceptable values are between 0 and 255.
$yiq-contrasted-threshold:  150 !default;

// Customize the light and dark text colors for use in our YIQ color contrast function.
$yiq-text-dark:             var(--gray-dark) !default;
$yiq-text-light:            var(--white) !default;

// Smaller color maps for less used components

$alert-colors: () !default;
$alert-colors: map-merge(
  (
    "success":    $success,
    "info":       $info,
    "warning":    $warning,
    "danger":     $danger
  ),
  $alert-colors
);

$badge-colors: () !default;
$badge-colors: map-merge(
  (
    "primary":    var(--primary),
    "secondary":  var(--secondary),
    "success":    $success,
    "info":       $info,
    "warning":    $warning,
    "dark":       $dark
  ),
  $badge-colors
);

$btn-colors: () !default;
$btn-colors: map-merge(
  (
    "primary":    var(--primary),
    "secondary":  var(--secondary),
    "success":    $success,
    "danger":     $danger,
    "light":      $light,
    "dark":       $dark
  ),
  $btn-colors
);

$btn-outline-colors: () !default;
$btn-outline-colors: map-merge(
  (
    "primary":    var(--primary),
    "secondary":  var(--secondary),
    "success":    $success,
    "danger":     $danger
  ),
  $btn-outline-colors
);
