@use "sass:list";

/// is Property List
// --------------------------
/// Is the value given a property list?
///
/// @access public
/// @group property utilities
/// @param {value: any}
/// @return {boolean}
///

@function is-property-list($value) {
  @if type-of($value) == "map" {
    @return true;
  } @else {
    @return false;
  }
}
/// is Themable
// --------------------------
/// Determines wether or not the following property should get a custom property and thus
/// will be able to be themed.
///
/// @access public
/// @group property utilities
/// @param {value: any}
/// @return {boolean}
///

@function is-themable($property) {
  @return list-contains($themable, $property);
}

/// Property
// --------------------------
/// Creates all properties and automatically adds custom properties with defaults where necessary.
///
/// @access public
/// @group property utilities
/// @param {args: any}
/// @return {properties}
///

@function get-last-property($list) {
  @return nth($list, length($list));
}

@function cleanup-path($path) {
  $path: simple-to-string($path, " ");
  $p: "";

  @if type-of($path) == "list" {
    $p-list: ();
    @each $part in $path {
      $p-list: list.append($p-list, str-replace($part, ".", ""));
    }
    $p: simple-to-string($p-list, " ");
  } @else {
    $p: str-replace($path, ".", "");
  }
  @return $p;
}

@mixin property($args...) {
  @each $arg in $args {
    @if is-property-list($arg) {
      @each $property in $arg {
        $prop: nth($property, 1);
        $value: nth($property, 2);

        @if is-themable($prop) {
          $var: "#{letter-uppercase($prop)}";

          @if get-setting(classBasedProperties) == false {
            $m: &;

            @if not list-contains($args, "use-path") {
              $m: get-last-property($m);
            }
            $m: cleanup-path($m);
            $var: "#{$m}#{letter-uppercase($prop)}";
          }

          // #{$prop}: variable("#{$m}#{letter-uppercase($prop)}", $value);
          #{$prop}: variable($var, $value);
        } @else {
          #{$prop}: #{$value};
        }
      }
    }
  }
}
