import SwiftUI

// MARK: - SpotlightShape

/// A Shape that fills the entire rect with an even-odd "hole" cut out for the spotlight.
/// The filled region is everything OUTSIDE `spotlight`; the cutout is transparent.
///
/// Usage:
/// ```swift
/// SpotlightShape(spotlight: frame, cornerRadius: 18)
///     .fill(Color.black.opacity(0.45), style: FillStyle(eoFill: true))
/// ```
struct SpotlightShape: Shape {

    var spotlight: CGRect
    var cornerRadius: CGFloat

    // MARK: Animatable

    var animatableData: AnimatablePair<
        AnimatablePair<CGFloat, CGFloat>,
        AnimatablePair<AnimatablePair<CGFloat, CGFloat>, CGFloat>
    > {
        get {
            AnimatablePair(
                AnimatablePair(spotlight.origin.x, spotlight.origin.y),
                AnimatablePair(
                    AnimatablePair(spotlight.size.width, spotlight.size.height),
                    cornerRadius
                )
            )
        }
        set {
            spotlight = CGRect(
                x: newValue.first.first,
                y: newValue.first.second,
                width: newValue.second.first.first,
                height: newValue.second.first.second
            )
            cornerRadius = newValue.second.second
        }
    }

    // MARK: Path

    func path(in rect: CGRect) -> Path {
        var path = Path()
        // Outer rect (full screen)
        path.addRect(rect)
        // Spotlight cutout (rounded rectangle)
        path.addRoundedRect(
            in: spotlight,
            cornerSize: CGSize(width: cornerRadius, height: cornerRadius)
        )
        return path
    }
}
