"use strict";

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

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

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

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

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

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

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

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _AnchorContext = _interopRequireDefault(require("./AnchorContext"));

var AnchorLink = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(AnchorLink, _Component);

  var _super = (0, _createSuper2["default"])(AnchorLink);

  function AnchorLink() {
    var _this;

    (0, _classCallCheck2["default"])(this, AnchorLink);
    _this = _super.apply(this, arguments);

    _this.handleClick = function (e) {
      var c7nAnchor = _this.context.c7nAnchor;

      if (c7nAnchor) {
        var scrollTo = c7nAnchor.scrollTo,
            onClick = c7nAnchor.onClick;
        var _this$props = _this.props,
            href = _this$props.href,
            title = _this$props.title;

        if (onClick) {
          onClick(e, {
            title: title,
            href: href
          });
        }

        scrollTo(href);
      }
    };

    return _this;
  }

  (0, _createClass2["default"])(AnchorLink, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var c7nAnchor = this.context.c7nAnchor;

      if (c7nAnchor) {
        var href = this.props.href;
        c7nAnchor.registerLink(href);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(_ref) {
      var prevHref = _ref.href;
      var href = this.props.href;

      if (prevHref !== href) {
        var c7nAnchor = this.context.c7nAnchor;

        if (c7nAnchor) {
          c7nAnchor.unregisterLink(prevHref);
          c7nAnchor.registerLink(href);
        }
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var c7nAnchor = this.context.c7nAnchor;
      var href = this.props.href;

      if (c7nAnchor) {
        c7nAnchor.unregisterLink(href);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$context = this.context,
          c7nAnchor = _this$context.c7nAnchor,
          getPrefixCls = _this$context.getPrefixCls;
      var _this$props2 = this.props,
          customizePrefixCls = _this$props2.prefixCls,
          href = _this$props2.href,
          title = _this$props2.title,
          children = _this$props2.children,
          className = _this$props2.className;
      var prefixCls = getPrefixCls('anchor', customizePrefixCls);
      var active = c7nAnchor && c7nAnchor.activeLink === href;
      var wrapperClassName = (0, _classnames["default"])(className, "".concat(prefixCls, "-link"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-link-active"), active));
      var titleClassName = (0, _classnames["default"])("".concat(prefixCls, "-link-title"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-link-title-active"), active));
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: wrapperClassName
      }, /*#__PURE__*/_react["default"].createElement("a", {
        className: titleClassName,
        href: href,
        title: typeof title === 'string' ? title : '',
        onClick: this.handleClick
      }, title), children);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _AnchorContext["default"];
    }
  }]);
  return AnchorLink;
}(_react.Component);

exports["default"] = AnchorLink;
AnchorLink.displayName = 'AnchorLink';
AnchorLink.defaultProps = {
  href: '#'
};
//# sourceMappingURL=AnchorLink.js.map
