/* -------------------------------------------------- */
/*    Element Querries
/* -------------------------------------------------- */

@mixin eq($conditions...) {
  $selector: &;
  $joined-conditions: ();

  // at least one EQCSS condition is required
  @if length($conditions) < 1 or type-of(nth($conditions, length($conditions))) != map {
    @error 'element query must include at least one condition';
  }

  @for $i from 1 through length($conditions) {
    $condition: nth($conditions, $i);
    // if a selector (string) wasn't specified at the beginning of the arguments, this will be the current context
    @if $i == 1 and type-of($condition) == string {
      $selector: $condition;
    }
    @else if type-of($condition) == map {
      // join each with ' and '
      @each $key, $value in $condition {
        $joined-conditions: $joined-conditions + if(length($joined-conditions) > 0, ' and ', '');

        $joined-conditions: $joined-conditions + '(#{$key}: #{$value})';
      }
    }
    @else { @error 'element query conditions must be a map'; }
  }

  $element-query: unquote("'#{$selector}' and #{$joined-conditions}");

  @at-root {
    // @element syntax doesn't parse with sass, so send as raw string
    @element #{inspect($element-query)} {
      @content;
    }
  }
}
