@use "sass:map";
@use "../../dev" as *;
@use "../../variables" as *;

// ============================================================================
// Helper Functions
// ============================================================================

/// Helper function to replace negative values with 0
/// @param {List} $radius - The radius value(s) to validate
/// @return {List} - The validated radius values with negatives replaced by 0
@function valid-radius($radius) {
    $return: ();
    @each $value in $radius {
        @if type-of($value) == number {
            $return: append($return, max($value, 0));
        } @else {
            $return: append($return, $value);
        }
    }
    @return $return;
}

/// Shape radius tokens
$object_shape_radius: (
    squared: 0,
    rounded: q(2),
    pill: 50vh,
) !default;

/// General-purpose shape mixin
/// @param {String} $variant - One of `squared`, `rounded`, `pill`
/// @group ObjectShape
@mixin object--corner($variant) {
    @if map.has-key($object_shape_radius, $variant) {
        border-radius: map.get($object_shape_radius, $variant) !important;
    } @else {
        @warn "Unknown shape variant: #{$variant}";
    }
}

/// Mixin to apply rounded corners to specific corners of an element.
/// @param {Number} $top-left - The radius for the top-left corner.
/// @param {Number} $top-right - The radius for the top-right corner.
/// @param {Number} $bottom-right - The radius for the bottom-right corner.
/// @param {Number} $bottom-left - The radius for the bottom-left corner.
@mixin object--corner--custom(
    $top-left: 0,
    $top-right: 0,
    $bottom-right: 0,
    $bottom-left: 0
) {
    border-top-left-radius: $top-left !important;
    border-top-right-radius: $top-right !important;
    border-bottom-right-radius: $bottom-right !important;
    border-bottom-left-radius: $bottom-left !important;
}

/// Mixin for setting a border radius of 0 (no border radius).
@mixin object--corner--0 {
    border-radius: 0;
}

/// Mixin for setting a border radius of 50% (creates a circular border radius for square elements).
@mixin object--corner--50 {
    border-radius: 50%;
}

/// Mixin for setting a border radius of 100% (makes an element fully round).
@mixin object--corner--100 {
    border-radius: 100%;
}

/// Specific mixins for shape variants
@mixin object--corner--squared {
    @include object--corner(squared);
}

@mixin object--corner--rounded {
    @include object--corner(rounded);
}

@mixin object--corner--pill {
    @include object--corner(pill);
}

/// Generate utility classes for each shape variant
@each $key, $val in $object_shape_radius {
    .object--corner--#{$key} {
        border-radius: $val !important;
    }
}

// ============================================================================
// Edge-Specific Corner Mixins
// ============================================================================

/// Apply border radius to all corners with validation
/// @param {Number|List} $radius - The radius value to apply
@mixin object--corner--radius($radius) {
    border-radius: valid-radius($radius);
}

/// Apply border radius to top edge (top-left and top-right corners)
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--top($radius: q(2)) {
    border-top-left-radius: valid-radius($radius);
    border-top-right-radius: valid-radius($radius);
}

/// Apply border radius to end edge (top-right and bottom-right corners)
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--end($radius: q(2)) {
    border-top-right-radius: valid-radius($radius);
    border-bottom-right-radius: valid-radius($radius);
}

/// Apply border radius to bottom edge (bottom-left and bottom-right corners)
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--bottom($radius: q(2)) {
    border-bottom-right-radius: valid-radius($radius);
    border-bottom-left-radius: valid-radius($radius);
}

/// Apply border radius to start edge (top-left and bottom-left corners)
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--start($radius: q(2)) {
    border-top-left-radius: valid-radius($radius);
    border-bottom-left-radius: valid-radius($radius);
}

// ============================================================================
// Individual Corner Mixins
// ============================================================================

/// Apply border radius to top-start (top-left) corner only
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--top-start($radius: q(2)) {
    border-top-left-radius: valid-radius($radius);
}

/// Apply border radius to top-end (top-right) corner only
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--top-end($radius: q(2)) {
    border-top-right-radius: valid-radius($radius);
}

/// Apply border radius to bottom-end (bottom-right) corner only
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--bottom-end($radius: q(2)) {
    border-bottom-right-radius: valid-radius($radius);
}

/// Apply border radius to bottom-start (bottom-left) corner only
/// @param {Number} $radius [q(2)] - The radius value to apply
@mixin object--corner--bottom-start($radius: q(2)) {
    border-bottom-left-radius: valid-radius($radius);
}
