@use 'sass:math';
@use 'sass:list';
@use 'sass:meta';
@use 'sass:map';

// @see https://raw.githubusercontent.com/pierreburel/sass-rem/master/_rem.scss
// sass-lint:disable quotes
$rem-baseline: 16px !default;
$rem-fallback: false !default;
$rem-px-only: false !default;

@function rem-separator($list, $separator: false) {
  @if $separator == "comma" or $separator == "space" {
    @return list.append($list, null, $separator);
  }

  @return list.separator($list);
}

@mixin rem-baseline($zoom: 100%) {
  font-size: math.div($zoom, 16px) * $rem-baseline;
}

@function rem-convert($to, $values...) {
  $result: ();
  $separator: rem-separator($values);

  @each $value in $values {
    @if meta.type-of($value) == "number" and math.unit($value) == "rem" and $to == "px" {
      $result: list.append($result, math.div($value, 1rem) * $rem-baseline, $separator);
    } @else if meta.type-of($value) == "number" and math.unit($value) == "px" and $to == "rem" {
      $result: meta.append($result, math.div($value, $rem-baseline) * 1rem, $separator);
    } @else if meta.type-of($value) == "list" {
      $value-separator: rem-separator($value);
      $value: rem-convert($to, $value...);
      $value: rem-separator($value, $value-separator);
      $result: list.append($result, $value, $separator);
    } @else {
      $result: list.append($result, $value, $separator);
    }
  }

  @return if(list.length($result) == 1, list.nth($result, 1), $result);
}

@function rem($values...) {
  @if $rem-px-only {
    @return rem-convert(px, $values...);
  } @else {
    @return rem-convert(rem, $values...);
  }
}

@mixin rem($properties, $values...) {
  @if meta.type-of($properties) == "map" {
    @each $property in map.keys($properties) {
      @include rem($property, map.get($properties, $property));
    }
  } @else {
    @each $property in $properties {
      @if $rem-fallback or $rem-px-only {
        #{$property}: rem-convert(px, $values...);
      }

      @if not $rem-px-only {
        #{$property}: rem-convert(rem, $values...);
      }
    }
  }
}
