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

'use strict';

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

var Vue = require('vue');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
var components_anchor_utils = require('./utils.js');
var components_anchor_props = require('./props.js');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

const classPrefixMixins = components_configProvider_configReceiver.getClassPrefixMixins("anchor");
var _AnchorItem = utils_mixins["default"](Vue__default["default"], classPrefixMixins).extend({
  name: "LAnchorItem",
  props: {
    ...components_anchor_props.AnchorItemProps,
    href: {
      type: String,
      required: true,
      validator(v) {
        return components_anchor_utils.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)]);
  }
});

exports["default"] = _AnchorItem;
//# sourceMappingURL=item.js.map
