/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import Vue from 'vue';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import { ANCHOR_SHARP_REGEXP } from './utils.js';
import { AnchorItemProps } from './props.js';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-def4c2ce.js';

const classPrefixMixins = getClassPrefixMixins("anchor");
var _AnchorItem = mixins(Vue, classPrefixMixins).extend({
  name: "LAnchorItem",
  props: {
    ...AnchorItemProps,
    href: {
      type: String,
      required: true,
      validator(v) {
        return ANCHOR_SHARP_REGEXP.test(v);
      }
    }
  },
  inject: {
    tAnchor: {
      default: void 0
    }
  },
  watch: {
    href: {
      immediate: true,
      handler() {
        this.unregister();
        this.register();
      }
    }
  },
  destroyed() {
    this.unregister();
  },
  methods: {
    register() {
      this.tAnchor.registerLink(this.href);
    },
    unregister() {
      const {
        href
      } = this;
      if (!href) return;
      this.tAnchor.unregisterLink(href);
    },
    handleClick(e) {
      const {
        href,
        tAnchor,
        title
      } = this;
      tAnchor.handleScrollTo(href);
      tAnchor.handleLinkClick({
        href,
        title: typeof title === "string" ? title : void 0,
        e
      });
    },
    renderTitle() {
      const {
        title,
        $scopedSlots
      } = this;
      const {
        title: titleSlot
      } = $scopedSlots;
      let titleVal;
      if (typeof title === "string") titleVal = title;else if (typeof title === "function") titleVal = title(this.$createElement);else if (titleSlot) titleVal = titleSlot(null);
      return titleVal;
    }
  },
  render() {
    const h = arguments[0];
    const {
      href,
      target,
      $scopedSlots,
      tAnchor
    } = this;
    const {
      default: children,
      title: titleSlot
    } = $scopedSlots;
    const title = this.renderTitle();
    const titleAttr = typeof title === "string" ? title : null;
    const isActive = tAnchor.active === href;
    const wrapperClass = {
      [`${this.componentName}__item`]: true,
      [this.commonStatusClassName.active]: isActive
    };
    const titleClass = {
      [`${this.componentName}__item-link`]: true
    };
    return h("div", {
      "class": wrapperClass
    }, [h("a", {
      "attrs": {
        "href": href,
        "title": titleAttr,
        "target": target
      },
      "class": titleClass,
      "on": {
        "click": this.handleClick
      }
    }, [titleSlot ? titleSlot(null) : title]), children && children(null)]);
  }
});

export { _AnchorItem as default };
//# sourceMappingURL=item.js.map
