import SwiftUI

extension View {
    /// Applies the iOS 26 Liquid Glass effect when available, falling back to
    /// `ultraThinMaterial` on earlier OSes. Pass a clipping shape so the glass
    /// has the right silhouette.
    @ViewBuilder
    func glassSurface<S: Shape>(in shape: S = RoundedRectangle(cornerRadius: 22, style: .continuous)) -> some View {
        if #available(iOS 26.0, macOS 26.0, *) {
            self.glassEffect(.regular, in: shape)
        } else {
            self
                .background(.ultraThinMaterial, in: shape)
                .overlay(shape.stroke(Color.white.opacity(0.08), lineWidth: 1))
        }
    }

    /// Interactive version of `glassSurface` — pressed states animate.
    @ViewBuilder
    func interactiveGlassSurface<S: Shape>(in shape: S = Capsule()) -> some View {
        if #available(iOS 26.0, macOS 26.0, *) {
            self.glassEffect(.regular.interactive(), in: shape)
        } else {
            self
                .background(.thinMaterial, in: shape)
                .overlay(shape.stroke(Color.white.opacity(0.10), lineWidth: 1))
        }
    }

    /// Replaces a button's chrome with a Liquid Glass capsule when available.
    @ViewBuilder
    func glassButton(prominent: Bool = false) -> some View {
        if #available(iOS 26.0, macOS 26.0, *) {
            if prominent {
                self.buttonStyle(.glassProminent)
            } else {
                self.buttonStyle(.glass)
            }
        } else {
            if prominent {
                self.buttonStyle(.borderedProminent)
            } else {
                self.buttonStyle(.bordered)
            }
        }
    }

    /// Soft top-edge fade so content slips under the floating toolbar without
    /// a hard cutoff.
    @ViewBuilder
    func softScrollEdge() -> some View {
        if #available(iOS 26.0, macOS 26.0, *) {
            self.scrollEdgeEffectStyle(.soft, for: .top)
        } else {
            self
        }
    }
}

/// Pretty-prints a port number without locale grouping ("5900", not "5,900").
@inlinable
func portLabel(_ port: Int) -> String {
    String(port)
}
