// Form control focus state
//
// Generate a customized focus state and for any input with the specified color,
// which defaults to the `$input-focus-border-color` variable.
//
// We highly encourage you to not customize the default value, but instead use
// this to tweak colors on an as-needed basis. This aesthetic change is based on
// WebKit's default styles, but applicable to a wider range of browsers. Its
// usability and accessibility should be taken into account with any change.
//
// Example usage: change the default blue border and shadow to white for better
// contrast against a dark gray background.
@mixin form-control-focus($ignore-warning: false) {
  @include deprecate("The `form-control-focus()` mixin", "v4.4.0", "v5", $ignore-warning);

  &:focus {
    background-color: $input-focus-bg;
    border-color: $input-focus-border-color;
    color: $input-focus-color;
    outline: 0;
    @if $enable-shadows {
      @include box-shadow($input-box-shadow, $input-focus-box-shadow);
    } @else {
      // Avoid using mixin so we can pass custom focus shadow properly
      box-shadow: $input-focus-box-shadow;
    }
  }
}

// This mixin uses an `if()` technique to be compatible with Dart Sass
// See https://github.com/sass/sass/issues/1873#issuecomment-152293725 for more details
@mixin form-validation-state-selector($state) {
  @if ($state == "valid" or $state == "invalid") {
    .was-validated #{if(&, "&", "")}:#{$state},
    #{if(&, "&", "")}.is-#{$state} {
      @content;
    }
  } @else {
    #{if(&, "&", "")}.is-#{$state} {
      @content;
    }
  }
}

@mixin form-validation-state($state, $color, $icon) {
  @include form-validation-state-selector($state) {
    ~ .#{$state}-feedback,
    ~ .#{$state}-tooltip {
      display: block;
    }
  }
  .#{$state}-feedback {
    @include font-size($form-feedback-font-size);
    color: var(#{$color}-color);
    display: none;
    margin-top: $form-feedback-margin-top;
    width: 100%;
  }

  .#{$state}-tooltip {
    @include font-size($form-feedback-tooltip-font-size);
    @include border-radius($form-feedback-tooltip-border-radius);
    background-color: hsla(var(#{$color}-color-h),
    var(#{$color}-color-s),
    var(#{$color}-color-l),
    $form-feedback-tooltip-opacity);
    color: color-yiq($color);
    display: none;
    left: 0;
    line-height: $form-feedback-tooltip-line-height;
    margin-top: 0.1rem;
    max-width: 100%; // Contain to parent when possible
    padding: $form-feedback-tooltip-padding-y $form-feedback-tooltip-padding-x;
    position: absolute;
    top: 100%;
    z-index: 5;
  }

  .form-control {
    @include form-validation-state-selector($state) {
      border-color: normal-color($color);

      &:focus {
        border-color: normal-color($color);
        box-shadow: 0 0 0 $input-focus-width set-opacity($color, 0.25);
      }

      @if $enable-validation-icons {
        background-image: escape-svg($icon);
        background-position: right $input-height-inner-quarter center;
        background-repeat: no-repeat;
        background-size: $input-height-inner-half $input-height-inner-half;
        padding-right: $input-height-inner;
      }
    }
  }

  // stylelint-disable-next-line selector-no-qualifying-type
  textarea.form-control {
    @include form-validation-state-selector($state) {
      @if $enable-validation-icons {
        background-position: top $input-height-inner-quarter right $input-height-inner-quarter;
        padding-right: $input-height-inner;
      }
    }
  }

  .custom-select {
    @include form-validation-state-selector($state) {
      border-color: normal-color($color);

      &:focus {
        border-color: normal-color($color);
        box-shadow: 0 0 0 $input-focus-width set-opacity($color, 0.25);
      }

      @if $enable-validation-icons {
        background: $custom-select-background,
          escape-svg($icon) $custom-select-bg no-repeat $custom-select-feedback-icon-position /
          $custom-select-feedback-icon-size;
        padding-right: $custom-select-feedback-icon-padding-right;
      }
    }
  }

  .form-check-input {
    @include form-validation-state-selector($state) {
      ~ .form-check-label {
        color: normal-color($color);
      }

      ~ .#{$state}-feedback,
      ~ .#{$state}-tooltip {
        display: block;
      }
    }
  }

  .custom-control-input {
    @include form-validation-state-selector($state) {
      ~ .custom-control-label {
        color: normal-color($color);

        &::before {
          border-color: normal-color($color);
        }
      }

      &:checked {
        ~ .custom-control-label::before {
          @include gradient-bg(normal-color($color));
          border-color: normal-color($color);
        }
      }

      &:focus {
        ~ .custom-control-label::before {
          box-shadow: 0 0 0 $input-focus-width set-opacity($color, 0.25);
        }

        &:not(:checked) ~ .custom-control-label::before {
          border-color: normal-color($color);
        }
      }
    }
  }

  // custom file
  .custom-file-input {
    @include form-validation-state-selector($state) {
      ~ .custom-file-label {
        border-color: normal-color($color);
      }

      &:focus {
        ~ .custom-file-label {
          border-color: var(#{$color}-color);
          box-shadow: 0 0 0 $input-focus-width set-opacity($color, 0.25);
        }
      }
    }
  }
}
