// ==========================================================================
// TOOLS - #SPACING
// ==========================================================================

// Single point spacing
// ==========================================================================

//
// Returns measurement corresponding to the spacing point requested.
//
// @param {Number} $spacing-point - Point on the spacing scale (set in `settings/_spacing.sccs`)
//
// @returns {String} Spacing Measurement eg. 8px
//
// @example scss
//  .foo {
//    padding: dfe-spacing(5);
//    top: dfe-spacing(2) !important; // if `!important` is required
//   }
//
// Original code taken from GDS (Government Digital Service)
// https://github.com/alphagov/govuk-frontend
//

@function dfe-spacing($spacing-point) {

  $actual-input-type: type-of($spacing-point);
  @if $actual-input-type != 'number' {
    @error 'Expected a number (integer), but got a '
    + '#{$actual-input-type}.'; /* stylelint-disable-line indentation */
  }

  @if not map-has-key($dfe-spacing-points, $spacing-point) {
    @error 'Unknown spacing variable `#{$spacing-point}`. Make sure you are using a point from the spacing scale in `_settings/spacing.scss`.';
  }

  @return map-get($dfe-spacing-points, $spacing-point);
}

// Responsive spacing
// ==========================================================================

//
// Adds responsive spacing (either padding or margin, depending on `$property`)
// by fetching a 'spacing map' from the responsive spacing scale, which defines
// different spacing values at different breakpoints.
//
// To generate responsive spacing, use 'dfe-responsive-margin' or
// 'dfe-responsive-padding' mixins
//
// @param {Number} $responsive-spacing-point - Point on the responsive spacing
//  scale, corresponds to a map of breakpoints and spacing values
// @param {String} $property - Property to add spacing to (e.g. 'margin')
// @param {String} $direction [all] - Direction to add spacing to
//  (`top`, `right`, `bottom`, `left`, `all`)
// @param {Boolean} $important [false] - Whether to mark as `!important`
// @param {Number} $adjustment [false] - Offset to adjust spacing by
//
// @example scss
//  .foo {
//    padding: dfe-spacing(5);
//    top: dfe-spacing(2) !important; // if `!important` is required
//   }
//
// 1. Make sure that the return value from `_settings/spacing.scss` is a map.
// 2. Loop through each breakpoint in the map
// 3. The 'null' breakpoint is for mobile.
//
// Original code taken from GDS (Government Digital Service)
// https://github.com/alphagov/govuk-frontend
//

@mixin _dfe-responsive-spacing($responsive-spacing-point, $property, $direction: 'all', $important: false, $adjustment: false) {

  $actual-input-type: type-of($responsive-spacing-point);
  @if $actual-input-type != 'number' {
    @error 'Expected a number (integer), but got a ' + '#{$actual-input-type}.';
  }

  @if not map-has-key($dfe-spacing-responsive-scale, $responsive-spacing-point) {
    @error 'Unknown spacing point `#{$responsive-spacing-point}`. Make sure you are using a point from the '
    + 'responsive spacing scale in `_settings/spacing.scss`.'; /* stylelint-disable-line indentation */
  }

  $scale-map: map-get($dfe-spacing-responsive-scale, $responsive-spacing-point); // [1] //
  $actual-map-type: type-of($scale-map);
  @if $actual-map-type != 'map' {
    @error 'Expected a number (integer), but got a '
    + '#{$actual-map-type}. Make sure you are using a map to set the responsive spacing in `_settings/spacing.scss`)'; /* stylelint-disable-line indentation */
  }

  @each $breakpoint, $breakpoint-value in $scale-map { // [2] //

    @if ($adjustment) {
      $breakpoint-value: $breakpoint-value + $adjustment;
    }

    @if $breakpoint == null { // [3] //

      @if $direction == all {
        #{$property}: $breakpoint-value iff($important, !important);
      } @else {
        #{$property}-#{$direction}: $breakpoint-value iff($important, !important);
      }
    } @else {
      @include govuk-media-query($from: $breakpoint) {
        @if $direction == all {
          #{$property}: $breakpoint-value iff($important, !important);
        } @else {
          #{$property}-#{$direction}: $breakpoint-value iff($important, !important);
        }
      }
    }
  }
}

// Responsive margin
// ==========================================================================

//
// Adds responsive margin by fetching a 'spacing map' from the responsive
// spacing scale, which defines different spacing values at different
// breakpoints. Wrapper for the `_dfe-responsive-spacing` mixin.
//
// @see {mixin} _dfe-responsive-spacing
//
// @param {Number} $responsive-spacing-point - Point on the responsive spacing
// scale, corresponds to a map of breakpoints and spacing values
// @param {String} $direction [all] - Direction to add spacing to
//   (`top`, `right`, `bottom`, `left`, `all`)
// @param {Boolean} $important [false] - Whether to mark as `!important`
// @param {Number} $adjustment [false] - Offset to adjust spacing by
//
// @example scss
//   .foo {
//      @include dfe-responsive-margin(6, 'left', $adjustment: 1px);
//   }
//
// Original code taken from GDS (Government Digital Service)
// https://github.com/alphagov/govuk-frontend
//

@mixin dfe-responsive-margin($responsive-spacing-point, $direction: 'all', $important: false, $adjustment: false) {
  @include _dfe-responsive-spacing($responsive-spacing-point, 'margin', $direction, $important, $adjustment);
}

// Responsive padding
// ==========================================================================

//
// Adds responsive padding by fetching a 'spacing map' from the responsive
// spacing scale, which defines different spacing values at different
// breakpoints. Wrapper for the `_dfe-responsive-spacing` mixin.
//
// @see {mixin} _dfe-responsive-spacing
//
// @param {Number} $responsive-spacing-point - Point on the responsive spacing
//   scale, corresponds to a map of breakpoints and spacing values
// @param {String} $direction [all] - Direction to add spacing to
//   (`top`, `right`, `bottom`, `left`, `all`)
// @param {Boolean} $important [false] - Whether to mark as `!important`
// @param {Number} $adjustment [false] - Offset to adjust spacing
//
// @example scss
//   .foo {
//      @include dfe-responsive-padding(6, 'left', $adjustment: 1px);
//   }
//
// Original code taken from GDS (Government Digital Service)
// https://github.com/alphagov/govuk-frontend
//

@mixin dfe-responsive-padding($responsive-spacing-point, $direction: 'all', $important: false, $adjustment: false) {
  @include _dfe-responsive-spacing($responsive-spacing-point, 'padding', $direction, $important, $adjustment);
}
