"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.IndexBar = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _nativeProps = require("../../utils/native-props");
var _ahooks = require("ahooks");
var _withDefaultProps = require("../../utils/with-default-props");
var _sidebar = require("./sidebar");
var _convertPx = require("../../utils/convert-px");
var _panel = require("./panel");
var _devLog = require("../../utils/dev-log");
var _traverseReactNode = require("../../utils/traverse-react-node");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-index-bar`;
const defaultProps = {
  sticky: true
};
const IndexBar = exports.IndexBar = (0, _react.forwardRef)((p, ref) => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const titleHeight = (0, _convertPx.convertPx)(35);
  const bodyRef = (0, _react.useRef)(null);
  const indexItems = [];
  const panels = [];
  (0, _traverseReactNode.traverseReactNode)(props.children, child => {
    var _a;
    if (!_react.default.isValidElement(child)) return;
    if (child.type !== _panel.Panel) {
      (0, _devLog.devWarning)('IndexBar', 'The children of `IndexBar` must be `IndexBar.Panel` components.');
      return;
    }
    indexItems.push({
      index: child.props.index,
      brief: (_a = child.props.brief) !== null && _a !== void 0 ? _a : child.props.index.charAt(0)
    });
    panels.push((0, _nativeProps.withNativeProps)(child.props, _react.default.createElement("div", {
      key: child.props.index,
      "data-index": child.props.index,
      className: `${classPrefix}-anchor`
    }, _react.default.createElement("div", {
      className: `${classPrefix}-anchor-title`
    }, child.props.title || child.props.index), child.props.children)));
  });
  const [activeIndex, setActiveIndex] = (0, _react.useState)(() => {
    const firstItem = indexItems[0];
    return firstItem ? firstItem.index : null;
  });
  (0, _react.useImperativeHandle)(ref, () => ({
    scrollTo
  }));
  function scrollTo(index) {
    var _a;
    const body = bodyRef.current;
    if (!body) return;
    const children = body.children;
    for (let i = 0; i < children.length; i++) {
      const panel = children.item(i);
      if (!panel) continue;
      const panelIndex = panel.dataset['index'];
      if (panelIndex === index) {
        body.scrollTop = panel.offsetTop;
        setActiveIndex(index);
        activeIndex !== index && ((_a = props.onIndexChange) === null || _a === void 0 ? void 0 : _a.call(props, index));
        return;
      }
    }
  }
  const {
    run: checkActiveIndex
  } = (0, _ahooks.useThrottleFn)(() => {
    var _a;
    const body = bodyRef.current;
    if (!body) return;
    const scrollTop = body.scrollTop;
    const elements = body.getElementsByClassName(`${classPrefix}-anchor`);
    for (let i = 0; i < elements.length; i++) {
      const panel = elements.item(i);
      if (!panel) continue;
      const panelIndex = panel.dataset['index'];
      if (!panelIndex) continue;
      if (panel.offsetTop + panel.clientHeight - titleHeight > scrollTop) {
        setActiveIndex(panelIndex);
        activeIndex !== panelIndex && ((_a = props.onIndexChange) === null || _a === void 0 ? void 0 : _a.call(props, panelIndex));
        return;
      }
    }
  }, {
    wait: 50,
    trailing: true,
    leading: true
  });
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    className: (0, _classnames.default)(`${classPrefix}`, {
      [`${classPrefix}-sticky`]: props.sticky
    })
  }, _react.default.createElement(_sidebar.Sidebar, {
    indexItems: indexItems,
    activeIndex: activeIndex,
    onActive: index => {
      scrollTo(index);
    }
  }), _react.default.createElement("div", {
    className: `${classPrefix}-body`,
    ref: bodyRef,
    onScroll: checkActiveIndex
  }, panels)));
});