/// **Important: This must be called in order to use any of the tools.**
/// ---
/// Pulls together config variables and routes them into structures expected by the framework, then makes them global.
/// For instance, the `$type-h1-size` variable is accepted as a list, but will be available as a single size, while making `$type-h1-lh` available.
/// @group core
///
@mixin init() {
    @include _set-global-typography-variables();

    //Expand color map if necessary
    $base-colors: _verify-color-map-depth($base-colors) !global;

/// Abstraction of the base spacing unit used in any mixin or function that defines spacing units.
/// @type Number
/// @group core
///
    $type-base-unit: $base-line-height !global;

    @if $base-font-size >= $type-base-unit {
        $type-base-unit: baseline-calc($base-font-size) *
            $base-font-size !global;
    }

/// Ratio used to calculate the difference between font sizes and spacing units.
/// @type Number
/// @group core
///
    $type-base-ratio: ($type-base-unit / $base-font-size) !global;

/// Used for defining automatic headings functions.
/// @type List | Number
///
    $type-headings: 6, 5, 4, 3, 2, 1 !global;

    /// Silent class registry.
    /// Holds all auto-generated silent classes for debugging.
    $_silent-class-registry: () !global;

    /// All breakpoint names
    $_all-breakpoints: map-keys($breakpoints);

    /// Grabs the first `$_all-breakpoints` name, allowing for abstraction.
    $breakpoint-smallest: nth($_all-breakpoints, 1) !global;

    /// Grabs the last `$_all-breakpoints` name, allowing for abstraction.
    $breakpoint-largest: nth(
        $_all-breakpoints,
        length($_all-breakpoints)
    ) !global;
}

/// Makes sure color map contains the necessary depth to be used by `colors` function
/// @param {Map} $color-map
///
@function _verify-color-map-depth($color-map) {
    $is-deep-enough: true;

    @if map-depth($color-map) {
        $is-deep-enough: false;
    } @else {
        $i: 1;
        $map-length: length($color-map);

        @while ($i <= $map-length and $is-deep-enough) {
            $name: nth(nth($color-map, $i), 1);

            @if (type-of(map-get($color-map, $name) != 'map')) {
                $is-deep-enough: false;
            }

            $i: $i + 1;
        }
    }

    @if ($is-deep-enough == false) {
        $color-map: generate-color-variations($color-map);
    }

    @return $color-map;
}

/// Pulls typography variables apart and puts them into easier to use pieces.
/// For instance `$type-h1-size` starts as a list and becomes a single value, while adding `$type-h1-lh` (line-height) to the global scope for use.
@mixin _set-global-typography-variables() {
    @if $auto-scale-type {
        $type-h1-size: modular-scale(7) !global;
        $type-h2-size: modular-scale(5) !global;
        $type-h3-size: modular-scale(4) !global;
        $type-h4-size: modular-scale(3) !global;
        $type-h5-size: modular-scale(2) !global;
        $type-h6-size: modular-scale(1) !global;
        $type-p-size: modular-scale(0) !global;
        $type-small-size: modular-scale(-1) !global;
    }

    //Check for existence of line-height override, and store it

/// H1 font-size
/// @group core
    $h1-fs: nth($type-h1-size, 1) !global;
/// H1 line-height
/// @group core
    $h1-lh: _extract-line-height($type-h1-size) !global;

/// H2 font-size
/// @group core
    $h2-fs: nth($type-h2-size, 1) !global;
/// H2 line-height
/// @group core
    $h2-lh: _extract-line-height($type-h2-size) !global;

/// H3 font-size
/// @group core
    $h3-fs: nth($type-h3-size, 1) !global;
/// h3 line-height
/// @group core
    $h3-lh: _extract-line-height($type-h3-size) !global;

/// H4 font-size
/// @group core
    $h4-fs: nth($type-h4-size, 1) !global;
/// h4 line-height
/// @group core
    $h4-lh: _extract-line-height($type-h4-size) !global;

/// H5 font-size
/// @group core
    $h5-fs: nth($type-h5-size, 1) !global;
/// h5 line-height
/// @group core
///
    $h5-lh: _extract-line-height($type-h5-size) !global;

/// H6 font-size
/// @type Number (pixel)
///
    $h6-fs: nth($type-h6-size, 1) !global;
/// h6 line-height
/// @group core
///
    $h6-lh: _extract-line-height($type-h6-size) !global;

/// paragraph font-size
/// @group core
///
    $p-fs: nth($type-p-size, 1) !global;
/// paragraph line-height
/// @group core
///
    $p-lh: _extract-line-height($type-p-size) !global;

/// small font-size
/// @group core
///
    $small-fs: nth($type-small-size, 1) !global;
/// small line-height
/// @group core
///
    $small-lh: _extract-line-height($type-small-size) !global;

/// @alias h1-lh
///
    $type-h1-lh: $h1-lh !global;
    $type-h1-size: $h1-fs !global;
/// @alias h2-lh
///
    $type-h2-lh: $h2-lh !global;
    $type-h2-size: $h2-fs !global;
/// @alias h3-lh
///
    $type-h3-lh: $h3-lh !global;
    $type-h3-size: $h3-fs !global;
/// @alias h4-lh
///
    $type-h4-lh: $h4-lh !global;
    $type-h4-size: $h4-fs !global;
/// @alias h5-lh
///
    $type-h5-lh: $h5-lh !global;
    $type-h5-size: $h5-fs !global;
/// @alias h6-lh
///
    $type-h6-lh: $h6-lh !global;
    $type-h6-size: $h6-fs !global;
/// @alias p-lh
///
    $type-p-lh: $p-lh !global;
    $type-p-size: $p-fs !global;
/// @alias small-lh
///
    $type-small-lh: $small-lh !global;
    $type-small-size: $small-fs !global;
}

/// @alias init
/// @group core
///
@mixin globalize-config {
    @include init;
}
