"use strict";

exports.__esModule = true;
exports.default = exports.INDEX_ANCHORE_KEY = void 0;

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

var _clsx2 = _interopRequireDefault(require("clsx"));

var _useRect = require("../hooks/use-rect");

var _useHeight = _interopRequireDefault(require("../hooks/use-height"));

var _IndexBarContext = _interopRequireDefault(require("./IndexBarContext"));

var _utils = require("../utils");

var _constant = require("../utils/constant");

var _hooks = require("../hooks");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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); }

var INDEX_ANCHORE_KEY = Symbol('index-anchor');
exports.INDEX_ANCHORE_KEY = INDEX_ANCHORE_KEY;
var IndexAnchor = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _clsx;

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var root = (0, _react.useRef)();
  var height = (0, _useHeight.default)(root);
  var context = (0, _react.useContext)(_IndexBarContext.default);

  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 = (0, _hooks.useSetState)({
    top: 0,
    left: 0,
    rect: {
      top: 0,
      height: 0
    },
    width: 0,
    active: false
  }),
      state = _useSetState[0],
      updateState = _useSetState[1];

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

  var isSticky = (0, _react.useCallback)(function () {
    return state.active && context.sticky;
  }, [state.active, context.sticky]);
  var anchorStyle = (0, _react.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 = (0, _useRect.getRect)(root.current);

    var newState = _extends({}, state);

    newState.rect.height = rootRect.height;

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

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

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