"use strict";

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

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

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

const INDEX_ANCHORE_KEY = Symbol('index-anchor');
exports.INDEX_ANCHORE_KEY = INDEX_ANCHORE_KEY;
const IndexAnchor = (0, _react().forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('index-anchor', prefixCls);
  const root = (0, _react().useRef)();
  const height = (0, _useHeight.default)(root);
  const 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>.');
    }
  }

  const [state, updateState] = (0, _hooks.useSetState)({
    top: 0,
    left: 0,
    rect: {
      top: 0,
      height: 0
    },
    width: 0,
    active: false
  });
  const [rect, setRect] = (0, _react().useState)({
    top: 0,
    height: 0
  });
  const isSticky = (0, _react().useCallback)(() => state.active && context.sticky, [state.active, context.sticky]);
  const anchorStyle = (0, _react().useMemo)(() => {
    const {
      zIndex,
      highlightColor
    } = context;

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

  const getRect = (scrollParent, scrollParentRect) => {
    const rootRect = (0, _useRect.getRect)(root.current);
    const newState = Object.assign({}, 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)(() => {
    setRect({
      top: rect.top,
      height
    });
  }, [height]);
  (0, _react().useImperativeHandle)(ref, () => ({
    getRect,
    state,
    updateState,
    root
  }));
  const sticky = isSticky();
  return (0, _jsxRuntime().jsx)("div", Object.assign({
    className: props.className,
    ref: root,
    style: Object.assign(Object.assign({}, props.style), {
      height: sticky ? `${state.rect.height}px` : undefined
    })
  }, {
    children: (0, _jsxRuntime().jsx)("div", Object.assign({
      style: anchorStyle,
      className: (0, _clsx().default)(bem({
        sticky
      }), {
        [_constant.BORDER_BOTTOM]: sticky
      })
    }, {
      children: props.children || props.index
    }))
  }));
});
IndexAnchor[_constant.COMPONENT_TYPE_KEY] = INDEX_ANCHORE_KEY;
var _default = IndexAnchor;
exports.default = _default;