function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState, useContext, useCallback } from 'react';
import clsx from 'clsx';
import { getRect as getElementRect } from '../hooks/use-rect';
import useHeight from '../hooks/use-height';
import IndexBarContext from './IndexBarContext';
import { getScrollTop, getRootScrollTop } from '../utils';
import { BORDER_BOTTOM, COMPONENT_TYPE_KEY } from '../utils/constant';
import { useSetState } from '../hooks';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
export var INDEX_ANCHORE_KEY = Symbol('index-anchor');
var IndexAnchor = /*#__PURE__*/forwardRef(function (props, ref) {
  var _clsx;

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('index-anchor', prefixCls),
      bem = _createNamespace[0];

  var root = useRef();
  var height = useHeight(root);
  var context = useContext(IndexBarContext);

  if (!context) {
    if (process.env.NODE_ENV !== 'production') {
      // eslint-disable-next-line no-console
      console.error('[React Vant] <IndexAnchor> must be a child component of <IndexBar>.');
    }
  }

  var _useSetState = useSetState({
    top: 0,
    left: 0,
    rect: {
      top: 0,
      height: 0
    },
    width: 0,
    active: false
  }),
      state = _useSetState[0],
      updateState = _useSetState[1];

  var _useState = useState({
    top: 0,
    height: 0
  }),
      rect = _useState[0],
      setRect = _useState[1];

  var isSticky = useCallback(function () {
    return state.active && context.sticky;
  }, [state.active, context.sticky]);
  var anchorStyle = useMemo(function () {
    var zIndex = context.zIndex,
        highlightColor = context.highlightColor;

    if (isSticky()) {
      return {
        zIndex: "" + zIndex,
        left: state.left ? state.left + "px" : null,
        width: state.width ? state.width + "px" : null,
        transform: state.top ? "translate3d(0, " + state.top + "px, 0)" : null,
        color: highlightColor
      };
    }

    return null;
  }, [isSticky(), state.width, state.left, state.top]);

  var getRect = function getRect(scrollParent, scrollParentRect) {
    var rootRect = getElementRect(root.current);

    var newState = _extends({}, state);

    newState.rect.height = rootRect.height;

    if (scrollParent === window || scrollParent === document.body) {
      newState.rect.top = rootRect.top + getRootScrollTop();
    } else {
      newState.rect.top = rootRect.top + getScrollTop(scrollParent) - scrollParentRect.top;
    }

    updateState(newState);
    return newState.rect;
  };

  useEffect(function () {
    setRect({
      top: rect.top,
      height: height
    });
  }, [height]);
  useImperativeHandle(ref, function () {
    return {
      getRect: getRect,
      state: state,
      updateState: updateState,
      root: root
    };
  });
  var sticky = isSticky();
  return /*#__PURE__*/React.createElement("div", {
    className: props.className,
    ref: root,
    style: _extends({}, props.style, {
      height: sticky ? state.rect.height + "px" : undefined
    })
  }, /*#__PURE__*/React.createElement("div", {
    style: anchorStyle,
    className: clsx(bem({
      sticky: sticky
    }), (_clsx = {}, _clsx[BORDER_BOTTOM] = sticky, _clsx))
  }, props.children || props.index));
});
IndexAnchor[COMPONENT_TYPE_KEY] = INDEX_ANCHORE_KEY;
export default IndexAnchor;