// Copyright (c) 2016-2018 VMware, Inc. All Rights Reserved.
// This software is released under MIT license.
// The full license information can be found in LICENSE in the root directory of this project.

@mixin clr-make-grid-columns(
    $columns: $clr-grid-columns,
    $gutter: $clr-grid-gutter-width,
    $breakpoints: $clr-grid-breakpoints
) {
    // Common properties for all breakpoints
    %clr-grid-column {
        width: 100%;
        min-height: 1px; // Prevent columns from collapsing when empty
        padding-right: ($gutter / 2);
        padding-left: ($gutter / 2);
    }

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

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

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

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

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

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

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

            // `$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
                    .clr-offset#{$infix}-#{$i} {
                        @include clr-make-col-offset($i, $columns);
                    }
                }
            }
        }
    }
}
