"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Link = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _isExternalUrl = require("@audentio/utils/lib/isExternalUrl");

var _react = _interopRequireWildcard(require("react"));

var _PseudoBox = require("../PseudoBox");

var _router = require("../utils/router");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;
var baseStyleProps = {
  transition: "all 0.15s ease-out",
  cursor: 'pointer',
  textDecoration: 'none',
  outline: 'none',
  color: 'bodyText',
  _hover: {
    color: 'titleText'
  },
  _focus: {
    boxShadow: 'outline'
  },
  _disabled: {
    opacity: '0.4',
    cursor: 'not-allowed',
    textDecoration: 'none'
  }
};
var Link = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var isDisabled = _ref.isDisabled,
      onClick = _ref.onClick,
      href = _ref.href,
      rest = (0, _objectWithoutProperties2["default"])(_ref, ["isDisabled", "onClick", "href"]);

  var _useRouter = (0, _router.useRouter)(),
      RouterLink = _useRouter.Link,
      id = _useRouter.id;

  function getHref() {
    var _window;

    if (href && typeof window !== 'undefined' && href.indexOf((_window = window) === null || _window === void 0 ? void 0 : _window.location.origin) === 0) {
      var _window2;

      return href.replace((_window2 = window) === null || _window2 === void 0 ? void 0 : _window2.location.origin, '');
    }

    return href;
  }

  var linkHref = getHref();
  var isExternal = (0, _isExternalUrl.isExternalUrl)(linkHref);
  var linkProps = {};

  if (isExternal || !linkHref) {
    // use normal anchor for external links
    linkProps = {
      as: 'a',
      href: linkHref,
      target: '_blank',
      rel: 'noopener noreferrer'
    };
  } else if (id === 'next') {
    // use next/link inside next apps
    return (// next/link: child must be an anchor which wraps the link content
      __jsx(RouterLink, {
        href: linkHref
      }, __jsx(_PseudoBox.PseudoBox, (0, _extends2["default"])({
        ref: ref,
        tabIndex: isDisabled ? -1 : undefined,
        "aria-disabled": isDisabled,
        onClick: isDisabled ? function (event) {
          return event.preventDefault();
        } : onClick,
        as: "a"
      }, baseStyleProps, rest)))
    );
  } else {
    // use react-router as fallback
    linkProps = {
      as: RouterLink || 'a',
      to: linkHref
    };
  }

  return (// @ts-ignore
    __jsx(_PseudoBox.PseudoBox, (0, _extends2["default"])({
      ref: ref,
      tabIndex: isDisabled ? -1 : undefined,
      "aria-disabled": isDisabled,
      onClick: isDisabled ? function (event) {
        return event.preventDefault();
      } : onClick
    }, baseStyleProps, linkProps, rest))
  );
});
exports.Link = Link;
//# sourceMappingURL=Link.js.map