import { defineComponent, watch, nextTick, onMounted, onBeforeUnmount, createVNode } from 'vue';
import '../../../utils/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import { useInjectAnchor } from './context.mjs';
import { anchorLinkProps } from './props.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

var AAnchorLink = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAnchorLink",
  inheritAttrs: false,
  props: anchorLinkProps(),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    let mergedTitle = null;
    const {
      handleClick: contextHandleClick,
      scrollTo,
      unregisterLink,
      registerLink,
      activeLink
    } = useInjectAnchor();
    const {
      prefixCls
    } = useConfigInject("anchor", props);
    const handleClick = (e) => {
      const {
        href
      } = props;
      contextHandleClick(e, {
        title: mergedTitle,
        href
      });
      scrollTo(href);
    };
    watch(() => props.href, (val, oldVal) => {
      nextTick(() => {
        unregisterLink(oldVal);
        registerLink(val);
      });
    });
    onMounted(() => {
      registerLink(props.href);
    });
    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 = classNames(`${pre}-link`, {
        [`${pre}-link-active`]: active
      }, attrs.class);
      const titleClassName = classNames(`${pre}-link-title`, {
        [`${pre}-link-title-active`]: active
      });
      return createVNode("div", {
        ...attrs,
        "class": wrapperClassName
      }, [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)]);
    };
  }
});

export { AAnchorLink as default };
//# sourceMappingURL=AnchorLink.mjs.map
