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

@use "./etra-string" as *;
@use "./etra-list" as *;
@use "./etra-maps" as *;

@function mnvar($args...) {
  $result: ();

  @each $arg in $args {
    $is-dotted: string.index($arg, ".");

    @if $is-dotted != null {
      $splitted: split($arg, ".");

      @each $arg in $splitted {
        $result: list.append($result, $arg);
      }
    } @else {
      $result: list.append($result, $arg);
    }
  }

  $name: join($result, "-");
  @return "--mie-#{$name}";
}

@function mvar($args...) {
  $is-important: if(list.nth($args, list.length($args)) == true, true, false);

  @if $is-important {
    $args: drop-right($args, 1);
  }

  @return var(#{mnvar($args...)}) #{if($is-important, !important, "")};
}

@mixin mdvar($key, $value, $is-important: false) {
  @if not $key or length($key) == 0 {
    @error "$key can`t be empty";
  }

  @if type-of($key) == "string" {
    $key: ($key);
  }

  $is-important: if($is-important, "!important", "");
  $prop-key: mnvar($key...);

  #{$prop-key}: #{$value} #{$is-important};
}
