import * as _vue from "vue";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { inject } from 'vue';
import PropTypes from '../_util/vue-types';
import { initDefaultProps, getComponent } from '../_util/props-util';
import classNames from '../_util/classNames';
import { ConfigConsumerProps } from '../config-provider';

function noop() {}

export var AnchorLinkProps = {
  prefixCls: PropTypes.string,
  href: PropTypes.string,
  title: PropTypes.any,
  target: PropTypes.string
};
export default {
  name: 'AAnchorLink',
  props: initDefaultProps(AnchorLinkProps, {
    href: '#'
  }),
  setup: function setup() {
    return {
      antAnchor: inject('antAnchor', {
        registerLink: noop,
        unregisterLink: noop,
        scrollTo: noop,
        $data: {}
      }),
      antAnchorContext: inject('antAnchorContext', {}),
      configProvider: inject('configProvider', ConfigConsumerProps)
    };
  },
  watch: {
    href: function href(val, oldVal) {
      var _this = this;

      this.$nextTick(function () {
        _this.antAnchor.unregisterLink(oldVal);

        _this.antAnchor.registerLink(val);
      });
    }
  },
  mounted: function mounted() {
    this.antAnchor.registerLink(this.href);
  },
  beforeUnmount: function beforeUnmount() {
    this.antAnchor.unregisterLink(this.href);
  },
  methods: {
    handleClick: function handleClick(e) {
      this.antAnchor.scrollTo(this.href);
      var scrollTo = this.antAnchor.scrollTo;
      var _this$$props = this.$props,
          href = _this$$props.href,
          title = _this$$props.title;

      if (this.antAnchorContext.$emit) {
        this.antAnchorContext.$emit('click', e, {
          title: title,
          href: href
        });
      }

      scrollTo(href);
    }
  },
  render: function render() {
    var customizePrefixCls = this.prefixCls,
        href = this.href,
        $slots = this.$slots,
        target = this.target;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('anchor', customizePrefixCls);
    var title = getComponent(this, 'title');
    var active = this.antAnchor.$data.activeLink === href;
    var wrapperClassName = classNames("".concat(prefixCls, "-link"), _defineProperty({}, "".concat(prefixCls, "-link-active"), active));
    var titleClassName = classNames("".concat(prefixCls, "-link-title"), _defineProperty({}, "".concat(prefixCls, "-link-title-active"), active));
    return _vue.createVNode("div", {
      "class": wrapperClassName
    }, [_vue.createVNode("a", {
      "class": titleClassName,
      "href": href,
      "title": typeof title === 'string' ? title : '',
      "target": target,
      "onClick": this.handleClick
    }, [title]), $slots.default && $slots.default()]);
  }
};