// ==========================================================================
// Typography mixins
// ==========================================================================

// REM
// @include rem(margin, $font-size-base);
@use 'sass:math';

@mixin rem($property, $sizeValue: $font-size-base) {
    $unitlessSizeValue: strip-units($sizeValue);
    $remValue: math.div($unitlessSizeValue, $font-size-base);

    #{$property}: ceil($unitlessSizeValue) + px;
    #{$property}: $remValue + rem;
}

// EM
// @include em(margin, $font-size-base);
@mixin em($property, $sizeValue: $font-size-base) {
    #{$property}: math.div(strip-units($sizeValue), $font-size-base) + em;
}

@mixin font-size($key: 'body-s', $line-height: true, $scale: 'default', $relativeToParent: false) {
    // if the $key passed in is actually just a font-size
    // then output it (may want to instead force a sass error saying to use the type map?)
    @if type-of($key) == 'number' {
        @if $relativeToParent == true {
            @include em(font-size, $key);
        } @else {
            @include rem(font-size, $key);
        }
    } @else if map-has-key($type, $key) { // else try and find the value in our type map
        $key-map: map-get($type, $key);
        $font-list: map-get($key-map, $scale);

        // If $size-map is a list, include
        // both font-size and line-height
        @if type-of($font-list) == 'list' {
            $font-size: nth($font-list, 1);

            @if $relativeToParent == true {
                @include em(font-size, $font-size);
            } @else {
                @include rem(font-size, $font-size);
            }

            @if length($font-list) > 1 and $line-height == true {
                $line-height: nth($font-list, 2);

                line-height: line-height($font-size, $line-height);
            }
        } @else {
            @if $relativeToParent == true {
                @include em(font-size, $font-list);
            } @else {
                @include rem(font-size, $font-list);
            }
        }
    } @else {
        @warn 'Unknown `#{$key}` in $type.';
    }
}
