////
/// @group helpers
////

/// Map of grid column widths
///
/// @type Map
/// @access private

$_govuk-grid-widths: (
  one-quarter: 25%,
  one-third: 33.3333%,
  one-half: 50%,
  two-thirds: 66.6666%,
  three-quarters: 75%,
  full: 100%
) !default;

/// Grid width percentage
///
/// @param {String} $key - Name of grid width (e.g. two-thirds)
/// @return {Number} Percentage width
/// @throw if `$key` is not a valid grid width
/// @access public

@function grid-width($key) {
  @if map-has-key($_govuk-grid-widths, $key) {
    @return map-get($_govuk-grid-widths, $key);
  }

  @error "Unknown grid width `#{$key}`";
}

/// Generate grid row styles
///
/// Creates a grid row class with a standardised margin.
///
/// @param {String} $class [govuk-grid-row] CSS class name
///
/// @example scss - Default
///   @include govuk-grid-row;
///
/// @example scss - Customising the class name
///   @include govuk-grid-row("app-grid");
///
/// @access public

@mixin govuk-grid-row($class: "govuk-grid-row") {
  .#{$class} {
    @include govuk-clearfix;
    margin-right: - ($govuk-gutter-half);
    margin-left: - ($govuk-gutter-half);
  }
}

/// Generate grid column styles
///
/// Creates a cross browser grid column with a class of '.govuk-grid-column' by
/// default, and a standardised gutter between the columns.
///
/// Common widths are predefined above as keywords in the `$grid-widths` map.
///
/// By default their width changes from 100% to specified width at the 'tablet'
/// breakpoint, but that can be configured to be any other breakpoint by using
/// the `$at` parameter.
///
/// @param {String} $class [govuk-grid-column] CSS class name
/// @param {String} $width [full] one-quarter | one-third | one-half | two-third | three-quarters | full
/// @param {String} $float [left] left | right
/// @param {String} $at [tablet] - mobile | tablet | desktop | any custom breakpoint in px or em
///
/// @example scss - Default
///   @include govuk-grid-column(two-thirds)
///
/// @example scss - Customising the class name
///   @include govuk-grid-column(one-half, $class: "test-column");
///
/// @example scss - Customising the breakpoint where width percentage is applied
///   @include govuk-grid-column(one-half, $at: desktop);
///
/// @example scss - Customising the float direction
///   @include govuk-grid-column(one-half, $float: right);
///
/// @access public

@mixin govuk-grid-column($width: full, $float: left, $at: tablet, $class: "govuk-grid-column") {

  .#{$class}-#{$width} {
    box-sizing: border-box;
    @if $at != desktop {
      width: 100%;
    }
    padding: 0 $govuk-gutter-half;
    @include govuk-media-query($from: $at) {
      width: grid-width($width);
      float: $float;
    }
  }
}
