@use 'sass:meta';

/// Uses media queries to apply the given styles to a given range of Internet
/// Explorer versions only. Targets several different possible range
/// combinations between IE 6 and IE 11. Pass only one value to attempt to only
/// target that version of IE. Version arguments are inclusive to that version.
/// Only accepts integer version numbers for the big releases, do not attempt
/// to pass it a version number with decimal notation.
///
/// @param {Number} $version [null] - An IE version to target, or the first
/// version to target in range set by the subsequent argument. If no value (or
/// a `null` or `false` value) is passed, the mixin will target the most modern
/// versions of IE available, 10 and 11.
/// @param {Number} $version-max [$version] - The final IE version to target in
/// a range. If no value is passed, the mixin will attempt to target just the
/// version number given by $version.
///
/// @content Styles that target the IE versions given.
///
/// @group Utilities
@mixin only-ie($version: null, $version-max: $version) {
  @if $version {
    @if meta.type-of($version) != 'number' or
        meta.type-of($version-max) != 'number'
    {
      @error 'Invalid data type passed to the [ only-ie() ] mixin. You must pass ' +
          'one or two numbers to indicate a range of IE versions being targeted.';
    }
    @if $version-max < $version {
      @error 'The second argument in the [ only-ie() ] mixin, $version-max, must be a '
          'number equal to or greater than the first argument passed, ' +
          '$version. This indicates the range of IE versions being targeted';
    }
    @if $version < 6 or $version-max > 11 {
      @error 'Invalid data range for the [ only-ie() ] mixin. You must choose to ' +
          'target a range of IE versions, starting with IE6 and ending with IE11.';
    }
  }

  @if not $version or ($version == 10 and $version-max == 11) {
    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
      @content;
    }
  } @else if $version == 6 {
    // You can also prepend property values with a '*' character to target IE6-7
    @if $version-max == 7 {
      @media screen\9 {
        @content;
      }
    } @else if $version-max == 8 or $version-max == 6 {
      @media \0screen\, screen\9 {
        @content;
      }
      @if $version-max == 6 {
        @warn 'The [ only-ie() ] mixin cannot target only IE 6 alone. Consider ' +
          'prepending your property values with an `_` character. Targeting ' +
          'IE 6-8 instead now.';
      }
    }
  } @else if $version == 7 {
    @error 'The [ only-ie() ] mixin cannot target a version range that begins with ' +
          '7. Consider prepending your property values in the following way ' +
          'instead: *+html .ie7 { property: value; }';
  } @else if $version == 8 {
    @if $version-max == 8 {
      @media \0screen {
        @content;
      }
    } @else if $version-max == 9 or $version-max == 10 {
      @media screen\0 {
        @content;
      }
      @if $version-max == 9 {
        @warn 'The [ only-ie() ] mixin can target IE 8, 9, and 10, but cannot '
            'target just 8 and 9. targeting IE versions 8-10 instead now.';
      }
    } @else {
      @error 'The [ only-ie() ] mixin can target IE 8, 9, and 10, but not any ' +
          'higher while including IE 8. Please change your selected range.';
    }
  } @else if $version == 9 {
    @if $version-max == 9 {
      @media screen and (min-width: 0\0) and (min-resolution: 0.001dpcm) {
        @content;
      }
    } @else if $version-max == 10 or $version-max == 11 {
      @media screen and (min-width: 0\0) and (min-resolution: +72dpi) {
        @content;
      }
      @if $version-max == 10 {
        @warn 'The [ only-ie() ] mixin can target IE 9 and up, but cannot target ' +
            'just 9 and 10. targeting IE versions 9-11 instead now.';
      }
    }
  } @else if $version == 10 {
    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
      @content;
    }
    @if $version-max == 10 {
      @warn 'The [ only-ie() ] mixin can target IE 10 and up, but cannot target ' +
            'just IE10 alone. targeting IE versions 10 and up instead now. ' +
            'If you need to target only version 10, consider the following ' +
            'syntax instead: _:-ms-lang(x), .ie10 { property: value; }';
    }
  } @else if $version == 11 {
    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
      @content;
    }
    @warn 'The [ only-ie() ] mixin can target IE 10 and up, but cannot target ' +
        'just IE11 alone. targeting IE versions 10 and up instead now. If ' +
        'you need to target only version 11+, consider the following syntax ' +
        'instead: _:-ms-fullscreen, :root .ie11up { property: value; }';
  }
}

/// Uses media queries to apply the given styles to a given range of Internet
/// Explorer versions only. Targets several different possible range
/// combinations between IE 6 and IE 11. Pass only one value to attempt to only
/// target that version of IE. Version arguments are inclusive to that version.
/// Only accepts integer version numbers for the big releases, do not attempt
/// to pass it a version number with decimal notation.
///
/// @param {Number} $version [null] - An IE version to target, or the first
/// version to target in range set by the subsequent argument. If no value (or
/// a `null` or `false` value) is passed, the mixin will target the most modern
/// versions of IE available, 10 and 11.
/// @param {Number} $version-max [$version] - The final IE version to target in
/// a range. If no value is passed, the mixin will attempt to target just the
/// version number given by $version.
///
/// @content Styles that target the IE versions given.
///
/// @group Utilities
/// @require {mixin} only-ie
/// @alias only-ie
@mixin ie-only($version: null, $version-max: $version) {
  @include only-ie($version, $version-max) {
    @content;
  }
}

/// Uses media queries to apply the given styles to a given range of Internet
/// Explorer versions only. Targets several different possible range
/// combinations between IE 6 and IE 11. Pass only one value to attempt to only
/// target that version of IE. Version arguments are inclusive to that version.
/// Only accepts integer version numbers for the big releases, do not attempt
/// to pass it a version number with decimal notation.
///
/// @param {Number} $version [null] - An IE version to target, or the first
/// version to target in range set by the subsequent argument. If no value (or
/// a `null` or `false` value) is passed, the mixin will target the most modern
/// versions of IE available, 10 and 11.
/// @param {Number} $version-max [$version] - The final IE version to target in
/// a range. If no value is passed, the mixin will attempt to target just the
/// version number given by $version.
///
/// @content Styles that target the IE versions given.
///
/// @group Utilities
/// @require {mixin} only-hidpi
/// @alias only-ie
@mixin target-ie($version: null, $version-max: $version) {
  @include only-ie($version, $version-max) {
    @content;
  }
}
