@use 'sass:meta' as *;
@use '../../base' as *;

/// Emits the scrollbar tokens and the standard scrollbar properties.
/// `scrollbar-color`/`scrollbar-width` substitute their `var()`s on the declaring element and
/// inherit the resolved value, so every scope that overrides the tokens must include this mixin.
/// @access private
/// @requires {mixin} css-vars-from-theme
/// @param {Map} $theme - The theme used to style the component.
/// @param {String} $scope [null] - The selector the tokens and styles are scoped to.
@mixin scrollbar($theme, $scope: null) {
    $_scope: if(sass($scope): $scope; else: '.ig-scrollbar');

    // When nested, the parent selector gets the tokens too, so the whole subtree inherits them.
    // `&` is only valid when nested; at root it leaks out and invalidates the selector list.
    @if is-root() {
        #{$_scope} {
            @include css-vars-from-theme($theme);
            @include _scrollbar-properties($theme);
        }
    } @else {
        &,
        #{$_scope} {
            @include css-vars-from-theme($theme);
            @include _scrollbar-properties($theme);
        }
    }
}

/// @access private
/// @param {Map} $theme - The theme used to style the component.
@mixin _scrollbar-properties($theme) {
    // `scrollbar-width` takes auto | thin | none only, so the length-based `sb-size` can't drive it.
    scrollbar-width: var(--sb-width, auto);
    scrollbar-color: var-get($theme, 'sb-thumb-bg-color') var-get($theme, 'sb-track-bg-color');
}
