"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Clickable = 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 = _interopRequireDefault(require("react"));

var _Box = require("../Box");

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

var __jsx = _react["default"].createElement;

var Clickable = function Clickable(_ref) {
  var onClick = _ref.onClick,
      href = _ref.href,
      as = _ref.as,
      staticContext = _ref.staticContext,
      innerRef = _ref.innerRef,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["onClick", "href", "as", "staticContext", "innerRef"]);

  var _useRouter = (0, _router.useRouter)(),
      router = _useRouter.router;

  var handleClick = function handleClick(e) {
    if (onClick) {
      onClick(e);
    }

    if (!href) return;
    var target = e.target;

    if ( // not anchor
    !target.href && !target.parentNode.href && // not button
    target.nodeName !== 'BUTTON' && target.parentNode.nodeName !== 'BUTTON') {
      var isExternal = (0, _isExternalUrl.isExternalUrl)(href);

      if (e.metaKey && !isExternal) {
        // cmd+click internal link – open in new tab
        window.open(window.location.origin + href, '_blank');
      } else if (isExternal) {
        // external link always open in new tab
        window.open(href, '_blank');
      } else {
        // internal link
        // push to history
        router.push(href);
      }
    } // don't do anything if a nested anchor or button was clicked

  };

  return __jsx(_Box.Box, (0, _extends2["default"])({
    as: as,
    onClick: handleClick,
    ref: innerRef,
    cursor: (href || onClick) && 'pointer'
  }, props));
};

exports.Clickable = Clickable;
//# sourceMappingURL=Clickable.js.map