/*!
* sass-greedy v1.1.3
* A flexible, lightweight and simple grid generator for sass.
*
* Author: Huseyin Elmas
*/
// greedy options getter method
@function greedy-options($prop) {
  @return map-get($_options, $prop);
}

// check if grid wrapper is enabled
@function is-enabled($option, $mixin-name: null) {
  @if $mixin-name and greedy-options($option) == true and mixin-exists($mixin-name) {
    @return true;
  }

  @if type-of(greedy-options($option)) == string and greedy-options($option) != '' {
    @return true;
  }

  @if type-of(greedy-options($option)) == number and greedy-options($option) > 0 {
    @return true;
  }

  @return false;
}

// greedy logger
@function log($msg, $type: 'error') {
  // valiadate message
  @if $msg and $msg != '' {
    @if $type == 'error' {
      @error $msg;
    } @else if $type == 'warn' {
      @warn $msg;
    }
  }

  @return true;
}

// merge objects into one
@function merge-objects($objects...) {
  // check if any object is provided
  @if length($objects) == 0 {
    $_: log('No object provided to be merged.');
  }
  
  $merged: ();
  
  // loop each provided objects to be merged
  @each $object in $objects {
    // check if object has any property
    @if length($object) > 0 {
      // loop all existing proerties in the object
      @each $propKey, $propVal in $object {
        $merged: map-merge($merged, ($propKey: $propVal));
      }
    }
  }
  
  // return the merged object
  @return $merged;
}

// column generator
@mixin column-generator($size) {
  $column-name: greedy-options('column-class');
  
  // if numbered-column-class is provided, use it instead of column-class
  @if greedy-options('numbered-column-class') {
    $column-name: greedy-options('numbered-column-class');
  }
  
  // if size is provided, insert it at the end of the column-name
  @if $size and $size != '' {
    $column-name: '#{$column-name}-#{$size}';
  }

  // check if grid-wrapper-class is provided
  @if is-enabled('grid-wrapper-class') {
    $column-name: '#{greedy-options('grid-wrapper-class')} .#{$column-name}';
  }
  
  @for $i from 1 through greedy-options('columns') {
    // e.g.: slice-1
    .#{$column-name}-#{$i} {
      width: percentage($i / greedy-options('columns'));
      flex: none;
    }
    
    // run custom column mixins if it does exist and injections are active
    @if is-enabled('injections', 'greedy-custom-column-mixins') {
      @include greedy-custom-column-mixins($size, $i);
    }
  }
}

// grid generator
@mixin grid-generator($option) {
  $_options: merge-objects($_defaults, $option) !global;
  $row-class: greedy-options('row-class');
  $column-class: greedy-options('column-class');

  @if length(greedy-options('sizes')) == 0 {
    $_: log('length of sizes cannot be zero.');
  }

  // check if grid-wrapper-class is provided
  @if is-enabled('grid-wrapper-class') {
    $row-class: '#{greedy-options('grid-wrapper-class')} .#{greedy-options('row-class')}';
    $column-class: '#{greedy-options('grid-wrapper-class')} .#{greedy-options('column-class')}';
  }

  @if is-enabled('row-class') {
    // row element styles
    .#{$row-class} {
      @if is-enabled('gutter') {
        margin-left: -#{greedy-options('gutter') / 2};
        margin-right: -#{greedy-options('gutter') / 2};
      }
      
      display: flex;
      flex-wrap: wrap;
    }
  }
  
  @if is-enabled('column-class') {
    // column element styles
    .#{$column-class} {
      @if is-enabled('gutter') {
        padding-left: #{greedy-options('gutter') / 2};
        padding-right: #{greedy-options('gutter') / 2};
      }
    
      flex-basis: 0;
      flex-grow: 1;
      flex-shrink: 1;
    }
  }
  
  // run custom row mixins if it does exist and injections are active
  @if is-enabled('injections', 'greedy-custom-row-mixins') {
    @include greedy-custom-row-mixins();
  }
  
  // generate columns for each sizes
  @each $size in greedy-options('sizes') {
    $size-name: $size;
    $breakpoint: null;
    
    // generate columns if the size has breakpoint for responsiveness by using include-media
    @if type-of($size-name) == map {
      $size-name: map-get($size, 'name');
      $breakpoint: map-get($size, 'breakpoint');
      
      @include media($breakpoint...) {
        @include column-generator($size-name);

        // run custom size mixins if it does exist and injections are active
        @if is-enabled('injections', 'greedy-custom-size-mixins') {
          @include greedy-custom-size-mixins($size-name, $breakpoint);
        }
      }
    } @else {
      @include column-generator($size-name);
    }
  }

  // reset internal option variable
  $_options: null;
}

// greedy defaults
$_defaults: (
  'row-class': 'row',
  'column-class': 'column',
  'numbered-column-class': 'slice',
  'grid-wrapper-class': false,
  'injections': true
);

// greedy grid generator options for internal usage
$_options: null;

// initialize grid generator
@mixin greedy($options...) {
  @if length($options) == 0 {
    $_: log('No grid option provided.');
  }
  
  // loop each grid config that will be generated
  @each $option in $options {
    @include grid-generator($option);
  }
}
