<template>
    <span
        :id="`label-${data.id}`"
        @mouseover="hoverHandler"
        @mouseleave="unhoverHandler"
        v-bind:class="{highlightText: isHighlight, loadingTooltip: isLoading}"
    >
        {{ data.title }}
        <b-tooltip 
            :target="`label-${data.id}`"
            :show.sync="showTooltip"
            v-if="showTooltip"
        >
            {{ labelTooltip }}
        </b-tooltip>
    </span>
</template>

<script>
import api from "./../../api/index";

export default {
    name: "CustomTooltip",
    props: {
        data: Object
    },
    data() {
        return {
            labelTooltip: "",
            hovering: "",
            show: false,
            menuMap: {
                CASES_INBOX: "inbox",
                CASES_DRAFT: "draft",
                CASES_PAUSED: "paused",
                CASES_SELFSERVICE: "unassigned"
            },
            isHighlight: false,
            showTooltip: false,
            isLoading: false,
            loading: ""
        };
    },
    methods: {
        /**
         * Delay the hover event
         */
        hoverHandler() {
            this.isLoading = true;
            this.hovering = setTimeout(() => { this.setTooltip() }, 3000);
        },
        /**
         * Reset the delay and hide the tooltip
         */
        unhoverHandler() {
            this.labelTooltip = "";
            this.showTooltip = false;
            this.isLoading = false;
            clearTimeout(this.hovering);
        },
        /**
         * Set the label to show in the tooltip
         */
        setTooltip() {
            let that = this;
            api.menu.getTooltip(that.data.page).then((response) => {
                that.showTooltip = true;
                that.isLoading = false;
                that.labelTooltip = response.data.label;
            });
        },
        /**
         * Set bold the label 
         */
        setHighlight() {
            this.isHighlight = true;
        }
    },
};
</script>
<style>
.highlightText {
    font-weight: 900;
}
.loadingTooltip {
    cursor: wait;
}
</style>
