/**
 * Generate size classes
 * Borrowed from: https://github.com/necolas/griddle
 */

@mixin width-classes(
  $units,
  $media-prefix: false,
  $modifier: false,
  $use-important: true,
  $name: 'size',
  $property: 'width'
) {

  $media-prefix: if($media-prefix, '#{$media-prefix}-', '');
  $modifier: if($modifier, '--#{$modifier}', '');
  $use-important: unquote(if($use-important, ' !important', ''));

  @each $n in $units {

    // Avoid creating rules like `.unit-12-12 {}`
    $x: $n - 1;

    @for $i from 1 through $x {

      // Initialize variables
      $i-r: 0;
      $n-r: 0;

      // Find the greatest common factor
      $g: gcd($i, $n);

      @if $g > 1 {

        // Reduced value of $i
        $i-r: $i/$g;

        // Reduced value of $n
        $n-r: $n/$g;
      }

      // Check if the reduced value of $n was also supplied in the list
      // of units to be built
      $canreduce: sl-contain($units, $n-r);

      // Create units based on fractions
      .u-#{$media-prefix}#{$name}#{$i}of#{$n}#{$modifier} {

        // If this unit can be reduced then extend the previous rule
        @if $canreduce {
          @extend .u-#{$media-prefix}#{$name}#{$i-r}of#{$n-r}#{$modifier} !optional;
        }

        // Otherwise create a new % width
        @else {
          #{$property}: percentage($i / $n) + $use-important;
        }
      }
    }
  }
}
