@use "sass:map";

// @import "../helpers/case";

$prefix: null;
$cached-variables: ();

/// init Prefix Properties mixin
// --------------------------
/// Initialize and set the prefix for the project
///
/// @access public
/// @group cached utilities
/// @param {}
/// @return {void}
///
@mixin initPrefix() {
  @if $prefix == null {
    $prefix-value: map.get($settings, "prefix");

    @if $prefix-value != "" {
      $prefix: "#{$prefix-value}-" !global;
    } @else {
      $prefix: "" !global;
    }
  }
}
@include initPrefix();

/// Custom Properties mixin
// --------------------------
/// Create custom properties from map
///
/// @access public
/// @group case utilities
/// @param {map} $group
/// @return {string}
///
@mixin customProperties($group, $affix: "") {
  @each $name, $value in $group {
    @if $value == "" {
      $value: '""';
    }
    --#{$prefix}#{camelCaseToKebabCase($name)}#{$affix}: #{$value};
  }
}

/// Set Variable Cached
// --------------------------
/// Adds a key with value to the cached variables
///
/// @access public
/// @group cached utilities
/// @param {key: string, value: string}
/// @return {map}
///

@function set-variable-cached($key, $value) {
  //  Directly return cached variables when the key is a class
  @if str-contains($key, ".") {
    @return $cached-variables;
  }
  @return map.set($cached-variables, $key, $value);
}

/// Get Variable Value
// --------------------------
/// Gets the value of a variable if it has a default
///
/// @access public
/// @group variable utilities
/// @param {var: string, default: string}
/// @return {string}
///

@function get-variable-value($var, $default) {
  $cache-key: "#{$var}+#{$default}-value";

  // // If it has a cached value, return it.\
  @if map.get($cached-variables, $cache-key) {
    @return map.get($cached-variables, $cache-key);
  }

  // If the variable has a value and default is empty.
  @if has-default($var) and $default == "" {
    $default: get-default($var);
    $cached-variables: set-variable-cached($cache-key, $default) !global;
    @return $default;
  }

  // Otherwise, if default is a string and default isnt empty default is camelCase.
  @if type-of($default) == "string" and $default != "" and isCamelCase($default)
  {
    $default: variable($default);
    $cached-variables: set-variable-cached($cache-key, $default) !global;
    @return $default;
  }

  @return $default;
}

/// Variable Name
// --------------------------
/// Creates the variable name
///
/// @access public
/// @group variable utilities
/// @param {var: string}
/// @return {string}
///

@function variable-name($var, $default) {
  $variable: "";

  @if $default == "" {
    $variable: "--#{$prefix}#{camelCaseToKebabCase($var)}";
  }

  @return $variable;
}

@function wrap-in-classname($variable, $var) {
  @if get-setting(classBasedProperties) and $var != "" {
    $m: &;
    $m: get-last-property($m);
    $m: cleanup-path($m);
    $m: camelCaseToKebabCase($m);

    $v: "#{$prefix}#{$m}-#{$var}";
    @return var(--#{$v}, #{$variable});
  } @else {
    @return $variable;
  }
}

/// Variable
// --------------------------
/// Main function to create a variable, based on it parent and automatically adds defaults
///
/// @access public
/// @group variable utilities
/// @param {var: string, default: string}
/// @return {string}
///

@function variable($var, $default: "", $debug: false) {



  // Key to save direct responses
  $cached-key: "#{$var}+#{$default}";

  // Check if the value exists in cache and return if true.
  $cached-value: map.get($cached-defaults, $cached-key);

  @if $cached-value {
    @return $cached-value;
  }
  // Create variable name
  $variable: variable-name($var, $default);

  $default-value: null;


  $default-value: get-variable-value($var, $default);


  // Return
  $returnValue: "";

  @if $default-value != "" {
    $wrapper: "";
    @if $default != "" and $var != "" {
      $wrapper: $var;
    }
    $v: "";
    @if $variable != "" {
      // @debug "default-value: #{$default-value} // type-of: #{type-of($default-value)}";

      // @if $default-value != "" or $default-value != null or type-of($default-value) != null {

      $v: var(#{$variable}, $default-value);
      // } @else {
      //   $v: var(#{$variable});
      // }
    } @else {
      $v: $default-value;
    }
    $returnValue: wrap-in-classname($v, $wrapper);
  } @else {
    $returnValue: #{$var};
  }

  $cached-variables: set-variable-cached($cached-key, $returnValue) !global;
  @return $returnValue;
}

// Shortcut
@function v($key, $value: "", $debug: false) {

  @return variable($key, $value);
}

// Shortcut
@function _v($key, $value: "") {
  @return _variable($key, $value);
}
