/**
 * Get a settings from the global settings stack
 *
 * @param 				{String} 			$path 						The setting path wanted separated by dots
 * @param 				{String} 			[$context=s-context()] 		The context name to get the setting
 * @param 				{Mixed} 			[$default=null] 			The value to return if no value is found
 * @return 				{Mixed} 										The setting value¨
 *
 * @example
 * $font-size : s-setting('typography.font-size');
 * // etc...
 *
 * @author 			Olivier Bossel <olivier.bossel@gmail.com> (https://olivierbossel.com)
 */
$_sugar-settings-cache: ();
@function s-setting($path, $context: $_sugar-current-context, $default: null) {
  $base_path: $path;
  @if $context == default {
    $context: null;
  }

  // check if has a component
  @if $context {
    $path: "contexts.#{$context}.#{$path}";
  }

  // check in cache
  $cache: map-get($_sugar-settings-cache, $path);
  @if $cache {
    @return $cache;
  }

  $value: map-get-path($_sugar-settings, $path);

  @if not $value and $context {
    // try to get the value from the base component
    $value: s-setting($base_path, default, $default);
  }

  @if type-of($value) == number and not unitless($value) {
    @if unit($value) == px or unit($value) == pt or unit($value) == rem {
      $value: s-convert($value);
    }
  }

  // save the value in cache
  $_sugar-settings-cache: map-set(
    $_sugar-settings-cache,
    $path,
    $value
  ) !global;

  // if no value
  @if not $value {
    @return $default;
  }

  // return the value
  @return $value;
}
