"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
require("../../../../../../node_modules/element-plus/es/index.js");
;/* empty css                                                                */
;/* empty css                                                                     */
;/* empty css                                                                          */
const Vue = require("vue");
;/* empty css                     */
const _pluginVue_exportHelper = require("../../../../../../_virtual/_plugin-vue_export-helper.js");
const index = require("../../../../../../node_modules/element-plus/es/components/anchor/index.js");
const _hoisted_1 = { class: "anchor-nav" };
const _hoisted_2 = { key: 0 };
const _sfc_main = {
  __name: "RightAnchorNav",
  props: {
    links: {
      type: Array,
      default: () => []
    },
    modelValue: {
      type: [String, Number],
      default: ""
    },
    fecFormRef: {
      type: Object
    }
  },
  emits: ["change", "update:modelValue"],
  setup(__props, { emit: __emit }) {
    var _a;
    const props = __props;
    const anchorRef = Vue.ref();
    const emit = __emit;
    Vue.watch(
      () => props.links,
      (newLinks) => {
        if (newLinks.length && !props.modelValue) {
          emit("update:modelValue", newLinks[0].id);
        }
      },
      { immediate: true }
    );
    let isClickScrolling = false;
    function handleClick(e, link) {
      var _a2, _b;
      e.preventDefault();
      emit("update:modelValue", link.id);
      emit("change", link);
      const fecForm = (_b = (_a2 = anchorRef.value) == null ? void 0 : _a2.$el) == null ? void 0 : _b.closest(".fec-form");
      const element = fecForm == null ? void 0 : fecForm.querySelector(`#comp_${link.id}`);
      if (element) {
        isClickScrolling = true;
        element.scrollIntoView({ behavior: "smooth", block: "start" });
        waitScrollEnd(() => {
          isClickScrolling = false;
        });
      }
    }
    const scrollWrap = (_a = props.fecFormRef) == null ? void 0 : _a.$el.querySelector(".el-scrollbar__wrap");
    scrollWrap == null ? void 0 : scrollWrap.addEventListener("scroll", onScroll);
    function waitScrollEnd(callback) {
      let timer = null;
      if (!scrollWrap) return;
      const handler = () => {
        clearTimeout(timer);
        timer = setTimeout(() => {
          scrollWrap.removeEventListener("scroll", handler);
          callback();
        }, 150);
      };
      scrollWrap.addEventListener("scroll", handler);
    }
    function onScroll() {
      var _a2;
      if (isClickScrolling) return;
      const top = scrollWrap.scrollTop;
      let current = null;
      for (const item of props.links) {
        const el = document.getElementById(String(item.id));
        if (!el) continue;
        if (el.offsetTop - 70 <= top) current = item;
      }
      if (current) {
        emit("update:modelValue", current.id);
        (_a2 = anchorRef.value) == null ? void 0 : _a2.scrollTo(`#${current.id}`);
      }
    }
    Vue.onMounted(() => {
      var _a2;
      (_a2 = anchorRef.value) == null ? void 0 : _a2.scrollTo(`#${props.modelValue}`);
    });
    return (_ctx, _cache) => {
      const _component_el_anchor_link = index.ElAnchorLink;
      const _component_el_anchor = index.ElAnchor;
      return Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
        _cache[0] || (_cache[0] = Vue.createElementVNode("div", { class: "anchor-title" }, [
          Vue.createElementVNode("div", { class: "label-line" }),
          Vue.createTextVNode(" 导航 ")
        ], -1)),
        Vue.createVNode(_component_el_anchor, {
          type: "underline",
          offset: 70,
          ref_key: "anchorRef",
          ref: anchorRef
        }, {
          default: Vue.withCtx(() => [
            (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(__props.links, (link) => {
              return Vue.openBlock(), Vue.createBlock(_component_el_anchor_link, {
                key: link.id,
                href: `#${link.id}`,
                onClick: Vue.withModifiers(($event) => handleClick($event, link), ["prevent"])
              }, {
                default: Vue.withCtx(() => [
                  Vue.createTextVNode(Vue.toDisplayString(link.title) + " ", 1),
                  link.count ? (Vue.openBlock(), Vue.createElementBlock("span", _hoisted_2, "(" + Vue.toDisplayString(link.count) + ")", 1)) : Vue.createCommentVNode("", true)
                ]),
                _: 2
              }, 1032, ["href", "onClick"]);
            }), 128))
          ]),
          _: 1
        }, 512)
      ]);
    };
  }
};
const RightAnchorNav = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-298a5455"]]);
exports.default = RightAnchorNav;
