@use "sass:map";
@use "sass:math";

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

@forward "./variables";
@forward "./breakpoints";

// Generate semantic grid columns with these mixins.
@mixin make-container($padding-x: $grid-container-padding-x) {
  & {
    width: 100%;
    padding-right: $padding-x;
    padding-left: $padding-x;
    margin-right: auto;
    margin-left: auto;
  }
}

// For each breakpoint, define the maximum width of the container in a media query
@mixin make-container-max-widths($max-widths: $grid-container-max-widths, $breakpoints: $grid-breakpoints) {
  @each $breakpoint, $container-max-width in $max-widths {
    @include media-breakpoint-up($breakpoint, $breakpoints) {
      max-width: $container-max-width;
    }
  }
}

@mixin make-row($gutter: $grid-gutter-width) {
  & {
    display: flex;
    flex-wrap: wrap;
    margin-right: math.div(-$gutter, 2);
    margin-left: math.div(-$gutter, 2);
  }
}

@mixin make-col-ready($gutter: $grid-gutter-width) {
  & {
    position: relative;
    // Prevent columns from becoming too narrow when at smaller grid tiers by
    // always setting `width: 100%;`. This works because we use `flex` values
    // later on to override this initial width.
    width: 100%;
    padding-right: math.div($gutter, 2);
    padding-left: math.div($gutter, 2);
  }
}

@mixin make-col($size, $columns: $grid-columns) {
  & {
    flex: 0 0 math.percentage(math.div($size, $columns));
    // Add a `max-width` to ensure content within each column does not blow out
    // the width of the column. Applies to IE10+ and Firefox. Chrome and Safari
    // do not appear to require this.
    max-width: math.percentage(math.div($size, $columns));
  }
}

@mixin make-col-offset($size, $columns: $grid-columns) {
  $num: math.div($size, $columns);
  & {
    //margin-left: if($num == 0, 0, math.percentage($num));
    @if $num == 0 {
      margin-left: 0;
    }
    @else {
      margin-left: math.percentage($num);
    }
  }
}

@mixin make-grid-columns($columns: $grid-columns, $gutter: $grid-gutter-width, $breakpoints: $grid-breakpoints) {
  // Common properties for all breakpoints
  %grid-column {
    position: relative;
    width: 100%;
    padding-right: math.div($gutter, 2);
    padding-left: math.div($gutter, 2);
  }

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

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

    @include media-breakpoint-up($breakpoint, $breakpoints) {
      // Provide basic `.brick-grid-col-{bp}` classes for equal-width flexbox columns
      .brick-grid-col#{$infix} {
        flex-basis: 0;
        flex-grow: 1;
        max-width: 100%;
      }
      .brick-grid-col#{$infix}-auto {
        flex: 0 0 auto;
        width: auto;
        max-width: 100%; // Reset earlier grid tiers
      }

      @for $i from 1 through $columns {
        .brick-grid-col#{$infix}-#{$i} {
          @include make-col($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-0
          .offset#{$infix}-#{$i} {
            @include make-col-offset($i, $columns);
          }
        }
      }
    }
  }
}
