@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';
@use 'is-number' as *;

/// Clamps `$value` between `$min` and `$max`. Because the CSS version of
/// `clamp()` still does not have full support for all Safari versions in use,
/// this version will provide the same effect with the more supported `min()`
/// and `max()` methods but with the more simplified clamp() syntax that is the
/// same as in the CSS version.
///
/// @param {Number} $min - The minimum value.
/// @param {Number} $value - The number to clamp between $min and $max.
/// @param {Number} $max - The maximum value.
///
/// @return {Calculation} If $value is within the limits, returns the `$value`,
/// if it is below the limit of `$min`, it will return `$min`, if it is above
/// the limit of `$max`, it will return `$max`.
///
/// @access public
/// @group Utilities
/// @require {function} is-number
///
/// @throw $min value greater than $max value.
@function clamp-val($min, $value, $max) {
  @if is-number($min) and
    is-number($max) and
    math.compatible($min, $max) and
    $min > $max
  {
    @error 'Invalid $min and $max value of [ $min: #{$min}, $max: #{$max} ] ' +
         'for the [ clamp-size() ] function. The minimum value must be less ' +
         'than the maximum value.';
  }

  @return min(max(#{'#{$min}'}, #{'#{$value}'}), #{'#{$max}'});
}

/// Clamps `$value` between `$min` and `$max`. Because the CSS version of
/// `clamp()` still does not have full support for all Safari versions in use,
/// this version will provide the same effect with the more supported `min()`
/// and `max()` methods but with the more simplified clamp() syntax that is the
/// same as in the CSS version.
///
/// @param {Number} $min - The minimum value.
/// @param {Number} $value - The number to clamp between $min and $max.
/// @param {Number} $max - The maximum value.
///
/// @return {Calculation} If $value is within the limits, returns the `$value`,
/// if it is below the limit of `$min`, it will return `$min`, if it is above
/// the limit of `$max`, it will return `$max`.
///
/// @access public
/// @group Utilities
/// @require {function} clamp-val
///
/// @throw $min value greater than $max value.
///
/// @alias clamp-val
@function clamp-value($min, $value, $max) {
  @return clamp-val($min, $value, $max);
}
