@use "sass:math";
@use "sass:list";
@use "sass:meta";
@use "sass:string";

$_eds-spacing-utility-tokens: ($t-a-space-absolute-0, $t-a-space-absolute-100, $t-a-space-absolute-200, $t-a-space-absolute-400, $t-a-space-absolute-600, $t-a-space-absolute-800, $t-a-space-absolute-1200, $t-a-space-absolute-1600);
$_eds-spacing-utility-margin-keywords: (auto);
$_eds-spacing-utility-names: (
	margin: (
		'ma' 'margin',
		'mt' 'margin-top',
		'mr' 'margin-right',
		'mb' 'margin-bottom',
		'ml' 'margin-left'
	),
	padding: (
		'pa' 'padding',
		'pt' 'padding-top',
		'pr' 'padding-right',
		'pb' 'padding-bottom',
		'pl' 'padding-left'
	)
);

@function remove-units($list) {
	$new-list: ();
	@each $item in $list {
	  @if math.is-unitless($item) {
		$new-list: list.append($new-list, $item);
	  } @else {
		$unitless-item: strip-unit($item);
		$new-list: list.append($new-list, $unitless-item);
	  }
	}
	@return $new-list;
}

$_eds-spacing-unitless: remove-units($_eds-spacing-utility-tokens);

@function _get-spacing-utility-value($value) {
	$value: if(meta.type-of($value) == string, string.unquote($value), $value);

	@return if(meta.type-of($value) == number and $value > 0, $value * 1px, $value);
}

@each $property-group, $pair in $_eds-spacing-utility-names {
	$utility-values: if($property-group == margin, list.join($_eds-spacing-unitless, $_eds-spacing-utility-margin-keywords), $_eds-spacing-unitless);

	@each $name, $property in $pair {
		@each $value in $utility-values {
			.u-#{$name}-#{$value} {
				#{$property}: _get-spacing-utility-value($value);
			}
		}
	}
}