"use strict";

var __extends = void 0 && (void 0).__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 = void 0 && (void 0).__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);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var Anchor_1 = require("./Anchor");

var context_1 = __importDefault(require("./context"));

require("./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_1["default"](prefixCls + "-link", (_b = {}, _b[prefixCls + "-link-active"] = active, _b), className);
      var titleClassName = classnames_1["default"](prefixCls + "-link-title", (_c = {}, _c[prefixCls + "-link-title-active"] = active, _c));
      return jsx_runtime_1.jsxs("div", __assign({
        className: wrapperClassName
      }, {
        children: [jsx_runtime_1.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_runtime_1.jsx(Anchor_1.ConfigContext.Consumer, {
      children: this.renderAnchorLink
    }, void 0);
  };

  AnchorLink.defaultProps = {
    href: '#'
  };
  AnchorLink.contextType = context_1["default"];
  return AnchorLink;
}(React.Component);

exports.AnchorLink = AnchorLink;
var AnchorLinkMemo = React.memo(AnchorLink);
exports.AnchorLinkMemo = AnchorLinkMemo;
AnchorLinkMemo.displayName = 'AffixMemo';

