/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var isString = require('lodash/isString');
var hooks_useConfig = require('../hooks/useConfig.js');
var anchor_utils = require('./utils.js');
var anchor_anchorItemProps = require('./anchor-item-props.js');
var anchor_constants = require('./constants.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);

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 localProps = _objectSpread(_objectSpread({}, anchor_anchorItemProps["default"]), {}, {
  href: {
    type: String,
    required: true,
    validator: function validator(v) {
      return anchor_utils.ANCHOR_SHARP_REGEXP.test(v);
    }
  }
});
var _AnchorItem = vue.defineComponent({
  name: "TAnchorItem",
  inject: {
    tAnchor: {
      "default": void 0
    }
  },
  props: localProps,
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var anchor = vue.inject(anchor_constants.AnchorInjectionKey, void 0);
    var CLASSNAME_PREFIX = hooks_useConfig.usePrefixClass("anchor__item");
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS;
    var register = function register() {
      anchor.registerLink(props2.href);
    };
    var unregister = function unregister() {
      var href = props2.href;
      if (!href) return;
      anchor.unregisterLink(href);
    };
    var handleClick = function handleClick(e) {
      var href = props2.href,
        title = props2.title;
      anchor.handleScrollTo(href);
      anchor.handleLinkClick({
        href: href,
        title: isString__default["default"](title) ? title : void 0,
        e: e
      });
    };
    var renderTitle = function renderTitle() {
      var title = props2.title;
      var titleSlot = slots.title;
      var titleVal;
      if (isString__default["default"](title)) titleVal = title;else if (isFunction__default["default"](title)) titleVal = title(vue.h);else if (titleSlot) titleVal = titleSlot(null);
      return titleVal;
    };
    vue.watch(function () {
      return props2.href;
    }, function () {
      unregister();
      register();
    }, {
      immediate: true
    });
    vue.onMounted(function () {
      register();
    });
    vue.onUnmounted(function () {
      unregister();
    });
    return function () {
      var _wrapperClass;
      var href = props2.href,
        target = props2.target;
      var children = slots["default"],
        titleSlot = slots.title;
      var title = renderTitle();
      var titleAttr = isString__default["default"](title) ? title : null;
      var active = anchor.active === href;
      var wrapperClass = (_wrapperClass = {}, _defineProperty__default["default"](_wrapperClass, CLASSNAME_PREFIX.value, true), _defineProperty__default["default"](_wrapperClass, STATUS.value.active, active), _wrapperClass);
      var titleClass = _defineProperty__default["default"]({}, "".concat(CLASSNAME_PREFIX.value, "-link"), true);
      return vue.createVNode("div", {
        "class": wrapperClass
      }, [vue.createVNode("a", {
        "href": href,
        "title": titleAttr,
        "class": titleClass,
        "target": target,
        "onClick": handleClick
      }, [titleSlot ? titleSlot(null) : title]), children && children(null)]);
    };
  }
});

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