<script lang="ts">
    import { onDestroy } from "svelte";
    export let text: string;

    let isVisible: boolean = false;
    let timer: number;

    function showTooltip() {
        isVisible = true;
        timer = setTimeout(() => {
            isVisible = false;
        }, 2000);
    }

    function hideTooltip() {
        isVisible = false;
        clearTimeout(timer);
    }

    onDestroy(() => {
        clearTimeout(timer);
    });
</script>

<div class="tooltip" on:touchstart={showTooltip} on:touchend={hideTooltip} on:touchcancel={hideTooltip}>
    <slot />
    <span class="tooltiptext">{text}</span>
</div>

<style>
    .tooltip {
        position: relative;
        display: inline-block;
        cursor: pointer;
    }

    .tooltip .tooltiptext {
        visibility: hidden;
        width: 120px;
        background-color: var(--island-button-color);
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 5px;
        position: absolute;
        z-index: 1;
        top: 50%;
        right: 125%;
        transform: translateY(-50%);
        margin-left: -60px;
        opacity: 0;
        transition: opacity 0.5s;
    }

    .tooltip:hover .tooltiptext {
        visibility: visible;
        opacity: 0.8;
    }
</style>
