var __extends = this && this.__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");

    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import { ConfigContext } from "./Anchor";
import AnchorContext from "./context";
import "./style/anchor.css";

var AnchorLink =
/** @class */
function (_super) {
  __extends(AnchorLink, _super);

  function AnchorLink() {
    var _this = _super !== null && _super.apply(this, arguments) || this;

    _this.handleClick = function (e) {
      var _a = _this.context,
          scrollTo = _a.scrollTo,
          onClick = _a.onClick;
      var _b = _this.props,
          href = _b.href,
          title = _b.title;
      onClick === null || onClick === void 0 ? void 0 : onClick(e, {
        title: title,
        href: href
      });
      scrollTo(href);
    };

    _this.renderAnchorLink = function (_a) {
      var _b, _c;

      var getPrefixCls = _a.getPrefixCls;
      var _d = _this.props,
          customizePrefixCls = _d.prefixCls,
          href = _d.href,
          title = _d.title,
          children = _d.children,
          className = _d.className,
          target = _d.target;
      var prefixCls = getPrefixCls('anchor', customizePrefixCls);
      var active = _this.context.activeLink === href;
      var wrapperClassName = classNames(prefixCls + "-link", (_b = {}, _b[prefixCls + "-link-active"] = active, _b), className);
      var titleClassName = classNames(prefixCls + "-link-title", (_c = {}, _c[prefixCls + "-link-title-active"] = active, _c));
      return _jsxs("div", __assign({
        className: wrapperClassName
      }, {
        children: [_jsx("a", __assign({
          className: titleClassName,
          href: href,
          title: typeof title === 'string' ? title : '',
          target: target,
          onClick: _this.handleClick
        }, {
          children: title
        }), void 0), children]
      }), void 0);
    };

    return _this;
  }

  AnchorLink.prototype.componentDidMount = function () {
    this.context.registerLink(this.props.href);
  };

  AnchorLink.prototype.componentDidUpdate = function (_a) {
    var prevHref = _a.href;
    var href = this.props.href;

    if (prevHref !== href) {
      this.context.unregisterLink(prevHref);
      this.context.registerLink(href);
    }
  };

  AnchorLink.prototype.componentWillUnmount = function () {
    this.context.unregisterLink(this.props.href);
  };

  AnchorLink.prototype.render = function () {
    return _jsx(ConfigContext.Consumer, {
      children: this.renderAnchorLink
    }, void 0);
  };

  AnchorLink.defaultProps = {
    href: '#'
  };
  AnchorLink.contextType = AnchorContext;
  return AnchorLink;
}(React.Component);

var AnchorLinkMemo = /*#__PURE__*/React.memo(AnchorLink);
AnchorLinkMemo.displayName = 'AffixMemo';
export { AnchorLink, AnchorLinkMemo };

