<template>
    <div class="guide" v-if="mount && (msg || $slots.default)" ref="guideRef" :class="{ 'floating-mode': floatClass }">
        <slot name="content" :show="shouldShow"></slot>
        <UIFloating v-if="floatClass" :mount="mountEl" :refer="guideRef" dire="bottom" :class="`guide-floating ${props.floatClass}  ${type}`">
            <Bubble :show="shouldShow" class="guide-bubble" :type="type">
                <template v-if="msg">
                    <p class="default-msg">{{ msg }}</p>
                </template>
                <slot v-else></slot>

                <span class="guide-close" @click="shouldShow = false">
                    <ui-icon type="close" ctrl />
                </span>
            </Bubble>
        </UIFloating>

        <Bubble :show="shouldShow" class="guide-bubble" :type="type" v-else>
            <template v-if="msg">
                <p class="default-msg">{{ msg }}</p>
            </template>
            <slot v-else></slot>

            <span class="guide-close" @click="shouldShow = false">
                <ui-icon type="close" ctrl />
            </span>
        </Bubble>
    </div>
    <slot name="content" v-else :show="false"></slot>
</template>

<script setup>
import Bubble from '../bubble'
import UIFloating from '../floating/index.vue'
import { ref, watch } from 'vue'

const props = defineProps({
    mark: {
        type: String,
    },
    msg: {
        type: String,
    },
    floatClass: {
        type: String,
    },
    type: {
        type: String,
        default: 'top',
    },
})

const mountEl = document.body
const guideRef = ref()
const shouldShow = ref(true)
if (props.mark) {
    shouldShow.value = !localStorage.getItem(props.mark)
    if (shouldShow.value) {
        watch(shouldShow, (newv, oldv) => {
            if (!newv && oldv) {
                setTimeout(() => {
                    localStorage.setItem(props.mark, 1)
                })
            }
        })
    }
} else {
    shouldShow.value = true
}

const mount = ref(shouldShow.value)
</script>

<script>
export default { name: 'UiGuide' }
</script>
