// EXTENDERS

// Creates a new BEM %class
// %block-default__element--modifier { ... }
@mixin extender($b, $e:null, $m:null, $test:null) {

  // Used for testing classes
  // $test:null = `%block { }'
  // $test:true = `block { }'
  $extend: '%';
  @if $test == true {
    $extend: null;
  }

  @if media-context() != "" {
    $b: "#{$b}-#{media-context()}";
  }

  @if $b and not $e and not $m {
    #{$extend}#{$b} { @content }
  }
  @else if $e and not $m {
    #{$extend}#{$b}__#{$e} { @extend #{$extend}#{$b} !optional; @content; }
  }
  @else if $m {
    #{$extend}#{$b}__#{$e}--#{$m} { @extend #{$extend}#{$b}__#{$e} !optional; @content; }
  }

}

// Extends an BEM extention
// @extend %block-default__element--modifier;
@mixin extend($b, $e:null, $m:null, $test:null) {

  // Used for testing classes
  // $test:null = `@extend: %block;'
  // $test:true = `@extend: block;'
  $extend: '%';
  @if $test == true {
    $extend: null;
  }

  @if media-context() != "" {
    $b: "#{$b}-#{media-context()}";
  }

  & {
    @if $b and not $e and not $m {
      @extend #{$extend}#{$b};
    }
    @else if $e and not $m {
      @extend #{$extend}#{$b}__#{$e};
    }
    @else if $m {
      @extend #{$extend}#{$b}__#{$e}--#{$m};
    }
  }

}


// Function for getting Media Context
@function media-context(){
    @return $_current_breakpoint_key;
}

// Generators using media() mixin
@mixin extender-media-generator() {
  // generates without media context
  // e.g. %grid__span__1
  @content;
  // generates with media context
  // e.g. %grid_small_span__1
  @each $key, $value in $breakpoints {
    @include media($key){ @content; }
  }
}

// Use this in project
// @include extender-media-generator() {
//   @include extender(..) {
//     ...
//   }
// }
