// methods for preserving the vertical rhythm by positioning text on the grid baselines
//
// when a box does not need extra top/bottom paddings
// the baseline-paddings mixin should be used instead of these functions
//
// but in cases where actual extra padding is needed
// these methods can be used directly
//
// e.g.
// for a box that needs an extra 24px padding on top of the baseline padding:
//
// padding-top: baseline-top-padding( 16px, 24px ) + steps( 2 )
//

@function baseline-top-padding( $font-size-in-px, $line-height-in-px, $extra-top-padding-in-px: 0 )
{
    @return baseline-padding( 'top', $font-size-in-px, $line-height-in-px, $extra-top-padding-in-px );
}

@function baseline-bottom-padding( $font-size-in-px, $line-height-in-px, $extra-bottom-padding-in-px: 0 )
{
    @return baseline-padding( 'bottom', $font-size-in-px, $line-height-in-px, $extra-bottom-padding-in-px );
}

@function baseline-padding( $type, $font-size-in-px, $line-height-in-px, $extra-padding-in-px: 0 )
{
    // to ensure that the baseline of the text always sits on the baseline grid
    // extra padding needs to be added to the top of each content box
    // the amount of padding needed depends on the font size and line height
    // and also on the properties of the font itself
    // therefore it has to be manually defined for each font-size / line-height combination
    // to avoid repeating this task, a map of needed paddings is defined here
    // that returns the needed padding for each combination

    // to ensure that the height of the box always stays a multiple of 12px
    // extra bottom padding is also needed and can be calculated
    // by subtracting the top padding from 12px

    // thus this function accepts font size and line height as arguments
    // as well as $type, specifying whether to return the top or the bottom padding

    // this function should not be used directly but via shorthand functions above

    // for each supported font size / line height combo $paddings gets sets to a list
    // the first item is the needed top padding
    // the second item is the bottom padding

    // the $padding-map has font-sizes as first level keys
    // and line-heights as second level keys
    // every font-size/line-height combination used in the site
    // must have an entry in this map

    // keys are prefixed with "fs" and "lh" respectively
    // so that the incoming values are always cast to strings
    // otherwise map-get( ( 30px: (...something...) ), steps(2.5) ) does not work
    // even though steps(2.5) evaluates to 30px and the 30px key is there

    $padding-map: (
        fs41px: (
            lh50px: (2px, 8px),
        ),
        fs38px: (
            lh50px: (4px, 6px),
        ),
        fs32px: (
            lh50px: (6px, 4px),
        ),
        fs28px: (
            lh40px: (2px, 8px),
        ),
        fs26px: (
            lh35px: (5px, 5px),
        ),
        fs24px: (
            lh40px: (2px, 8px),
        ),
        fs22px: (
            lh30px: (8px, 2px),
        ),
        fs20px: (
            lh25px: (2px, 8px),
        ),
        fs18px: (
            lh30px: (9px, 1px),
        ),
        fs16px: (
            lh30px: (0, 0),
            lh25px: (2px, 8px),
            lh20px: (0, 0),
        ),
        fs14px: (
            lh30px: (0, 0),
            lh25px: (3px, 7px),
            lh20px: (5px, 5px),
        ),
        fs12px: (
            lh20px: (6px, 4px),
        ),
    );

    $font-size-paddings: map-get($padding-map, "fs" + $font-size-in-px);
    @if ($font-size-paddings == null)
    {
        @error "Unrecognized font size / line height combination: #{$font-size-in-px} / #{$line-height-in-px}.";
    }

    $paddings: map-get( $font-size-paddings, "lh" + $line-height-in-px );
    @if ($paddings == null)
    {
        @error "Unrecognized font size / line height combination: #{$font-size-in-px} / #{$line-height-in-px}.";
    }

    $padding: 1000px;
    @if ($type == "top")
    {
        $padding: nth( $paddings, 1);
    }
    @else if ($type == "bottom")
    {
        $padding: nth( $paddings, 2);
    }
    @else
    {
        @error "Unrecognized baseline padding type: #{$type}.";
    }

    $padding: $padding + $extra-padding-in-px;

    @return $padding;
}

