<script>
import { ref, computed, defineComponent } from "vue";
import { uid } from "@grozav/utils";
import {
  usePopupControl,
  useClickOutside,
  useComponentColor,
  useComponentSize
} from "@inkline/inkline/composables";
const componentName = "ITooltip";
export default defineComponent({
  name: componentName,
  inheritAttrs: false,
  props: {
    /**
     * The color variant of the tooltip
     * @name color
     * @type light | dark
     * @default
     */
    color: {
      type: String,
      default: void 0
    },
    /**
     * The disabled state of the tooltip
     * @name disabled
     * @type Boolean
     * @default false
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * Used to manually control the visibility of the tooltip
     * @name visible
     * @type Boolean
     * @default undefined
     */
    visible: {
      type: Boolean,
      default: void 0
    },
    /**
     * The identifier of the tooltip
     * @name name
     * @type String
     * @default uid()
     */
    name: {
      type: String,
      default: () => {
        return uid("tooltip");
      }
    },
    /**
     * Displays an arrow on the tooltip pointing to the trigger element
     * @name arrow
     * @type Boolean
     * @default true
     */
    arrow: {
      type: Boolean,
      default: true
    },
    /**
     * The placement of the tooltip
     * @name placement
     * @type top | top-start | top-end | bottom | bottom-start | bottom-end | left | left-start | left-end | right | right-start | right-end
     * @default false
     */
    placement: {
      type: String,
      default: "top"
    },
    /**
     * The events used to trigger the tooltip
     * @name events
     * @type hover | focus | click | manual
     * @default [hover, focus]
     */
    events: {
      type: [String, Array],
      default: () => ["hover", "focus"]
    },
    /**
     * The offset of the tooltip relative to the trigger element
     * @name offset
     * @type Number
     * @default 6
     */
    offset: {
      type: Number,
      default: 6
    },
    /**
     * Determines whether hover state should be transferred from trigger to popup
     * @name interactable
     * @type Boolean
     * @default false
     */
    interactable: {
      type: Boolean,
      default: false
    },
    /**
     * Used to override the floating-ui options used for creating the tooltip
     * @name popupOptions
     * @type Object
     * @default {}
     */
    popupOptions: {
      type: Object,
      default: () => ({})
    },
    /**
     * The size variant of the tooltip
     * @name size
     * @type sm | md | lg
     * @default
     */
    size: {
      type: String,
      default: void 0
    },
    /**
     * Delay in milliseconds before the tooltip is hidden on hover
     * @name hoverHideDelay
     * @type Number
     * @default 300
     */
    hoverHideDelay: {
      type: Number,
      default: 300
    },
    /**
     * Animation duration in milliseconds
     * @name animationDuration
     * @type Number
     * @default 300
     */
    animationDuration: {
      type: Number,
      default: 300
    }
  },
  emits: [
    /**
     * Event emitted for setting the visible
     * @event update:visible
     */
    "update:visible",
    /**
     * Event emitted when clicking outside the tooltip
     * @event click:outside
     */
    "click:outside"
  ],
  setup(props, { emit }) {
    const wrapperRef = ref(null);
    const triggerRef = ref(null);
    const popupRef = ref(null);
    const arrowRef = ref(null);
    const currentColor = computed(() => props.color);
    const currentSize = computed(() => props.size);
    const { color } = useComponentColor({ componentName, currentColor });
    const { size } = useComponentSize({ componentName, currentSize });
    const componentProps = computed(() => ({
      disabled: props.disabled,
      events: props.events,
      placement: props.placement,
      interactable: props.interactable,
      visible: props.visible,
      animationDuration: props.animationDuration,
      hoverHideDelay: props.hoverHideDelay,
      offset: props.offset
    }));
    const { visible, onKeyEscape, onClickOutside } = usePopupControl({
      triggerRef,
      popupRef,
      arrowRef,
      componentProps,
      emit
    });
    useClickOutside({ elementRef: wrapperRef, fn: onClickOutside });
    const classes = computed(() => {
      return {
        [`-${color.value}`]: true,
        [`-${size.value}`]: true
      };
    });
    return {
      wrapperRef,
      triggerRef,
      popupRef,
      arrowRef,
      visible,
      onKeyEscape,
      classes
    };
  }
});
</script>

<template>
    <div
        v-bind="$attrs"
        :id="name"
        ref="wrapperRef"
        class="tooltip-wrapper"
        :class="classes"
        @keyup.esc="onKeyEscape"
    >
        <div
            ref="triggerRef"
            class="tooltip-trigger"
            :aria-describedby="`${name}-popup`"
            :aria-disabled="disabled"
            :aria-expanded="visible ? 'true' : 'false'"
        >
            <!-- @slot default Slot for tooltip trigger -->
            <slot />
        </div>

        <transition name="zoom-in-top-transition">
            <div
                v-show="visible"
                :id="`${name}-popup`"
                ref="popupRef"
                class="tooltip"
                role="tooltip"
                aria-live="polite"
                :aria-hidden="visible ? 'false' : 'true'"
            >
                <span v-if="arrow" ref="arrowRef" class="arrow" />
                <!-- @slot body Slot for tooltip body content -->
                <slot name="body" />
            </div>
        </transition>
    </div>
</template>
