////
/// @group grid
////

/// Grid mixin for applying to custom classes when creating a custom component
/// and creating 'semantic' (sensible) class names.
///
/// @param {boolean} $reverse [false] Whether the grid items flow the opposite way to their natural source order
/// @param {boolean} $gutterless [false] Whether the grid has no spacing
/// @param {boolean} $compact [false] Whether the grid has compact spacing
/// @param {boolean} $loose [false] Whether the grid has loose spacing
/// @param {boolean} $right [false] Whether to render items from the right instead of the left
/// @param {boolean} $center [false] Whether the grid's items are centrally aligned horizontally
/// @param {boolean} $debug [false] Whether to put a highlight round the grid for debugging
/// @require {mixin} grid-reverse
/// @require {mixin} grid-gutterless
/// @require {mixin} grid-right
/// @require {mixin} grid-center
/// @require {mixin} grid-debug
/// @example scss SCSS usage
///   .test {
///     @include grid($compact: true);
///
///     @include element('item') {
///       @include grid-item;
///       background: red;
///       width: 100%;
///     }
///
///     @include mq($from: md) {
///       @include element('item') {
///         background: green;
///         width: percentage(3 / 12);
///       }
///     }
///   }
/// @example html HTML usage
///   <div class="test">
///     <div class="test__item">item</div>
///   </div>
/// @since 0.1.0
@mixin grid($reverse: false, $gutterless: false, $compact: false, $loose: false, $right: false, $center: false, $debug: false) {
  letter-spacing: -.31em; // Remove whitespace caused by `inline-block` elements
  list-style: none; // Allow the grid system to be used on lists.
  margin: 0; // Remove any margins that might affect the grid system.
  margin-left: -$nice-grid-gutter; // Apply a negative `margin-left` to negate the columns’ gutters.
  padding: 0; // Remove any paddings that might affect the grid system.

  @if $reverse {
    @include grid-reverse;
  }

  @if $gutterless {
    @include grid-gutterless;
  }

  @if $compact {
    @include grid-compact;
  }

  @if $loose {
    @include grid-loose;
  }

  @if $right {
    @include grid-right;
  }

  @if $center {
    @include grid-center;
  }

  @if $debug {
    @include grid-debug;
  }
}

/// Reversed grids allow you to structure your source in the opposite order to how
/// your rendered layout will appear
/// @since 0.1.0
@mixin grid-reverse {
  direction: rtl;
  text-align: left;
}

/// Gutterless grids have all the properties of regular grids, minus any spacing
/// @see grid-item-gutterless
/// @since 0.1.0
@mixin grid-gutterless {
  margin-left: 0;
}

/// Compact grids have a smaller gutter
/// @see grid-item-compact
/// @since 0.2.10
@mixin grid-compact {
  margin-left: -$nice-grid-gutter-compact;
}

/// Loose grids have a smaller gutter
/// @see grid-item-compact
/// @since 0.2.10
@mixin grid-loose {
  margin-left: -$nice-grid-gutter-loose;
}

/// Align the entire grid to the right
/// @since 0.1.0
@mixin grid-right {
  text-align: right;
}

/// Centered grids align grid items centrally without needing to use push or pull classes
/// @since 0.1.0
@mixin grid-center {
  text-align: center;
}

/// Debug a grid by putting a highlight round it
/// @see grid-item-debug
/// @since 0.1.0
@mixin grid-debug {
  outline: 4px dashed red;
}

// Helper mixin for outputting breakpoints within a custom grid item.
@mixin _grid-item-breakpoint($bp, $bp-name) {
  @if $bp {
    @include mq($from: $bp-name) {
      @if is-integer($bp) {
        width: percentage($bp / 12);
      } @else if is-map($bp) {
        $cols: map-get($bp, cols);
        $push: map-get($bp, push);
        $pull: map-get($bp, pull);

        width: percentage($cols / 12);
        @if $push {
          left: percentage($push / 12);
        }
        @if $pull {
          right: percentage($pull / 12);
        }

      } @else {
        @error '$bp must be an integer or a map';
      }
    }
  }
}

