// 🚨 CAUTION. The approach of using `forVersion($level)` in this file is deprecated
// and superseded by the recommendation of using named feature flags with mixin `forFeature($featureName)`.
// =====================================================================================

// The Spartacus team is cautious with introducing style changes in a minor release,
// as it would conflict with the [semantic versioning](https://semver.org/)
// model. To improve the style layer during minor releases, without breaking the
// versioning, a version flag is used. The version is used to control breaking styles
// during the lifecycle of a major version.
//
// Breaking changes are added with the `forVersion()` mixin. The mixin compares the
// from version and (optional) to version, and will only add the mixin content if changes
// are part of the stable release, or explicitly requested by the customer.
//
// An example of the usage of the mixin is given below:
//
// ```scss
// @include forVersion(2.1){
//  // valid when 2.1 is explicit requested or from 3.0 onwards
// }
// ```
//
// Customers can opt-in for specific versions by declaring the `$styleVersion` variable or by
// using the `$useLatestStyles:true` to always be up-to-date with the latest styles.

// Indicates the current major/minor version, which is used to avoid breaking changes.
// Any CSS added later than this version is omitted by default, unless the `$useLatestStyles`
// or `$styleVersion` are enabled.
$_fullVersion: (
  major: 2211,
  minor: 31,
  patch: 1,
);

// The _global_ major version. Any (left over) styles from previous stable versions
// are processed to the final CSS.
$_majorVersion: 2211;

// The `$styleVersion` can be used by customers to explicitly opt-in to breaking style
// changes till the given style version. It must contain a floating number, such as `2.1`.
$styleVersion: $_majorVersion !default;

// For Spartacus development or demo's, the usage of latest styles is recommended.
// To avoid frequent changes of the $minorVersion in the application, we offer a
// flag that can be used to keep up with the latest version at any time.
$useLatestStyles: false !default;

// The `forVersion` mixin is used during development of the style layer. Breaking style
// changes can be added for a specific version, and customers can only use those styles if
// they explicitly enable breaking changes.
//
// The mixin requires a $from version and optional to version. Content can be flagged for
// specific versions or from a specific version onwards.
//
// Examples
// - `forVersion(2)` is valid from major version 2 onwards.
// - `forVersion(2.1)` Valid for version 2.1 and higher, but only added as breaking change
//    or when major version reaches 3.0.
// - `forVersion(2, 2.1)` Valid for version 2 and 2.1. Will no longer be available after 2.1.
//
// The version related content is only created if the user opt-in for breaking changes.

// @deprecated since 2211.20, use `forFeature($feature)` mixin instead
@mixin forVersion($from, $to: 0) {
  // convert variable to version map {major: number, minor: number, patch: number}
  $from: getVersion($from);

  // We never create content that is part of future releases, this should not really happen.
  @if (compareVersion($from, $_fullVersion) <=0) {
    @if (
      (isStableVersion($from, $to) and isValidVersion($from, $to)) or
        addBreakingChange($from, $to)
    ) {
      @content;
    }
  }
}

@function getVersion($value) {
  $major: 0;
  $minor: 0;
  $patch: 0;

  $string: toString($value);
  $i: str-index($string, '.');

  @if $i !=null {
    $major: toNumber(str-slice($string, 1, $i - 1));
    $remaining: str-slice($string, $i + 1);
    $d: str-index($remaining, '.');

    @if $d !=null {
      $minor: toNumber(str-slice($remaining, 1, $d - 1));
      $patch: toNumber(str-slice($remaining, $d + 1));
    } @else {
      $minor: toNumber($remaining);
    }
  } @else {
    $major: toNumber($string);
  }

  @return (major: $major, minor: $minor, patch: $patch);
}

@function toString($value) {
  @if type-of($value) == 'number' {
    @return #{$value};
  } @else if type-of($value) == 'string' {
    @return $value;
  } @else if type-of($value) == 'list' {
    // 1.2.3 will become list [1.2, 0.3]
    // 1.2.0 will become list [1.2, 0]
    // 1.0.2 will become list [1, 0.2]
    $firstItem: toString(nth($value, 1));
    $secondItem: toString(nth($value, 2));
    $firstDot: str-index($firstItem, '.');

    @if $firstDot !=null {
      $secondDot: str-index($secondItem, '.');

      @if $secondDot !=null {
        @return $firstItem + '.' + str-slice($secondItem, $secondDot + 1);
      } @else {
        @return $firstItem;
      }
    } @else {
      @return $firstItem + '.' + $secondItem;
    }
  } @else {
    @error 'Input #{$value} is invalid';
  }
}

@function toNumber($string) {
  $strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
  $result: 0;

  @for $i from 1 through str-length($string) {
    $character: str-slice($string, $i, $i);
    $number: index($strings, $character) - 1;
    $result: $result * 10 + $number;
  }

  @return $result;
}

@function compareVersion($v1, $v2) {
  @if type-of($v1) != 'map' {
    $v1: getVersion($v1);
  }

  @if type-of($v2) != 'map' {
    $v2: getVersion($v2);
  }

  $v1Major: map-get($v1, major);
  $v1Minor: map-get($v1, minor);
  $v1Patch: map-get($v1, patch);

  $v2Major: map-get($v2, major);
  $v2Minor: map-get($v2, minor);
  $v2Patch: map-get($v2, patch);

  $compareMajor: compareNumber($v1Major, $v2Major);

  @if ($compareMajor ==0) {
    $compareMinor: compareNumber($v1Minor, $v2Minor);

    @if ($compareMinor ==0) {
      @return compareNumber($v1Patch, $v2Patch);
    } @else {
      @return $compareMinor;
    }
  } @else {
    @return $compareMajor;
  }
}

@function compareNumber($n1, $n2) {
  @if ($n1 > $n2) {
    @return 1;
  } @else if ($n1 < $n2) {
    @return -1;
  } @else {
    @return 0;
  }
}

// Indicates that from and to versions are valid for the major version.
// - from 0.9 to 1.1 is valid for version 1
// - from 1.0 to 1.2 is valid for version 1
// - from 1.1 to 1.2 is not valid for version 1
// - from 1.1 to 1.2 is not valid for version 2
@function isStableVersion($from, $to: 0) {
  @if ($to ==0) {
    @return compareVersion($from, $_majorVersion) <=0;
  } @else {
    @return compareVersion($from, $_majorVersion) <=0 and
      compareVersion($to, $_majorVersion) >=0;
  }
}

// Indicates that the style rule should be created for the given opt-in rules.
@function addBreakingChange($from, $to) {
  @if (useBreakingChanges() ==false) {
    @return false;
  }

  @return isValidVersion($from, $to);
}

// Indicates that the given from / to versions are valid.
@function isValidVersion($from, $to: 0) {
  @if ($to ==0) {
    // ensure that we opt-in the from version
    @return $useLatestStyles or
      ($useLatestStyles ==false and compareVersion($from, $styleVersion) <=0);
  } @else {
    // ensure that we opt-in the from/to version
    @return ($useLatestStyles and compareVersion($to, $_fullVersion) >=0) or
      (
        $useLatestStyles ==false and compareVersion($from, $styleVersion) <=0
          and compareVersion($to, $styleVersion) >=0
      );
  }
}

// Indicates if breaking changes are requested.
@function useBreakingChanges() {
  @return $useLatestStyles or compareVersion($styleVersion, $_majorVersion) >0;
}
