// ============================================================================
// 02-TOOLS -> MIXINS -> #GENERATE-UTILITY
// ============================================================================


//
// SCSS imports
//

@use '../../01-settings' as *;
@use '../mixins/media-breakpoint' as *;


/// Create classes
/// 
/// @param {String} $breakpoint - Name of breakpoint in the $breakpoints map
/// (set in `01-settings/_breakpoints.scss`)
/// @param {Map} $directions - Optiona array of directions for CSS properties
/// @param {String} $property - CSS property to generate utility classes
/// @param {String} $prefix - Prefix of the utility class
/// @param {Map} $values - CSS property values to generate utility classes
/// 
/// @access private

@mixin _create-classes($breakpoint: null, $directions: null, $property, $prefix, $values) {
    @if type-of($values) != 'map' {
        @error "The argument $values: `#{values}` is of incorrect type: `#{type-of($values)}`. Type of `Map` is required.";
    }
    
    @if $directions {
        @each $size, $value in $values {
        .#{if($breakpoint, #{$breakpoint}\:, '')}#{$prefix}-#{$size} {
                #{$property}: #{$value};
            }
            
            @each $direction-value, $directions in $directions {
                $build-classes: '';
                
                @each $direction in $directions {
                    $build-classes: $build-classes + '.#{if($breakpoint, #{$breakpoint}\:, '')}#{$prefix}#{$direction}-#{$size},';
                }

                #{$build-classes} {
                    #{$property}-#{$direction-value}: $value;
                }
            }
        }
    } @else {
        @each $size, $value in $values {
            .#{if($breakpoint, #{$breakpoint}\:, '')}#{$prefix}-#{$size} {
                #{$property}: #{$value};
            }
        }
    }
}


/// Generate utility
/// 
/// @param {Map} $utility-map - Settings to create utility classes
/// 
/// @example scss
///   @include generate-utility((
///     'property':   'font-weight',
///     'prefix':     'font',
///     'values':     $font-weight
///   ));
/// 
/// @access public

@mixin generate-utility($utility-map) {
    @if type-of($utility-map) != 'map' {
        @error "The argument $map: `#{$utility-map}` is of incorrect type: `#{type-of($utility-map)}`. Type of `Map` is required.";
    }
    
    $directions: map-get($utility-map, 'directions');
    $property: map-get($utility-map, 'property');
    $prefix: map-get($utility-map, 'prefix');
    $responsive: map-get($utility-map, 'responsive');
    $values: map-get($utility-map, 'values');
    
    @include _create-classes(
        $directions: $directions,
        $property: $property,
        $prefix: $prefix, 
        $values: $values
    );
    
    @if $responsive {
        @each $breakpoint, $value in $breakpoints {
            @if $breakpoint != 'xs' {
                @include media-breakpoint-up($breakpoint) {
                    @include _create-classes(
                        $breakpoint: $breakpoint,
                        $directions: $directions,
                        $property: $property,
                        $prefix: $prefix, 
                        $values: $values
                    );
                }
            }
        }
    }
}