@use "sass:meta";
@use "sass:list";
@use "sass:math";
@use "sass:map";
@use "mediaQueries.scss" as *;
@use "../variables/spacingBase.scss" as *;
@forward "../variables/spacingBaseList.scss";
@use "../variables/spacingDetail.scss" as *;
@use "../variables/spacingDetailScreenLarge.scss" as *;
@forward "../variables/spacingDetailList.scss";

@function ds-spacing($units: null, $sizeUnit: "px", $negative: false) {
  @if $units {
    $values: ();

    @each $unit in $units {
      @if map.has-key($spacingDetail, $unit) {
        $value: var(--ds-spacing-#{$sizeUnit}-#{$unit});
        @if ($negative) {
          $value: calc(var(--ds-spacing-#{$sizeUnit}-#{$unit}) * -1);
        }
        $values: list.append($values, $value);
      } @else {
        $value: $unit;
        @if map.has-key($spacingBase, $unit) {
          $value: map.get($spacingBase, $unit);
        }

        @if ($value == 0) {
          $value: 0 * 1;
        } @else if ($value and meta.type-of($value) == "number") {
          @if ($sizeUnit == "rem") {
            $value: (math.div($value, 16)) * 1rem;
          } @else {
            $value: $value * 1px;
          }
          @if $negative {
            $value: $value * -1;
          }
        }
        @if (meta.type-of($value) == "number" or $value == "auto") {
          $values: list.append($values, $value);
        }
      }
    }

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

    @return $values;
  }

  @return null;
}
