@use "../../variables/index" as *;

@use "sass:math";
@use "sass:string";
// Framework grid generation

// Used only by Bootstrap to generate the correct number of grid classes given any value of `$grid-columns`.

// Helper mixins for make-grid-columns
@mixin col-helper($index, $list: null) {
  @if $list == null {
    // initial - one parameter
    $item: string.unquote('.col-xs-#{$index}, .col-sm-#{$index}, .col-md-#{$index}, .col-lg-#{$index}');
    @include col-helper(($index + 1), $item);
  } @else if $index <= $grid-columns {
    // general
    $item: string.unquote('.col-xs-#{$index}, .col-sm-#{$index}, .col-md-#{$index}, .col-lg-#{$index}');
    @include col-helper(($index + 1), string.unquote('#{$list}, #{$item}'));
  } @else {
    // terminal
    #{$list} {
      position: relative;
      // Prevent columns from collapsing when empty
      min-height: 1px;
      // Inner gutter via padding
      padding-left: calc($grid-gutter-width * 0.5);
      padding-right: calc($grid-gutter-width * 0.5);
    }
  }
}

@mixin make-grid-columns() {
  // Common styles for all sizes of grid columns, widths 1-12
  @include col-helper(1); // kickstart it
}

// Helper mixins for float-grid-columns
@mixin float-col-helper($class, $index, $list: null) {
  @if $list == null {
    // initial - one parameter
    $item: string.unquote('.col-#{$class}-#{$index}');
    @include float-col-helper($class, ($index + 1), $item);
  } @else if $index <= $grid-columns {
    // general
    $item: string.unquote('.col-#{$class}-#{$index}');
    @include float-col-helper($class, ($index + 1), string.unquote('#{$list}, #{$item}'));
  } @else {
    // terminal
    #{$list} {
      float: left;
    }
  }
}

@mixin float-grid-columns($class) {
  @include float-col-helper($class, 1); // kickstart it
}

@mixin calc-grid-column($index, $class, $type) {
  @if $type == width and $index > 0 {
    .col-#{$class}-#{$index} {
      width: math.percentage(math.div($index, $grid-columns));
    }
  } @else if $type == push and $index > 0 {
    .col-#{$class}-push-#{$index} {
      left: math.percentage(math.div($index, $grid-columns));
    }
  } @else if $type == push and $index == 0 {
    .col-#{$class}-push-0 {
      left: auto;
    }
  } @else if $type == pull and $index > 0 {
    .col-#{$class}-pull-#{$index} {
      right: math.percentage(math.div($index, $grid-columns));
    }
  } @else if $type == pull and $index == 0 {
    .col-#{$class}-pull-0 {
      right: auto;
    }
  } @else if $type == offset {
    .col-#{$class}-offset-#{$index} {
      margin-left: math.percentage(math.div($index, $grid-columns));
    }
  }
}

// Basic looping in SASS
@mixin loop-grid-columns($index, $class, $type) {
  @if $index >= 0 {
    @include calc-grid-column($index, $class, $type);
    // next iteration
    @include loop-grid-columns(($index - 1), $class, $type);
  }
}

// Create grid for specific class
@mixin make-grid($class) {
  @include float-grid-columns($class);
  @include loop-grid-columns($grid-columns, $class, width);
  // Removed unused Bootstrap 3 grid utilities (push/pull/offset) - verified 0 usages
  // @include loop-grid-columns($grid-columns, $class, pull);
  // @include loop-grid-columns($grid-columns, $class, push);
  @include loop-grid-columns($grid-columns, $class, offset);
}
