'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var context = require('./context.cjs');
var props = require('./props.cjs');
var _class = require('../../../utils/dom/class.cjs');

var AAnchorLink = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAnchorLink",
  inheritAttrs: false,
  props: props.anchorLinkProps(),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    let mergedTitle = null;
    const {
      handleClick: contextHandleClick,
      scrollTo,
      unregisterLink,
      registerLink,
      activeLink
    } = context.useInjectAnchor();
    const {
      prefixCls
    } = useConfigInject.default("anchor", props);
    const handleClick = (e) => {
      const {
        href
      } = props;
      contextHandleClick(e, {
        title: mergedTitle,
        href
      });
      scrollTo(href);
    };
    vue.watch(() => props.href, (val, oldVal) => {
      vue.nextTick(() => {
        unregisterLink(oldVal);
        registerLink(val);
      });
    });
    vue.onMounted(() => {
      registerLink(props.href);
    });
    vue.onBeforeUnmount(() => {
      unregisterLink(props.href);
    });
    return () => {
      var _a;
      const {
        href,
        target,
        title = slots.title,
        customTitleProps = {}
      } = props;
      const pre = prefixCls.value;
      mergedTitle = typeof title === "function" ? title(customTitleProps) : title;
      const active = activeLink.value === href;
      const wrapperClassName = _class.classNames(`${pre}-link`, {
        [`${pre}-link-active`]: active
      }, attrs.class);
      const titleClassName = _class.classNames(`${pre}-link-title`, {
        [`${pre}-link-title-active`]: active
      });
      return vue.createVNode("div", {
        ...attrs,
        "class": wrapperClassName
      }, [vue.createVNode("a", {
        "class": titleClassName,
        "href": href,
        "title": typeof mergedTitle === "string" ? mergedTitle : "",
        "target": target,
        "onClick": handleClick
      }, [slots.customTitle ? slots.customTitle(customTitleProps) : mergedTitle]), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
    };
  }
});

exports.default = AAnchorLink;
//# sourceMappingURL=AnchorLink.cjs.map
