@use 'sass:meta';
@use 'sass:list';
@use 'sass:string';
@use '../../functions/conv-color-name' as *;
@use '../../functions/is-color' as *;
@use '../../functions/is-string' as *;
@use '../../functions/is-true' as *;
@use '../../functions/is-valid-hex' as *;

/// Use on the wrapper for an element with the a clip-path effect applied and
/// it will add a drop shadow to the clipped shape.
///
/// @param {Color} $shadow-color [rgba(50, 50, 0, 0.5)] The shadow's color that
/// will be passed to the filter property's drop-shadow function.
/// @param {List} $shadow-vals [-1px 6px 3px] - The values passed before the
/// color in the drop-shadow function that is used with the `filter` property.
/// @param {Bool} $turn-off [false] - Pass true to turn off the drop-shadow
/// effect with `filter: none`.
///
/// @group Shapes
/// @require {function} conv-color-name
/// @require {function} is-color
/// @require {function} is-string
/// @require {function} is-true
/// @require {function} is-valid-hex
///
/// @throw Invalid color value for $shadow-color
/// @throw Invalid data type for $shadow-vals
@mixin shape-shadow(
  $shadow-color: rgba(50, 50, 0, 0.5),
  $shadow-vals: -1px 6px 3px,
  $turn-off: false
) {
  $parenthesis-index: null;

  @if $shadow-color and is-string($shadow-color) {
    $parenthesis-index: string.index($shadow-color, '(');
  }

  @if is-color($shadow-color) {
    $shadow-color: conv-color-name($shadow-color);
  } @else if not (
    is-string($shadow-color) and (
      is-valid-hex($shadow-color) or
      (
        $parenthesis-index and
        list.index(
          ('rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'lab', 'lch'),
          string.to-lower-case(
            string.slice(
              $shadow-color,
              1,
              $parenthesis-index - 1
            )
          )
        ) and
        ($parenthesis-index == 4 or $parenthesis-index == 5)
      ) or
      $shadow-color == 'transparent'
    )
  ) {
    @error 'Invalid $shadow-color value of #{meta.inspect($shadow-color)} ' +
        'passed to the [ shape-shadow() ] mixin. You must pass a color for ' +
        'this parameter.';
  }

  @if not is-list($shadow-vals) or list.length($shadow-vals) > 3 {
    @error 'Invalid parameter of `#{meta.inspect($shadow-vals)}` passed as ' +
        'the $shadow-vals parameter to the [ shape-shadow() ] mixin. This ' +
        'value must be a list of 3 length values (usually px) that will make ' +
        'up the values passed to the CSS `drop-shadow()` function which is ' +
        'used with the `filter` property. The default value for this parameter ' +
        'is: (-1px 6px 3px).';
  }

  @if not $shadow-color or not $shadow-vals or is-true($turn-off) {
    filter: none;
  } @else {
    filter: drop-shadow(#{$shadow-vals} #{$shadow-color});
  }
}
