// Lightning Design System 2.29.1
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license

// Calculate PX into EM
// Use - width: pem(600px);
// Output - width: 37.5em;
@function pem($pixels) {
  @return (math.div($pixels, 16px) * 1em);
}

// Calculate PX into REM
// Use - width: rem(600px);
// Output - width: 37.5rem;
@function rem($pixels) {
  @return (math.div($pixels, 16px) * 1rem);
}

// Replace `$search` with `$replace` in `$string`
// @author Hugo Giraudel
// Use - str-replace($string, ' ', '-')
@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;
}

/// Fix IE Rounding
///
/// IE computes 0.875rem into 13.92px instead of 14px
/// but is better at rounding values using calc()
/// so we use calc as a hack to get the correct width
///
/// This is required for closely positioning elements on a pixel grid
/// e.g. in an icon where pixel perfection is important
///
/// @param {Number} $width - unitless width
/// @param {Number} $base-font-size [16]
/// @return {Expression} same as $width, but in rem using calc - e.g. calc(2rem / 16)
@function fix-ie-rounding($width, $base-font-size: 16) {
  @return calc(#{$width * 1rem} / #{$base-font-size});
}

/// Remove the unit of a value with units, similar to JavaScript parseInt()
///
/// When a value of a calculation is needed without units (e.g. rem, px).
/// Helpful when determining the value of `transform: scale()`, and the
/// calculation involves a size token.
/// Example: transform: scale( 1 / strip-unit($font-size-heading-small) );
/// More info: https://css-tricks.com/snippets/sass/strip-unit-function/
///
/// @param {Number} $number - Number to remove unit from
/// @return {Number} - Unitless number
@function strip-unit($number) {
  @if type-of($number) == 'number' and not unitless($number) {
    @return math.div($number, $number * 0 + 1);
  }

  @return $number;
}

// Calculate value line-height as if it has box-sizing: border-box behavior
// Useful for blueprints that rely on line-height for their overall height
@function border-box-line-height($value, $border: $border-width-thin * 2, $padding: 0) {
  @if unitless($border) {
    $border: 0px; // stylelint-disable length-zero-no-unit
  }
  $box-model: 0;

  @if comparable($border, $padding) {
    $box-model: rem($border + $padding);
  } @else {
    $box-model: (rem($border) + $padding);
  }

  @if comparable($value, $box-model) {
    @return ($value - $box-model);
  } @else {
    @return (rem($value) - $box-model);
  }
}
