@use 'sass:map';

$theme: null !default;

@use 'breakpoints' with (
  $theme: $theme
);
@use 'grid' with (
  $theme: $theme
);

// Framework grid generation

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

/* stylelint-disable max-nesting-depth */

@mixin make-grid-columns(
  $columns: map.get($theme, 'grid', 'columns'),
  $gutters: map.get($theme, 'grid-gutter-width'),
  $breakpoints: map.get($theme, 'breakpoint')
) {
  // Common properties for all breakpoints
  %grid-column {
    box-sizing: border-box;
    min-height: 1px; // Prevent columns from collapsing when empty
    position: relative;
    width: 100%;

    @include grid.make-gutters($gutters);
  }

  @each $breakpoint in map.keys($breakpoints) {
    $infix: breakpoints.infix($breakpoint, $breakpoints);

    // Allow columns to stretch full width below their breakpoints
    @for $i from 1 through $columns {
      .ecl-col#{$infix}-#{$i} {
        @extend %grid-column;
      }
    }

    /* stylelint-disable-next-line order/order */
    @include breakpoints.up($breakpoint, $breakpoints) {
      @for $i from 1 through $columns {
        .ecl-col#{$infix}-#{$i} {
          @include grid.make-col($i, $columns);
        }
      }

      @each $modifier in (pull, push) {
        @for $i from 0 through $columns {
          .ecl-#{$modifier}#{$infix}-#{$i} {
            @include grid.make-col-modifier($modifier, $i, $columns);
          }
        }
      }

      // `$columns - 1` because offsetting by the width of an entire row isn't possible
      @for $i from 0 through ($columns - 1) {
        @if not($infix == '' and $i == 0) {
          // Avoid emitting useless .offset-xs-0
          .ecl-offset#{$infix}-#{$i} {
            @include grid.make-col-modifier(offset, $i, $columns);
          }
        }
      }
    }
  }
}
