<script lang="ts">
import { defineComponent, Fragment, createTextVNode, renderSlot, toDisplayString, createCommentVNode, withDirectives, Teleport, createVNode } from 'vue'
import PsPopper from '@present-ui/popper'
import { defaultProps, Effect } from '@present-ui/popper'
import { renderPopper, renderTrigger, renderArrow } from '@present-ui/popper'
import { ClickOutside } from '@present-ui/directives'
import { warn } from '@present-ui/utils/error'
import { renderBlock, renderIf, PatchFlags } from '@present-ui/utils/vnode'
import usePopover, { SHOW_EVENT, HIDE_EVENT } from './usePopover'

import type { PropType } from 'vue'
import type { TriggerType } from '@present-ui/popper'

const emits = ['update:visible', 'after-enter', 'after-leave', SHOW_EVENT, HIDE_EVENT ]
const NAME = 'PsPopover'

const _hoist = { key: 0, class: 'ps-popover__title', role: 'title' }

export default defineComponent({
  name: NAME,
  components: {
    PsPopper,
  },
  props: {
    ...defaultProps,
    content: {
      type: String,
    },
    trigger: {
      type: String as PropType<TriggerType>,
      default: 'click',
    },
    title: {
      type: String,
    },
    transition: {
      type: String,
      default: 'fade-in-linear',
    },
    width: {
      type: [String, Number],
      default: 150,
    },
    appendToBody: {
      type: Boolean,
      default: true,
    },
  },
  emits,
  setup(props, ctx) {
    if (process.env.NODE_ENV !== 'production' && props.visible && !ctx.slots.reference) {
      warn(NAME, `
        You cannot init popover without given reference
      `)
    }
    const states = usePopover(props, ctx)

    return states
  },
  render() {
    const { $slots } = this
    const trigger = $slots.reference ? $slots.reference() : null

    const title = renderIf(this.title, 'div', _hoist, toDisplayString(this.title), PatchFlags.TEXT)

    const content = renderSlot($slots, 'default', {},
      () => [createTextVNode(toDisplayString(this.content), PatchFlags.TEXT)])

    const {
      events,
      onAfterEnter,
      onAfterLeave,
      onPopperMouseEnter,
      onPopperMouseLeave,
      popperStyle,
      popperId,
      popperClass,
      showArrow,
      transition,
      visibility,
    } = this

    const kls = [
      this.content ? 'ps-popover--plain' : '',
      'ps-popover',
      popperClass,
    ].join(' ')

    let popover = renderPopper({
      effect: Effect.LIGHT,
      name: transition,
      popperClass: kls,
      popperStyle: popperStyle,
      popperId,
      visibility,
      onMouseenter: onPopperMouseEnter,
      onMouseleave: onPopperMouseLeave,
      onAfterEnter,
      onAfterLeave,
      stopPopperMouseEvent: false,
    }, [
      title,
      content,
      renderArrow(showArrow),
    ],
    )

    // when user uses popover directively, trigger will be null so that we only
    // render a popper window for displaying contents
    const _trigger = trigger ? renderTrigger(trigger, {
      ariaDescribedby: popperId,
      ref: 'triggerRef',
      ...events,
    }) : createCommentVNode('v-if', true)

    return renderBlock(Fragment, null, [
      this.trigger === 'click'
        ? withDirectives(_trigger, [[ClickOutside, this.hide]])
        : _trigger,
      createVNode(Teleport as any, {
        disabled: !this.appendToBody,
        to: 'body',
      }, [popover], PatchFlags.PROPS, ['disabled']),
    ])
  },
})
</script>
