@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';
@use 'sass:string';
@use 'sass:math';
@use "../variables/default" as vd;

$direction-map: ('l': left, 'r': right, 't': top, 'b': bottom);

@function merge($rest...) {
	$options: ();

	@each $item in $rest {
		@if  meta.type-of($item) == 'map' {
			$options: map.merge($options, $item);
		} @else if meta.type-of($item) == 'list' {
			$options: map.merge($options, merge($item));
		}
	}

	@return $options;
}

@function prefix($rest...) {
	$options: merge((unit: vd.$unit, prefix: vd.$prefix), $rest...);
	$unit: map.get($options, 'unit');
	$prefix: map.get($options, 'prefix');
	$name: map.get($options, 'name');

	@return #{'.'}#{if(
		sass($prefix and $prefix != ""):
		#{$prefix}#{if(sass(string.slice(#{$prefix}, -1) == '-'): ''; else: '-')};
		else: ""
	)}#{if(sass($name and $name != ""): #{$name}-; else: "")};
}

// 10/10px是number类型, 后者带单位; "10px"是string类型
// 当scale = 2时: 10px -> 20px; 当单位为rem时：10 -> 10rem; 仅当字符串时，保持不变
@function unitfix($value, $rest...) {
	$options: merge((unit: vd.$unit, scale: vd.$scale), $rest...);
	$unit: map.get($options, 'unit');
	$scale: map.get($options, 'scale');

	@if  meta.type-of($value) == 'number' {
		$value: if(sass(math.is_unitless($value)): #{$value * $scale}#{$unit}; else: #{$value * $scale});
	} @else if meta.type-of($value) == 'string' {
		$value: string.unquote($value);
	}

	@return $value;
}

// 当scale = 2时: g-fs-10 -> g-fs-20; 仅当字符串时，保持不变
@function suffix($value, $rest...) {
	$options: merge((scale: vd.$scale), $rest...);
	$scale: map.get($options, 'scale');

	@if  meta.type-of($value) == 'number' {
		$value: $value * $scale;
	} @else if meta.type-of($value) == 'string' {
		$value: string.unquote($value);
	}

	@return $value;
}

@function percentw($col, $total) {
	@return math.percentage(math.div($col, $total));
}
