"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useTouch = require("../composables/use-touch.js");
const useExpose = require("../composables/use-expose.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const basic = require("../utils/basic.js");
const dom = require("../utils/dom.js");
function genAlphabet() {
  const charCodeOfA = "A".charCodeAt(0);
  const indexList = Array(26).fill("").map((_, i) => String.fromCharCode(charCodeOfA + i));
  return indexList;
}
const [name, bem] = create.createNamespace("index-bar");
const indexBarProps = {
  sticky: props.truthProp,
  zIndex: props.numericProp,
  teleport: [String, Object],
  highlightColor: String,
  stickyOffsetTop: props.makeNumberProp(0),
  indexList: {
    type: Array,
    default: genAlphabet
  }
};
const INDEX_BAR_KEY = Symbol(name);
var stdin_default = Vue.defineComponent({
  name,
  props: indexBarProps,
  emits: ["select", "change"],
  setup(props2, {
    emit,
    slots
  }) {
    const root = Vue.ref();
    const sidebar = Vue.ref();
    const activeAnchor = Vue.ref("");
    const touch = useTouch.useTouch();
    const scrollParent = index_esm.useScrollParent(root);
    const {
      children,
      linkChildren
    } = index_esm.useChildren(INDEX_BAR_KEY);
    let selectActiveIndex;
    linkChildren({
      props: props2
    });
    const sidebarStyle = Vue.computed(() => {
      if (basic.isDef(props2.zIndex)) {
        return {
          zIndex: +props2.zIndex + 1
        };
      }
    });
    const highlightStyle = Vue.computed(() => {
      if (props2.highlightColor) {
        return {
          color: props2.highlightColor
        };
      }
    });
    const getActiveAnchor = (scrollTop, rects) => {
      for (let i = children.length - 1; i >= 0; i--) {
        const prevHeight = i > 0 ? rects[i - 1].height : 0;
        const reachTop = props2.sticky ? prevHeight + props2.stickyOffsetTop : 0;
        if (scrollTop + reachTop >= rects[i].top) {
          return i;
        }
      }
      return -1;
    };
    const getMatchAnchor = (index) => children.find((item) => String(item.index) === index);
    const onScroll = () => {
      if (dom.isHidden(root)) {
        return;
      }
      const {
        sticky,
        indexList
      } = props2;
      const scrollTop = dom.getScrollTop(scrollParent.value);
      const scrollParentRect = index_esm.useRect(scrollParent);
      const rects = children.map((item) => item.getRect(scrollParent.value, scrollParentRect));
      let active = -1;
      if (selectActiveIndex) {
        const match = getMatchAnchor(selectActiveIndex);
        if (match) {
          const rect = match.getRect(scrollParent.value, scrollParentRect);
          if (props2.sticky && props2.stickyOffsetTop) {
            active = getActiveAnchor(rect.top - props2.stickyOffsetTop, rects);
          } else {
            active = getActiveAnchor(rect.top, rects);
          }
        }
      } else {
        active = getActiveAnchor(scrollTop, rects);
      }
      activeAnchor.value = indexList[active];
      if (sticky) {
        children.forEach((item, index) => {
          const {
            state,
            $el
          } = item;
          if (index === active || index === active - 1) {
            const rect = $el.getBoundingClientRect();
            state.left = rect.left;
            state.width = rect.width;
          } else {
            state.left = null;
            state.width = null;
          }
          if (index === active) {
            state.active = true;
            state.top = Math.max(props2.stickyOffsetTop, rects[index].top - scrollTop) + scrollParentRect.top;
          } else if (index === active - 1 && selectActiveIndex === "") {
            const activeItemTop = rects[active].top - scrollTop;
            state.active = activeItemTop > 0;
            state.top = activeItemTop + scrollParentRect.top - rects[index].height;
          } else {
            state.active = false;
          }
        });
      }
      selectActiveIndex = "";
    };
    const init = () => {
      Vue.nextTick(onScroll);
    };
    index_esm.useEventListener("scroll", onScroll, {
      target: scrollParent,
      passive: true
    });
    Vue.onMounted(init);
    Vue.watch(() => props2.indexList, init);
    Vue.watch(activeAnchor, (value) => {
      if (value) {
        emit("change", value);
      }
    });
    const renderIndexes = () => props2.indexList.map((index) => {
      const active = index === activeAnchor.value;
      return Vue.createVNode("span", {
        "class": bem("index", {
          active
        }),
        "style": active ? highlightStyle.value : void 0,
        "data-index": index
      }, [index]);
    });
    const scrollTo = (index) => {
      selectActiveIndex = String(index);
      const match = getMatchAnchor(selectActiveIndex);
      if (match) {
        const scrollTop = dom.getScrollTop(scrollParent.value);
        const scrollParentRect = index_esm.useRect(scrollParent);
        const {
          offsetHeight
        } = document.documentElement;
        match.$el.scrollIntoView();
        if (scrollTop === offsetHeight - scrollParentRect.height) {
          onScroll();
          return;
        }
        if (props2.sticky && props2.stickyOffsetTop) {
          if (dom.getRootScrollTop() === offsetHeight - scrollParentRect.height) {
            dom.setRootScrollTop(dom.getRootScrollTop());
          } else {
            dom.setRootScrollTop(dom.getRootScrollTop() - props2.stickyOffsetTop);
          }
        }
        emit("select", match.index);
      }
    };
    const scrollToElement = (element) => {
      const {
        index
      } = element.dataset;
      if (index) {
        scrollTo(index);
      }
    };
    const onClickSidebar = (event) => {
      scrollToElement(event.target);
    };
    let touchActiveIndex;
    const onTouchMove = (event) => {
      touch.move(event);
      if (touch.isVertical()) {
        dom.preventDefault(event);
        const {
          clientX,
          clientY
        } = event.touches[0];
        const target = document.elementFromPoint(clientX, clientY);
        if (target) {
          const {
            index
          } = target.dataset;
          if (index && touchActiveIndex !== index) {
            touchActiveIndex = index;
            scrollToElement(target);
          }
        }
      }
    };
    const renderSidebar = () => Vue.createVNode("div", {
      "ref": sidebar,
      "class": bem("sidebar"),
      "style": sidebarStyle.value,
      "onClick": onClickSidebar,
      "onTouchstartPassive": touch.start
    }, [renderIndexes()]);
    useExpose.useExpose({
      scrollTo
    });
    index_esm.useEventListener("touchmove", onTouchMove, {
      target: sidebar
    });
    return () => {
      var _a;
      return Vue.createVNode("div", {
        "ref": root,
        "class": bem()
      }, [props2.teleport ? Vue.createVNode(Vue.Teleport, {
        "to": props2.teleport
      }, {
        default: () => [renderSidebar()]
      }) : renderSidebar(), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
    };
  }
});
exports.INDEX_BAR_KEY = INDEX_BAR_KEY;
exports.default = stdin_default;
exports.indexBarProps = indexBarProps;
