@use "sass:map";
@use "sass:list";
@use "helper";

$--units: () !default;

@mixin define-unit($name, $unit, $iterable: true) {
    $name: helper.string-of($name, "define-unit", "name");
    $unit: helper.number-of($unit, "define-unit", "unit");
    $iterable: helper.bool-of($iterable, "define-unit", "iterable");
    $--units: map.merge($--units, helper.map-from($name, $unit $iterable)) !global;
}

@function unit($name) {
    $name: helper.string-of($name, "unit", "name");
    @if map.has-key($--units, $name) {
        @return list.nth(map.get($--units, $name), 1);
    }
    @error "#{$name} unit not found!";
}

@function units($includes: ()) {
    $includes: helper.list-of($includes, "units", "includes");
    $res: ();
    @each $k, $v in $--units {
        @if list.index($includes, $k) != null or list.nth($v, 2) == true {
            $res: map.merge($res, helper.map-from($k, list.nth($v, 1)));
        }
    }
    @return $res;
}