/// Grid item mixin. Used in tandem with the `grid` mixin for creating semantic grid
/// classes within custom componenents.
/// @param {Number} $columns [12] The number of columns (1-12) to fill on mobile
/// @param {Number} $push [false] The number of columns (1-12) to push right
/// @param {Number} $push [false] The number of columns (1-12) to pull left
/// @param {boolean} $gutterless [false] Set to `true` to remove the spacing between grid items.
/// @param {boolean} $compact [false] Whether the grid has compact spacing
/// @param {boolean} $loose [false] Whether the grid has loose spacing
/// @param {boolean} $middle [false] Whether the grid item is vertically aligned to the middle
/// @param {boolean} $bottom [false] Whether the grid item is vertically aligned to the bottom
/// @param {boolean} $debug [false] Whether to put a highlight round the grid item for debugging
/// @param {Number | Map} $xs [false] The number of columns (1-12) to fill from xs breakpoint upwards, or a map with cols, push and pull
/// @param {Number | Map} $sm [false] The number of columns (1-12) to fill from sm breakpoint upwards, or a map with cols, push and pull
/// @param {Number | Map} $md [false] The number of columns (1-12) to fill from md breakpoint upwards, or a map with cols, push and pull
/// @param {Number | Map} $lg [false] The number of columns (1-12) to fill from lg breakpoint upwards, or a map with cols, push and pull
/// @param {Number | Map} $xl [false] The number of columns (1-12) to fill from xl breakpoint upwards, or a map with cols, push and pull
/// @require {mixin} grid-item-gutterless
/// @require {mixin} grid-item-compact
/// @require {mixin} grid-item-loose
/// @require {mixin} grid-item-middle
/// @require {mixin} grid-item-bottom
/// @require {mixin} grid-item-debug
/// @require mq <https://github.com/sass-mq/sass-mq>
/// @example scss
///   .test {
///     @include grid-item(12, $md: 8, $lg: (cols: 7, push: 1), $loose: true);
///   }
/// @since 0.1.0
@mixin grid-item($columns: 12,
  $push: false,
  $pull: false,
  $gutterless: false,
  $compact: false,
  $loose: false,
  $middle: false,
  $bottom: false,
  $debug: false,
  $xs: false,
  $sm: false,
  $md: false,
  $lg: false,
  $xl: false) {
  @include border-box;
  direction: ltr;
  display: inline-block;
  letter-spacing: normal;
  padding-left: $nice-grid-gutter;
  position: relative;
  text-align: left;
  vertical-align: top;
  width: percentage($columns / 12);
  word-spacing: normal;

  @if $push {
    left: percentage($push / 12);
  }

  @if $pull {
    right: percentage($pull / 12);
  }

  @if $gutterless {
    @include grid-item-gutterless;
  }

  @if $compact {
    @include grid-item-compact;
  }

  @if $loose {
    @include grid-item-loose;
  }

  @if $middle {
    @include grid-item-middle;
  }

  @if $bottom {
    @include grid-item-bottom;
  }

  @include _grid-item-breakpoint($xs, xs);
  @include _grid-item-breakpoint($sm, sm);
  @include _grid-item-breakpoint($md, md);
  @include _grid-item-breakpoint($lg, lg);
  @include _grid-item-breakpoint($xl, xl);
}

/// Gutterless grids have all the properties of regular grids, minus any spacing.
/// @since 0.1.0
@mixin grid-item-gutterless {
  padding-left: 0;
}

/// Compact grid item with a smaller gutter
/// @since 0.2.10
@mixin grid-item-compact {
  padding-left: $nice-grid-gutter-compact;
}

/// Loose grid item with a larger gutter
/// @since 0.2.10
@mixin grid-item-loose {
  padding-left: $nice-grid-gutter-loose;
}

/// Align grid cell vertically to the middle
/// @since 0.1.0
@mixin grid-item-middle {
  vertical-align: middle;
}

/// Align grid cell vertically to the bottom
/// @since 0.1.0
@mixin grid-item-bottom {
  vertical-align: bottom;
}

/// Debug a grid item by putting a highlight round it
/// @see grid-debug
/// @since 0.1.0
@mixin grid-item-debug {
  outline: 2px dashed blue;
}
