"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Heading = _interopRequireDefault(require("../Heading"));

var _IconButton = _interopRequireDefault(require("../IconButton"));

var _IconInsertLink = _interopRequireDefault(require("../IconInsertLink"));

var _ref2 = /*#__PURE__*/_react["default"].createElement(_IconInsertLink["default"], {
  size: 22
});

var Anchor = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var to = _ref.to;
  return /*#__PURE__*/_react["default"].createElement(_IconButton["default"], {
    as: "a",
    ref: ref,
    href: to,
    size: 22,
    sx: {
      m: 0,
      p: 0,
      ml: 2,
      verticalAlign: 'middle',
      cursor: 'pointer'
    }
  }, _ref2);
});
Anchor.displayName = 'Anchor';
Anchor.propTypes = {
  to: _propTypes["default"].string
};

var getLink = function getLink(href) {
  if (typeof window === 'undefined') {
    return '';
  }

  var _document$location = document.location,
      origin = _document$location.origin,
      search = _document$location.search,
      pathname = _document$location.pathname;
  return "".concat(origin + pathname + search, "#").concat(href);
};

var HeadingAnchor = /*#__PURE__*/(0, _react.forwardRef)(function (_ref3, ref) {
  var children = _ref3.children,
      id = _ref3.id,
      copyLink = _ref3.copyLink,
      rest = (0, _objectWithoutProperties2["default"])(_ref3, ["children", "id", "copyLink"]);

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      inHover = _useState2[0],
      setHover = _useState2[1];

  var to = copyLink || getLink(id);
  return /*#__PURE__*/_react["default"].createElement(_Heading["default"], (0, _extends2["default"])({
    onMouseEnter: function onMouseEnter() {
      return setHover(true);
    },
    onMouseLeave: function onMouseLeave() {
      return setHover(false);
    },
    id: id,
    ref: ref
  }, rest), children, (id || copyLink) && inHover && /*#__PURE__*/_react["default"].createElement(Anchor, {
    to: to
  }));
});
HeadingAnchor.displayName = 'HeadingAnchor';
process.env.NODE_ENV !== "production" ? HeadingAnchor.propTypes = {
  /** Children to be rendered in the main container. */
  children: _propTypes["default"].node,

  /** You can override the whole link. If not set the current location with
   * `id` as the hash is used. */
  copyLink: _propTypes["default"].string,
  id: _propTypes["default"].string
} : void 0;
var _default = HeadingAnchor;
exports["default"] = _default;