/////////////////////////////////////////////////////
///////////
//////////
///////// GRID MIXINS
////////
///////
////////////////////////////////////////////////////

@use "sass:math";
@import "../../settings/includes/grid";

@mixin make-grid($class) {
  @include loop-columns($grid-columns, $class, width);
}

@mixin calc-columns($index, $class, $type) {
  @if $type == width and $index > 0 {
    .#{$class}#{$index} {
      width: percentage(math.div($index, $grid-columns));
    }
  }
}

@mixin loop-columns($index, $class, $type) {
  @if $index >= 0 {
    @include calc-columns($index, $class, $type);

    // next iteration
    @include loop-columns(($index - 1), $class, $type);
  }
}

// defaults for auto cols
@mixin setup-auto-cols() {
  @include flex(1, 0, 0); // a unit on last value is required by IE10-11
}

// --------------------------------------------------
// order class generation mixins
// --------------------------------------------------

@mixin order($order: 0) {
  -ms-flex-order: $order;
  -webkit-order: $order;
  order: $order;
}

@mixin make-order-helpers() {
  @include loop-order-helpers($grid-columns);
}

@mixin loop-order-helpers($index, $breakpoint: null) {
  @if $index >= 0 {
    .order#{$breakpoint}-#{$index} {
      @include order($index);
    }

    // next iteration
    @include loop-order-helpers(($index - 1), $breakpoint);
  }
}

// --------------------------------------------------
// offset class generation mixins
// --------------------------------------------------
@mixin offset($index: 0) {
  $offset: math.div($index, $grid-columns);

  // convert to percentage only if not zero
  @if $offset != 0 {
    $offset: percentage($offset);
  }

  margin-left: $offset;
}

@mixin make-offset-helpers() {
  @include loop-offset-helpers($grid-columns - 1);
}

@mixin loop-offset-helpers($index, $breakpoint: null) {
  @if $index > 0 and $breakpoint == null {
    .offset#{$breakpoint}-#{$index} {
      @include offset($index);
    }

    // next iteration
    @include loop-offset-helpers(($index - 1), $breakpoint);
  }

  @else if $index >= 0 and $breakpoint != null {
    .offset#{$breakpoint}-#{$index} {
      @include offset($index);
    }

    // next iteration
    @include loop-offset-helpers(($index - 1), $breakpoint);
  }
}

// --------------------------------------------------
// reflex modifier mixins
// --------------------------------------------------

//
// display
// ---

@mixin display-flex() {
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
}

//
// flex
// ---

@mixin flex($grow: 0, $shrink: 1, $basis: auto) {
  @if $basis == 0 {
    -ms-flex: $grow $shrink auto; // ie10 needs auto
  }

  @else {
    -ms-flex: $grow $shrink $basis;
  }

  -webkit-flex: $grow $shrink $basis;
  flex: $grow $shrink $basis;
}

@mixin flex-grow($grow:1) {
  -ms-flex-positive: $grow;
  -webkit-flex-grow: $grow;
  flex-grow: $grow;
}

@mixin flex-shrink($shrink:1) {
  -ms-flex-negative: $shrink;
  -webkit-flex-shrink: $shrink;
  flex-shrink: $shrink;
}

@mixin flex-basis($basis:auto) {
  -ms-flex-preferred-size: $basis;
  -webkit-flex-basis: $basis;
  flex-basis: $basis;
}

@mixin flex-flow($direction: row, $wrap: nowrap) {
  -ms-flex-wrap: $wrap;
  -ms-flex-direction: $direction;
  -webkit-flex-flow: $direction $wrap;
  flex-flow: $direction $wrap;
}

@mixin flex-wrap($wrap: wrap) {
  -ms-flex-wrap: $wrap;
  -webkit-flex-wrap: $wrap;
  flex-wrap: $wrap;
}

@mixin flex-direction($direction: row) {
  -ms-flex-direction: $direction;
  -webkit-flex-direction: $direction;
  flex-direction: $direction;
}

//
// align
// ---

@mixin align-items($align: stretch) {
  -webkit-align-items: $align;
  align-items: $align;
}

@mixin align-self($align: stretch) {
  -webkit-align-self: $align;
  align-self: $align;
}

@mixin align-content($align: stretch) {
  -webkit-align-content: $align;
  align-content: $align;
}

//
// justify-content
//
// Uses "text-align" for the fallback inline-block grid
// "text-align" is globally supported and works on all rows except the last
// "text-align-last", where supported, handles the last line (and, happily, grids with only one row)
// ---

@mixin justify-content-start() {
  -ms-flex-pack: start;
  -webkit-justify-content: flex-start;
  justify-content: flex-start;
}

@mixin justify-content-end() {
  -ms-flex-pack: end;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
}

@mixin justify-content-center() {
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
}

@mixin justify-content-space-between() {
  -ms-flex-pack: justify;
  -webkit-justify-content: space-between;
  justify-content: space-between;
}

@mixin justify-content-space-around() {
  -ms-flex-pack: distribute;
  -webkit-justify-content: space-around;
  justify-content: space-around;
}

// --------------------------------------------------
// other mixins
// --------------------------------------------------

@mixin box-sizing($box-model) {
  -webkit-box-sizing: $box-model;
  -moz-box-sizing: $box-model;
  box-sizing: $box-model;
}

@mixin responsive-visibility-helpers() {
  @if $visibility-helpers == true {
    .u-hidden-xxs {
      @media (max-width: $breakpoint-xxs-max) {
        display: none;
      }
    }

    .u-hidden-xs {
      @media (min-width: $breakpoint-xs) and (max-width: $breakpoint-xs-max) {
        display: none;
      }
    }

    .u-hidden-sm {
      @media (min-width: $breakpoint-sm) and (max-width: $breakpoint-sm-max) {
        display: none;
      }
    }

    .u-hidden-md {
      @media (min-width: $breakpoint-md) and (max-width: $breakpoint-md-max) {
        display: none;
      }
    }

    .u-hidden-lg {
      @media (min-width: $breakpoint-lg) and (max-width: $breakpoint-lg-max) {
        display: none;
      }
    }

    .u-hidden-xlg {
      @media (min-width: $breakpoint-xlg) {
        display: none;
      }
    }
  }
}
