@use "../config/feature-flags" as config-flags;
@use "../functions/feature-flags" as fn-flags;
@mixin tooltip-variables {
    // Tooltip core variables
    --tooltip-bg: rgb(17 17 17 / 90%);
    --tooltip-shadow-color: rgb(0 0 0 / 20%);
    --tooltip-text-color: #fff;

    // Animation variables
    --tooltip-transition-duration: 0.18s;
    --tooltip-transition-easing: ease-in-out;
    --tooltip-transition-delay: 0s;

    // Typography variables
    --tooltip-font-size: 13px;
    --tooltip-font-weight: normal;
    --tooltip-text-transform: none;

    // Sizing variables
    --tooltip-small-width: 80px;
    --tooltip-medium-width: 150px;
    --tooltip-large-width: 260px;
    --tooltip-border-radius: 4px;
}

@if fn-flags.feature-enabled("tooltips") {
    @layer components {
        // Apply variables based on parent selector
        @if config-flags.$parent-selector == "" {
            :root {
                @include tooltip-variables;
            }
        } @else {
            #{config-flags.$parent-selector} {
                @include tooltip-variables;
            }
        }

        #{config-flags.$parent-selector} [data-tooltip][role~="tooltip"] {
            position: relative;

            &::before,
            &::after {
                transform: translate3d(0, 0, 0);
                backface-visibility: hidden;
                will-change: transform;
                opacity: 0;
                pointer-events: none;
                transition:
                    opacity var(--tooltip-transition-duration) var(--tooltip-transition-easing) var(--tooltip-transition-delay),
                    transform var(--tooltip-transition-duration) var(--tooltip-transition-easing) var(--tooltip-transition-delay);
                position: absolute;
                box-sizing: border-box;
                z-index: 10;
                transform-origin: top;
            }

            &::before {
                background-size: 100% auto !important;
                content: "";
            }

            &::after {
                background: var(--tooltip-bg);
                box-shadow: 0 3px 7px var(--tooltip-shadow-color);
                border-radius: var(--tooltip-border-radius);
                color: var(--tooltip-text-color);
                content: attr(data-tooltip);
                font-size: var(--tooltip-font-size);
                font-weight: var(--tooltip-font-weight);
                text-transform: var(--tooltip-text-transform);
                padding: 0.5em 1em;
                white-space: nowrap;
                box-sizing: content-box;
            }

            &:hover::before,
            &:hover::after,
            &:focus::before,
            &:focus::after {
                opacity: 1;
                pointer-events: auto;
            }
        }

        // Top position tooltips
        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position|="top"] {
            &::before {
                border-left: 9px solid transparent;
                border-right: 9px solid transparent;
                border-top: 6px solid var(--tooltip-bg);
                height: 0;
                width: 0;
                margin-bottom: 6px;
                transform: translate3d(-50%, 0, 0);
                bottom: 100%;
                left: 50%;
                z-index: 999;
            }

            &::after {
                margin-bottom: 11px;
                transform: translate3d(-50%, 0, 0);
                bottom: 100%;
                left: 50%;
            }

            &:hover::before {
                transform: translate3d(-50%, -5px, 0);
            }

            &:hover::after {
                transform: translate3d(-50%, -5px, 0);
            }
        }

        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position="top-left"] {
            &::after {
                transform: translate3d(calc(-100% + 16px), 0, 0);
                bottom: 100%;
            }

            &:hover::after {
                transform: translate3d(calc(-100% + 16px), -5px, 0);
            }
        }

        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position="top-right"] {
            &::after {
                transform: translate3d(calc(0% + -16px), 0, 0);
                bottom: 100%;
            }

            &:hover::after {
                transform: translate3d(calc(0% + -16px), -5px, 0);
            }
        }

        // Bottom position tooltips
        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position|="bottom"] {
            &::before {
                border-left: 9px solid transparent;
                border-right: 9px solid transparent;
                border-bottom: 6px solid var(--tooltip-bg);
                height: 0;
                width: 0;
                margin-top: 5px;
                margin-bottom: 0;
                transform: translate3d(-50%, -10px, 0);
                bottom: auto;
                left: 50%;
                top: 100%;
                z-index: 999;
            }

            &::after {
                margin-top: 11px;
                transform: translate3d(-50%, -10px, 0);
                top: 100%;
                left: 50%;
            }

            &:hover::before {
                transform: translate3d(-50%, 0, 0);
            }

            &:hover::after {
                transform: translate3d(-50%, 0, 0);
            }
        }

        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position="bottom-left"] {
            &::after {
                transform: translate3d(calc(-100% + 16px), -10px, 0);
                top: 100%;
            }

            &:hover::after {
                transform: translate3d(calc(-100% + 16px), 0, 0);
            }
        }

        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position="bottom-right"] {
            &::after {
                transform: translate3d(calc(0% + -16px), -10px, 0);
                top: 100%;
            }

            &:hover::after {
                transform: translate3d(calc(0% + -16px), 0, 0);
            }
        }

        // Left position tooltips
        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position="left"] {
            &::before,
            &::after {
                inset: auto auto auto 100%;
                left: auto; // Remove any left positioning
                right: 100%; // Position to the left of the element
                top: 50%;
                transform: translate3d(10px, -50%, 0);
            }

            &::before {
                border-top: 9px solid transparent;
                border-bottom: 9px solid transparent;
                border-left: 6px solid var(--tooltip-bg);
                height: 0;
                width: 0;
                margin-right: 5px;
                margin-bottom: 0;
                z-index: 999;
            }

            &::after {
                margin-right: 11px;
            }

            &:hover::before,
            &:hover::after {
                transform: translate3d(0, -50%, 0);
            }
        }

        // Right position tooltips
        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-position="right"] {
            &::before,
            &::after {
                bottom: auto;
                left: 100%;
                top: 50%;
                transform: translate3d(-10px, -50%, 0);
            }

            &::before {
                border-top: 9px solid transparent;
                border-bottom: 9px solid transparent;
                border-right: 6px solid var(--tooltip-bg);
                height: 0;
                width: 0;
                margin-bottom: 0;
                margin-left: 5px;
                z-index: 999;
            }

            &::after {
                margin-left: 11px;
            }

            &:hover::before,
            &:hover::after {
                transform: translate3d(0, -50%, 0);
            }
        }

        // Tooltip sizes
        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-size="small"]::after {
            white-space: initial;
            width: var(--tooltip-small-width);
        }

        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-size="medium"]::after {
            white-space: initial;
            width: var(--tooltip-medium-width);
        }

        #{config-flags.$parent-selector} [role~="tooltip"][data-tooltip-size="large"]::after {
            white-space: initial;
            width: var(--tooltip-large-width);
        }
    } // end @layer components
}
