@use 'sass:color';
@use 'sass:list';
@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';
@use 'conv-color' as *;
@use 'get-color-type' as *;
@use 'is-color' as *;
@use 'is-string' as *;
@use '../variables/colors' as *;
@use '../variables/misc' as *;

/// Determines whether to return a light or dark contrast color for text based
/// on the given background color.
///
/// @param {Color} $color - The color you are finding the contrast for.
/// Must be a color type recognized by Sass.
///
/// @param {String} $syntax [get-color-type($color)] - The desired syntax of the
/// returned color. Defaults to the color notation used by `$color`. Can be
/// 'hex', '#', 'rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'function', 'name', or 'cmyk'.
///
/// @return {Color} - The contrast color in the given syntax.
///
/// @access public
/// @group Utilities
/// @require {function} conv-color
/// @require {function} get-color-type
/// @require {function} is-color
/// @require {function} is-string
/// @require {variable} $text-contrast-threshold
/// @require {variable} $contrast-text-dark
/// @require {variable} $contrast-text-light
///
/// @throw Invalid $color data type, must be a color.
/// @throw Invalid $syntax data type, must be a string.
/// @throw Invalid $syntax value, must be a color syntax string.
@function color-text-contrast($color, $syntax: get-color-type($color)) {
  $valid-syntax-values: (
    'hex', '#', 'rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'function', 'name', 'cmyk'
  );

  @if not is-color($color) {
    @error 'Invalid $color data type of #{meta.inspect(meta.type-of($color))} ' +
        'passed to the [ color-contrast() ] function. This value is not ' +
        'currently recognized as a color by your version of Sass.';
  }

  @if $syntax and is-string($syntax) {
    $syntax: string.to-lower-case($syntax);
  } @else if not $syntax {
    $syntax: get-color-type($color);
  } @else {
    @error 'Invalid $sytax data type of #{meta.type-of($syntax)} passed to ' +
        'the [ color-contrast() ] function. The $syntax parameter must be ' +
        'of type \'string\' or be `null` or `false`. This function supports ' +
        'converting into the following $syntax values: ' +
        '[ \'hex\', \'#\', \'rgb\', \'rgba\', \'hsl\', \'hsla\', \'hwb\', ' +
        '\'function\', \'name\', \'cmyk\' ].';
  }

  @if not list.index($valid-syntax-values, $syntax) {
    @error 'Invalid $syntax value of #{meta.inspect($syntax)} passed to ' +
        'the [ color-complement() ] function. Value of $syntax must be a string ' +
        'representing a valid CSS color function specification. This function ' +
        'supports converting into the following $syntax values: ' +
        '[ \'hex\', \'#\', \'rgb\', \'rgba\', \'hsl\', \'hsla\', \'hwb\', ' +
        '\'function\', \'name\', \'cmyk\' ].';
  }

  $red: color.red($color);
  $green: color.green($color);
  $blue: color.blue($color);
  $alpha: color.alpha($color);
  $yiq: math.div((($red * 299) + ($green * 587) + ($blue * 114)), 1000);

  @if $yiq >= $text-contrast-threshold {
    @return conv-color($contrast-text-dark, $syntax);
  } @else {
    @return conv-color($contrast-text-light, $syntax);
  }
}
