@use "sass:map";
@use "../variables";
@use "breakpoints";
@use "grid";

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

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

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

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

        .col#{$infix},
        .col#{$infix}-auto {
            @extend %grid-column;
        }

        @include breakpoints.media-breakpoint-up($breakpoint, $breakpoints) {
            // Provide basic `.col-{bp}` classes for equal-width flexbox columns
            .col#{$infix} {
                flex-basis: 0;
                flex-grow: 1;
                min-width: 0; // See https://github.com/twbs/bootstrap/issues/25410
                max-width: 100%;
            }

            @if variables.$grid-row-columns > 0 {
                @for $i from 1 through variables.$grid-row-columns {
                    .row-cols#{$infix}-#{$i} {
                        @include grid.row-cols($i);
                    }
                }
            }

            .col#{$infix}-auto {
                @include grid.make-col-auto();
            }

            @if $columns > 0 {
                @for $i from 1 through $columns {
                    .col#{$infix}-#{$i} {
                        @include grid.make-col($i, $columns);
                    }
                }
            }

            .order#{$infix}-first {
                order: -1;
            }

            .order#{$infix}-last {
                order: $columns + 1;
            }

            @for $i from 0 through $columns {
                .order#{$infix}-#{$i} {
                    order: $i;
                }
            }

            @if $columns > 0 {
                // `$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 grid.make-col-offset($i, $columns);
                        }
                    }
                }
            }
        }
    }
}
