/* -------------------------------------------- */
/* MIXIN                                        */
/* -------------------------------------------- */

@mixin button-variant(
  $background,
  $border,
  $color: color-contrast($background),
  $hover-background: if($color == $color-contrast-light, shade-color($background, $btn-hover-bg-shade-amount), tint-color($background, $btn-hover-bg-tint-amount)),
  $hover-border: if($color == $color-contrast-light, shade-color($border, $btn-hover-border-shade-amount), tint-color($border, $btn-hover-border-tint-amount)),
  $hover-color: color-contrast($hover-background),
  $active-background: if($color == $color-contrast-light, shade-color($background, $btn-active-bg-shade-amount), tint-color($background, $btn-active-bg-tint-amount)),
  $active-border: if($color == $color-contrast-light, shade-color($border, $btn-active-border-shade-amount), tint-color($border, $btn-active-border-tint-amount)),
  $active-color: color-contrast($active-background),
  $disabled-background: $background,
  $disabled-border: $border,
  $disabled-color: color-contrast($disabled-background)
) {
  color: $color;
  @include gradient-bg($background);
  border-color: $border;
  @include box-shadow($btn-box-shadow);

  &:hover {
    color: $hover-color;
    background-color: $hover-background;
    border-color: $hover-border;
  }

  &:focus {
    color: $hover-color;
    border-color: $hover-border;
    background-color: $hover-background;

    @if $enable-shadows {
      @include box-shadow(
        $btn-box-shadow,
        0 0 0 $btn-focus-width rgba(
          mix($color, $border, 15%),
          .5
        )
      );
    } @else {
      // Avoid using mixin so we can pass custom focus shadow properly
      box-shadow: 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5);
    }
  }

  &:active,
  &.active {
    color: $active-color;
    background-color: $active-background;
    background-image: if($enable-gradients, none, null);
    border-color: $active-border;

    &:focus {
      @if $enable-shadows {
        @include box-shadow($btn-active-box-shadow, 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5));
      } @else {
        // Avoid using mixin so we can pass custom focus shadow properly
        box-shadow: 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5);
      }
    }
  }

  &:disabled,
  &.disabled {
    color: $disabled-color;
    background-color: $disabled-background;
    // Remove CSS gradients if they're enabled
    background-image: if($enable-gradients, none, null);
    border-color: $disabled-border;
  }
}

@mixin button-outline-variant(
  $color,
  $color-hover: color-contrast($color),
  $active-background: $color,
  $active-border: $color,
  $active-color: color-contrast($active-background)
) {
  color: $color;
  border-color: $color;

  &:hover {
    color: $color-hover;
    background-color: $active-background;
    border-color: $active-border;
  }

  &:focus {
    box-shadow: 0 0 0 $btn-focus-width rgba($color, .5);
  }

  &:active,
  &.active {
    color: $active-color;
    background-color: $active-background;
    border-color: $active-border;

    &:focus {
      @if $enable-shadows {
        @include box-shadow($btn-active-box-shadow, 0 0 0 $btn-focus-width rgba($color, .5));
      } @else {
        // Avoid using mixin so we can pass custom focus shadow properly
        box-shadow: 0 0 0 $btn-focus-width rgba($color, .5);
      }
    }
  }

  &:disabled,
  &.disabled {
    color: $color;
    background-color: transparent;
  }
}

@mixin button-size($padding-y, $padding-x, $font-size, $border-radius) {
  padding: $padding-y $padding-x;
  @include font-size($font-size);
  @include border-radius($border-radius, 0);
}


/// Button reset
///
%button-reset {
  border: none;
  background-color: transparent;
  padding: initial;
  text-align: initial;
  cursor: if($enable-button-pointers, pointer, null);
  user-select: none;
}

/* -------------------------------------------- */
/* BUTTONS                                      */
/* -------------------------------------------- */


.btn {
  display: inline-block;
  color: $body-color;
  font-weight: $btn-font-weight;
  font-family: $btn-font-family;
  line-height: $btn-line-height;
  letter-spacing: $btn-letter-spacing;
  white-space: $btn-white-space;
  text-align: center;
  text-decoration: if($link-decoration == none, null, none);
  vertical-align: middle;
  background-color: transparent;
  border: $btn-border-width solid transparent;
  cursor: if($enable-button-pointers, pointer, null);
  user-select: none;

  @if $btn-text-uppercase {
    text-transform: uppercase;
  }

  @include button-size($btn-padding-y, $btn-padding-x, $btn-font-size, $btn-border-radius);
  @include transition($btn-transition);

  &:hover {
    color: $body-color;
    text-decoration: if($link-hover-decoration == underline, none, null);
  }

  &:focus {
    outline: 0;
    box-shadow: $btn-focus-box-shadow;
  }

  &:active,
  &.active {
    @include box-shadow($btn-active-box-shadow);

    &:focus {
      @include box-shadow($btn-focus-box-shadow, $btn-active-box-shadow);
    }
  }

  &:disabled,
  &.disabled,
  fieldset:disabled & {
    opacity: $btn-disabled-opacity;
    pointer-events: none;
    @include box-shadow(none);
  }

  &.is-loading {
    &::after {
      content: "";
      @extend .spinner;
      width: 0.745rem;
      height: 0.745rem;
      border-width: 0.1rem;
      margin-left: $input-padding-x - .5;
    }
  }
}



//
// Alternate buttons
//

@each $color, $value in $button-colors {
  .btn.bg-#{$color} {
    @include button-variant($value, $value);
  }
}

@each $color, $value in $button-colors {
  .btn-#{$color} {
    @include button-outline-variant($value);
  }
}



//
// Link buttons
//

// Make a button look and behave like a link
.btn-href {
  color: $btn-link-color;
  font-weight: $font-weight-normal;
  text-decoration: $link-decoration;

  &:hover {
    color: $btn-link-hover-color;
    text-decoration: $link-hover-decoration;
  }

  &:focus {
    text-decoration: $link-hover-decoration;
  }

  &:disabled,
  &.disabled {
    color: $btn-link-disabled-color;
  }
}

.btn-sm {
  @include button-size(
    $btn-padding-y-sm,
    $btn-padding-x-sm,
    $btn-font-size-sm,
    $btn-border-radius-sm
  );
}


/// Button rendered with a cross (x) SVG
///
.btn-close {
  background-repeat: no-repeat;
  background-position: center;
  background-size: 2rem;
  background-image: url(icon-cross($black, .9));

  &:hover {
    opacity: .5;
  }
}
