import Vue from 'vue'
import PropTypes from '../_util/vue-types'
import { getStyle } from '../_util/props-util'
import BaseMixin from '../_util/BaseMixin'
import createChainedFunction from '../_util/createChainedFunction'
import getTransitionProps from '../_util/getTransitionProps'
import Notice from './Notice'
let seed = 0
const now = Date.now()
function getUuid () {
  return `rcNotification_${now}_${seed++}`
}
const Notification = {
  mixins: [BaseMixin],
  props: {
    prefixCls: PropTypes.string.def('rc-notification'),
    transitionName: PropTypes.string,
    animation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).def('fade'),
  },
  data () {
    return {
      notices: [],
    }
  },
  methods: {
    getTransitionName () {
      const props = this.$props
      let transitionName = props.transitionName
      if (!transitionName && props.animation) {
        transitionName = `${props.prefixCls}-${props.animation}`
      }
      return transitionName
    },
    add (notice) {
      const key = notice.key = notice.key || getUuid()
      this.setState(previousState => {
        const notices = previousState.notices
        if (!notices.filter(v => v.key === key).length) {
          return {
            notices: notices.concat(notice),
          }
        }
      })
    },
    remove (key) {
      this.setState(previousState => {
        return {
          notices: previousState.notices.filter(notice => notice.key !== key),
        }
      })
    },
  },
  render (h) {
    const { prefixCls, notices, remove, getTransitionName } = this
    const transitionProps = getTransitionProps(getTransitionName())
    const noticeNodes = notices.map((notice) => {
      const { content, duration, closable, onClose, key, style, class: className } = notice
      const close = createChainedFunction(remove.bind(this, key), onClose)
      const noticeProps = {
        props: {
          prefixCls,
          duration,
          closable,
        },
        on: {
          close,
        },
        style,
        class: className,
        key,
      }
      return (