@import "compass/css3/animation";
@import "compass/css3/opacity";
@import "true";

$some-default-value: some default value;
@function has-default-arglist($arglist...) {
  @return set-arglist-default($arglist, $some-default-value);
}

@mixin with-declared-support($browsers) {
  $old-browsers: $supported-browsers;
  $supported-browsers: $browsers !global;
  @content;
  $supported-browsers: $old-browsers !global;
}

@mixin with-declared-minimums($supported-browsers) {
    $old-mins: $browser-minimum-versions;
    $browser-minimum-versions: $supported-browsers !global;
    @content;
    $browser-minimum-versions: $old-mins !global;
}

@include test-module('Compass Support') {
  @include test('[function] set-arglist-default() with no arguments') {
    @include assert-true(
               has-default-arglist() == $some-default-value,
               'No arguments should get a default value.');
  }
  @include test('[function] set-arglist-default() with a "default" argument') {
    @include assert-true(
               nth(has-default-arglist(default), 1) == $some-default-value,
               'an argument of "default" should get a default value.');
  }
  @include test('[function] set-arglist-default() with a "default" argument and another argument') {
    @include assert-true(
               has-default-arglist(default, some other value) == ($some-default-value, some other value),
               'an argument of "default" should get a default value.');
  }
  @include test('[function] set-arglist-default() with a "default" argument not first and another argument') {
    @include assert-true(
               has-default-arglist(some other value, default) == (some other value, $some-default-value),
               'an argument of "default" should get a default value.');
  }

  @include test('[function] support-legacy-browser() from threshold') {
    @include assert-true(support-legacy-browser('ie', '6', $threshold: 0.00000001),
                         "should be supported because of user support threshold.");
    @include assert-false(support-legacy-browser('ie', '6', $threshold: 10.000),
                          "should not be supported because of support threshold.");
  }

  @include test('[function] support-legacy-browser() from minimums') {
    @include with-declared-minimums(('ie': '7')) {
      @include assert-true(support-legacy-browser('ie', '7', $threshold: 10.00),
      "should be supported because of minimum version.");
      @include assert-false(support-legacy-browser('ie', '6', $threshold: 10.000),
      "should not be supported because of minimum version.");
    }
  }

  @include test('[function] browser-out-of-scope() with no scope') {
    @include assert-true(not browser-out-of-scope('ie'), "should be in scope.");
    @include assert-true(not browser-out-of-scope('chrome'), "should be in scope.");
  }

  @include test('prefix context tracking') {
    $outer-prefix-invoked: false;
    $inner-prefix-invoked: false;
    $mismatched-prefix-invoked: false;
    @include with-prefix(-webkit) {
      $outer-prefix-invoked: true;
      @include assert-equal(-webkit, $current-prefix, "should be equal.");
      @include assert-equal(-webkit, $prefix-context, "should be equal.");
      @include with-prefix(null) {
        $inner-prefix-invoked: true;
        @include assert-equal(null, $current-prefix, "should be equal.");
        @include assert-equal(-webkit, $prefix-context, "should be equal.");
      }
      @include with-prefix(-ms) {
        $mismatched-prefix-invoked: true;
      }
    }
    @include assert-true($inner-prefix-invoked, "should have been invoked");
    @include assert-true($outer-prefix-invoked, "should have been invoked");
    @include assert-false($mismatched-prefix-invoked, "should not have been invoked");
    @include assert-equal(null, $prefix-context, "should be null");
    @include assert-equal(null, $current-prefix, "should be null");
  }

  @include test('[function] browser-out-of-scope() with a scope') {
    @include with-prefix(browser-prefix('chrome')) {
      @include assert-true(not browser-out-of-scope('chrome'), "should be in scope.");
      @include assert-false(not browser-out-of-scope('ie'), "should not be in scope.");
    }
  }

  @include test('[function] browser-out-of-scope() with version') {
    @include with-browser-ranges((ie: '8' '8')) {
      @include assert-false(not browser-out-of-scope('ie', '10'), "should not be in scope.");
      @include assert-true(not browser-out-of-scope('ie', '8'), "should be in scope.");
      @include assert-false(not browser-out-of-scope('ie', '7'), "should not be in scope.");
    }
  }

  @include test('[mixin] with-prefix()') {
    @include assert-true($current-prefix == null, "should be null.");
    @include with-prefix(-webkit) {
      @include assert-true($current-prefix == -webkit, "should be set.");
      $called: false;
      @include with-prefix(-webkit) {
        $called: true;
      }
      @include assert-true($called, "should be called.");
      $called: false;
      @include with-prefix(-ms) {
        $called: true;
      }
      @include assert-false($called, "should not be called.");
    }
  }

  @include test('[mixin] with-each-prefix()') {
    $prefixes: ();
    @include with-each-prefix(css-regions, 0.0001) {
      $prefixes: append($prefixes, $current-prefix);
    }
    @include assert-equal((-ms -webkit null), $prefixes, "Not equal");
  }

  @include test('[mixin] with-each-prefix() respects $supported-browsers') {
    @include with-declared-support("ie") {
      $prefixes: ();
      @include with-each-prefix(css-regions, 0.0001) {
        $prefixes: append($prefixes, $current-prefix);
      }
      @include assert-equal((-ms null), $prefixes, "Not equal");
    }
  }

  @include test('[mixin] with-each-prefix() respects $current-prefix') {
    $contexts: ();
    $prefixes: ();
    @include with-prefix(-webkit) {
      $prefixes: append($prefixes, $current-prefix);
      $contexts: append($contexts, $prefix-context);
      @include with-each-prefix(css-regions, 0.0001) {
        $prefixes: append($prefixes, $current-prefix);
        $contexts: append($contexts, $prefix-context);
      }
    }
    @include assert-true(index($prefixes, null), "Must have a null");
    @include assert-equal((-webkit -webkit null), $prefixes, "Not equal");
    @include assert-equal((-webkit -webkit -webkit), $contexts, "Not equal");
  }

  @include test('[fuction] has-browser-subset()') {
    @include assert-true(has-browser-subset((firefox: "16" "25"), (firefox: "2" "25")), "Should detect version overlap");
    @include assert-true(not has-browser-subset((firefox: "16" "25"), (firefox: "2" "15")), "Should not detect version overlap");
    @include assert-true(not has-browser-subset((firefox: "2" "15"), (firefox: "16" "25")), "Should not detect version overlap");
    @include assert-true(has-browser-subset((firefox: "16" "24"), (firefox: "2" "25")), "Should detect version overlap");
    @include assert-true(has-browser-subset((firefox: "2" "25"), (firefox: "16" "24")), "Should detect version overlap");
  }
}

@include with-browser-support-debugging {
  @include keyframes(foo) {
    0% { @include opacity(0); }
    100% { @include opacity(1); }
  }
  .foo {
    @include animation(foo 1s);
  }
}
