@use 'sass:math';
@use 'sass:meta';
@use '../../functions/is-percentage' as *;
@use '../../functions/strip-unit' as *;

/// Takes a given value in either 0-1 decimal or 2-100 integer scale and applies
/// that level of opacity.
///
/// @param {Number} $opacity - The opacity level. Can take a decimal
/// beteen 0 and 1 inclusive, or either a unitless integer or a percentage
/// between 1 exclusive and 100 inclusive.
///
/// @group Utilities
///
/// @throw Invalid $opacity data type.
/// @throw $opacity out of range.
@mixin opacity($opacity) {
  @if not math.is-unitless($opacity) and math.unit($opacity) != '%' {
    @error 'Invalid opacity value of `#{meta.inspect($opacity)}` passed to' +
        'the [ opacity() ] mixin. $opacity must be a decimal between 0 and 1 ' +
        'inclusive or a percentage value between 0% and 100% inclusive.';
  }

  @if $opacity > 1 or math.unit($opacity) == '%' {
    @if $opacity > 100 {
      @warn 'The $opacity was set to `#{meta.inspect($opacity)}` with the ' +
          '[ opacity() ] mixin. The value has been changed to the max of 1.';

      $opacity: 100;
    }
    @if math.unit($opacity == '%') {
      $opacity: math.div($opacity, 100%);
    } @else {
      $opacity: math.div($opacity, 100);
    }
  }

  opacity: $opacity;
}
