/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var anchor_utils = require('./utils.js');
var anchor_anchorItemProps = require('./anchor-item-props.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../utils/mixins.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var classPrefixMixins = configProvider_configReceiver.getClassPrefixMixins("anchor");
var _AnchorItem = utils_mixins["default"](Vue__default["default"], classPrefixMixins).extend({
  name: "TAnchorItem",
  props: _objectSpread(_objectSpread({}, anchor_anchorItemProps["default"]), {}, {
    href: {
      type: String,
      required: true,
      validator: function validator(v) {
        return anchor_utils.ANCHOR_SHARP_REGEXP.test(v);
      }
    }
  }),
  inject: {
    tAnchor: {
      "default": void 0
    }
  },
  watch: {
    href: {
      immediate: true,
      handler: function handler() {
        this.unregister();
        this.register();
      }
    }
  },
  destroyed: function destroyed() {
    this.unregister();
  },
  methods: {
    register: function register() {
      this.tAnchor.registerLink(this.href);
    },
    unregister: function unregister() {
      var href = this.href;
      if (!href) return;
      this.tAnchor.unregisterLink(href);
    },
    handleClick: function handleClick(e) {
      var href = this.href,
          tAnchor = this.tAnchor,
          title = this.title;
      tAnchor.handleScrollTo(href);
      tAnchor.handleLinkClick({
        href: href,
        title: typeof title === "string" ? title : void 0,
        e: e
      });
    },
    renderTitle: function renderTitle() {
      var title = this.title,
          $scopedSlots = this.$scopedSlots;
      var titleSlot = $scopedSlots.title;
      var 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: function render() {
    var _wrapperClass;

    var h = arguments[0];
    var href = this.href,
        target = this.target,
        $scopedSlots = this.$scopedSlots,
        tAnchor = this.tAnchor;
    var children = $scopedSlots["default"],
        titleSlot = $scopedSlots.title;
    var title = this.renderTitle();
    var titleAttr = typeof title === "string" ? title : null;
    var isActive = tAnchor.active === href;
    var wrapperClass = (_wrapperClass = {}, _defineProperty__default["default"](_wrapperClass, "".concat(this.componentName, "__item"), true), _defineProperty__default["default"](_wrapperClass, this.commonStatusClassName.active, isActive), _wrapperClass);

    var titleClass = _defineProperty__default["default"]({}, "".concat(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=anchor-item.js.map
