@use 'sass:math';
@use 'sass:meta';
@use '../settings/globals';

///*------------------------------------*\
//    # REM
//\*------------------------------------*/
$gel-tools-rem-enable--function: true !default;
$gel-tools-rem-enable--mixin: true !default;
$gel-tools-rem-enable--fallback: true !default;


// 'rem' is a Sass function that converts pixel values to rem values
// for whatever property is passed to it.
//
// @param {Int}     $value - The unit you want converting to a rem unit
// @param {Int}     $baseline (globals.$gel-base-font-size) - The base font size of the page
//
// @return {Int}    The supplied converted to a rem unit
//
// @author          Kaelig - https://github.com/guardian/guss-rem
//
@function toRem($value, $baseline: globals.$gel-base-font-size) {

    // if function is disabled then return the value
    @if ($gel-tools-rem-enable--function == false) {
        @return $value;
    }

    @if $value == 0 {
        @return 0; // 0rem -> 0
    }

    @if meta.type-of($value) == list {
        $result: ();

        @each $e in $value {
            $result: append($result, rem($e, $baseline));
        }

        @return $result;
    }

    @return if(meta.type-of($value) == number and math.unit($value) == px, math.div($value, $baseline) * 1rem, $value);
}



// Converts pixel values to rem values for whatever property is passed to it. It returns two
// lines of code — one of the regular pixel values (for old IE), and another with the
// converted rem values (for everyone else).
//
// @param {Int}     $property - The CSS property (e.g. font-size, margin-top)
// @param {Int}     $value - The pixel value you want to convert
//
// @author          Shaun Bent
//
@mixin toRem($property, $value) {

    // Return the pixel value first
    @if ($gel-tools-rem-enable--mixin == false or $gel-tools-rem-enable--fallback == true) {
        #{$property}: $value;
    }

    // Pass the px value into the rem function
    @if ($gel-tools-rem-enable--function == true and $gel-tools-rem-enable--mixin == true) {
        #{$property}: toRem($value);
    }

}
