$_sugar-inited: false;
/**
 * Init the sugar toolkit.
 * This has to be called **AFTER** all the [./_s-setup.scss] mixin call
 * and **BEFORE** all the others sugar mixins and functions calls
 *
 * @example
 * \@include s-init();
 *
 * @author 		Olivier Bossel <olivier.bossel@gmail.com> (https://olivierbossel.com)
 */
@mixin s-init() {
  // do that only if not already inited
  @if not $_sugar-inited {
    // update inited status
    $_sugar-inited: true !global;

    // reset
    @if s-setting("reset") == true {
      @include s-reset();
    }

    // styleguide
    @include s-styleguide-colors();

    // settings
    .s-settings:after {
      // process colors
      $settings-to-process: _extend((), $_sugar-settings);
      // get colors
      $colors: s-setting("colors");
      @each $color-name, $color in $colors {
        // @warn($color);
        $modifiers: _get($color, modifiers);
        @if $modifiers {
          $new-modifiers: ();
          @each $modifier-name, $modifier in $modifiers {
            // @warn(s-color($color-name, $modifier));
            // @warn("#{$color-name} modifiers #{$modifier-name}");
            $settings-to-process: _set(
              $settings-to-process,
              colors "#{$color-name}"modifiers"#{$modifier-name}",
              s-color($color-name, $modifier)
            );
          }
        }
      }
      content: json-encode($settings-to-process);
      display: none;
    }

    // border box
    @if s-setting("border-box") == true {
      html {
        box-sizing: border-box;
      }
      *,
      *:before,
      *:after {
        box-sizing: inherit;
      }
    }

    // links
    a {
      color: inherit;
    }

    // check settings
    $base-font-family: s-setting("typography.font-family");
    $fonts: s-setting("fonts");
    @if $base-font-family and not map-get($fonts, $base-font-family) {
      @error ("Your typography.font-family setting has to be a registered font...");
    }

    // set the base font family if not defined
    @if not $base-font-family {
      @include s-setup(
        (
          typo: (
            font-family: default
          )
        )
      );
    }

    // font
    html {
      $base-font-size: s-setting("typography.font-size");
      $base-font-sizes: s-setting("typography.font-sizes");
      font-size: s-Px($base-font-size);
      color: s-color(text);
      @if type-of($base-font-sizes) == map {
        @each $size, $media in $base-font-sizes {
          @include s-media($media...) {
            font-size: s-Px($size);
          }
        }
      }
      // try to get the font family from fonts
      @include s-font(s-setting("typography.font-family"));
    }

    // default color and stuffs on the body
    body {
      color: s-color(text);
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }

    // font faces
    $fonts: s-setting("fonts");
    @each $font-name, $font in $fonts {
      @if type-of($font) == string {
        $font: map-get($fonts, $font);
      }
      @if not map-get($font, font-family) {
        $font: map-set($font, font-family, $font-name);
      }
      $font: map-remove($font, cap-height);
      @if map-get($font, src) {
        @include s-font-face($font...);
      } @else {
        $ff: map-get($font, font-family);
        $fs: map-get($font, font-style);
        $fw: map-get($font, font-weight);
        $fd: map-get($font, font-display);
        /**
				 * @name   			#{$font-name}
				 * @styleguide 		Fonts / Faces
				 * @display 		font
				 * @font-family 	#{$ff}
				 * @font-style 		#{$fs}
				 * @font-weight 	#{$fw}
				 * @font-display    #{$fd}
				 */
      }
    }

    // modular scale
    $ms-base: s-setting("modular-scale.base") !global;
    $ms-ratio: s-setting("modular-scale.ratio") !global;
  }
}
