@use "sass:math";
@use "sass:list";
 
//
// Typography
//

$font-size-root: 16px !default;
$px-to-pt: math.div(3, 4);


@function number-to-em($n) { @return $n * 1em; }
@function em-to-number($n) { @return math.div($n, 1em); }

@function number-to-rem($n) { @return $n * 1rem; }
@function rem-to-number($n) { @return math.div($n, 1rem); }

@function number-to-px($n, $base-size: $font-size-root) { @return $n * $base-size; }
@function px-to-number($n, $base-size: $font-size-root) { @return math.div($n, $base-size); }


@function pt-to-px($n) { @return math.div($n, $px-to-pt); }
@function px-to-pt($n) { @return $n * $px-to-pt; }


@function line-height-offset($line-height, $base: 1) {
    @return ($line-height - $base) * 0.5;
}

@function rem($unit-values, $base-size: $font-size-root) {
    @return get-units(1rem, $unit-values, $base-size);
}

@function to-em($unit-values, $base-size: $font-size-root) {
    @return get-units(1em, $unit-values, $base-size);
}

@function get-units($unit-basis: 1rem, $unit-values, $base-size: $font-size-root) {
  // Create an empty list that we can dump values into
  $rem-values: ();

  @each $value in $unit-values {
    // Check to see if value is 0, auto, or inherited
    // If so, do nothing and output value
    @if $value == 0 or $value == auto or $value == inherit {
      $rem-values: list.append($rem-values, $value);
    }

    // If the value is a pixel, convert to rem value
    @else if math.unit($value) == 'px' {
      $rem-values: list.append($rem-values, math.div($value, $base-size) * $unit-basis);
    }

    // If the value is unitless, assume it is a px value and convert to rem value
    @else if math.is-unitless($value) {
      $rem-values: list.append($rem-values, math.div($value, math.stripped-unit($base-size)) * $unit-basis);
    }

    // For everything else, keep as is
    @else {
      $rem-values: list.append($rem-values, $value);
    }
  }

  // return just a single value if only one was passed in
  @if list.length($rem-values) == 1 {
    @return list.nth($rem-values, 1);
  }

  @return $rem-values;
}
