@use 'sass:meta';

/// Easily generates the appropriate typographic properties for the given values,
/// following the W3C specifications.
/// @link https://developer.mozilla.org/en-US/docs/Web/CSS/font
///
/// @param {Number} $size [null] - The font-size value.
/// @param {Number | String} $weight [null] - The font-weight value.
/// @param {String} $style [null] - The font-style value.
/// @param {String} $variant [null] - The font-variant value.
/// @param {Number} $height [null] - The line-height value.
/// @param {String | List} $family [null] - The font-family value.
/// @param {Number} $spacing [null] - The letter-spacing value.
/// @param {String} $stretch [null] - The font-stretch value.
/// @param {String} $kern [null] - The font-kerning value.
///
/// @group Utilities
/// @throw Error, invalid $kern keyword value for font-kerning property.
@mixin font(
  $size: null,
  $weight: null,
  $style: null,
  $variant: null,
  $height: null,
  $family: null,
  $stretch: null,
  $spacing: null,
  $kern: null
) {
  $stretch-no-keyword: false;

  @if $stretch and
    $stretch !=
    ultra-condensed and
    $stretch !=
    extra-condensed and
    $stretch !=
    condensed and
    $stretch !=
    semi-condensed and
    $stretch !=
    normal and
    $stretch !=
    semi-expanded and
    $stretch !=
    expanded and
    $stretch !=
    extra-expanded and
    $stretch !=
    ultra-expanded
  {
    // Stretch must be a keyword to be included in the font shorthand
    $stretch-no-keyword: true;
  }
  @if $kern and $kern != auto and $kern != normal and $kern != none {
    @warn 'You have tried giving an incorrect value of `#{meta.inspect($kern)}` ' +
      'for the font-kerning property in the [ font() ] mixin. The value was ' +
      'reset to null and no font-kerning will be applied. Please check ' +
      'your stylesheet. The value of $kern must be one of the following: ' +
      '[auto, normal, none].';

    $kern: null;
  }
  @if not $family or not $size {
    @if $family {
      font-family: #{$family};
    }
    @if $kern {
      font-kerning: $kern;
    }
    @if $size {
      font-size: $size;
    }
    @if $stretch {
      font-stretch: #{$stretch};
    }
    @if $style {
      font-style: $style;
    }
    @if $variant {
      font-variant: $variant;
    }
    @if $weight {
      font-weight: #{$weight};
    }
    @if $spacing {
      letter-spacing: $spacing;
    }
    @if $height {
      line-height: $height;
    }
  } @else {
    @if $height {
      @if $variant and $variant != small-caps and $variant != normal {
        @if $stretch-no-keyword {
          font: $style $weight #{$size}/#{$height} $family;
          font-variant: $variant;
          font-stretch: $stretch;
        } @else {
          font: $style $weight $stretch #{$size}/#{$height} $family;
          font-variant: $variant;
        }
      } @else {
        @if $stretch-no-keyword {
          font: $style $variant $weight #{$size}/#{$height} $family;
          font-stretch: $stretch;
        } @else {
          font: $style $variant $weight $stretch #{$size}/#{$height} $family;
        }
      }
    } @else {
      @if $variant and $variant != small-caps and $variant != normal {
        @if $stretch-no-keyword {
          font: $style $weight $size $family;
          font-variant: $variant;
          font-stretch: $stretch;
        } @else {
          font: $style $weight $stretch $size $family;
          font-variant: $variant;
        }
      } @else {
        @if $stretch-no-keyword {
          font: $style $variant $weight $size $family;
          font-stretch: $stretch;
        } @else {
          font: $style $variant $weight $stretch $size $family;
        }
      }
    }
    @if $kern {
      font-kerning: $kern;
    }
    @if $spacing {
      letter-spacing: $spacing;
    }
  }
}
