import { createVNode as _createVNode } from "vue";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { RouterLink } from 'vue-router';
import { computed } from 'vue';
import { PropTypes, withInstall } from '../utils';
import { omit } from 'lodash-es';

/**
 * 外链正则表达式
 */
var externalReg = /(http|https|ftp):\/\/([\w.]+\/?)\S*/;

var AppLink = function AppLink(props, _ref) {
  var _slots$default3;

  var attrs = _ref.attrs,
      slots = _ref.slots,
      emit = _ref.emit;
  var external = props.external,
      target = props.target,
      to = props.to,
      customClickEvent = props.customClickEvent,
      registered = props.registered;
  var isExternal = computed(function () {
    return external || typeof to === 'string' && externalReg.test(to);
  });

  if (isExternal.value) {
    var _slots$default;

    return _createVNode("a", {
      "href": to,
      "target": target
    }, [(_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)]);
  }
  /* 路由未注册则直接返回 */
  // if (!registered) {
  //   return slots.default?.();
  // }

  /* 如果跳转链接不存在或存在自定义点击事件，则直接返回插槽内容 */


  if (!to || customClickEvent) {
    var _slots$default2;

    return (_slots$default2 = slots.default) === null || _slots$default2 === void 0 ? void 0 : _slots$default2.call(slots);
  }

  return _createVNode(RouterLink, _objectSpread(_objectSpread({}, omit(props, 'external', 'target', 'registered', 'customClickEvent')), {}, {
    "target": "_self",
    "to": to,
    "replace": true
  }), {
    default: function _default() {
      return [(_slots$default3 = slots.default) === null || _slots$default3 === void 0 ? void 0 : _slots$default3.call(slots)];
    }
  });
};

AppLink.displayName = 'AppLink';
AppLink.props = {
  external: PropTypes.bool.def(false),
  target: PropTypes.string.def('_blank'),
  registered: PropTypes.bool.def(false),
  to: {
    type: [String, Object]
  },
  customClickEvent: PropTypes.bool.def(false)
};
AppLink.inheritAttrs = false;
export default withInstall(AppLink);