"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
;/* empty css                    */
const _pluginVue_exportHelper = require("../../../../../../_virtual/_plugin-vue_export-helper.js");
const _hoisted_1 = { class: "fec-form-links" };
const _hoisted_2 = ["href", "onClick"];
const _hoisted_3 = { class: "fec-form-link-content" };
const _hoisted_4 = {
  key: 0,
  class: "tab-count"
};
const _sfc_main = {
  __name: "FormAnchorNav",
  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 formAnchorRef = 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;
      e.preventDefault();
      emit("update:modelValue", link.id);
      emit("change", link);
      const fecForm = (_a2 = formAnchorRef.value) == null ? void 0 : _a2.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() {
      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 <= top) current = item;
      }
      if (current) {
        emit("update:modelValue", current.id);
      }
    }
    return (_ctx, _cache) => {
      return __props.links.length ? (Vue.openBlock(), Vue.createElementBlock("div", {
        key: 0,
        class: "fec-form-nav",
        ref_key: "formAnchorRef",
        ref: formAnchorRef
      }, [
        Vue.createElementVNode("div", _hoisted_1, [
          (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(__props.links, (link) => {
            return Vue.openBlock(), Vue.createElementBlock("a", {
              key: link.id,
              href: `#${link.id}`,
              class: Vue.normalizeClass(["fec-form-link-item", { "is-active": __props.modelValue == link.id }]),
              onClick: Vue.withModifiers(($event) => handleClick($event, link), ["prevent"])
            }, [
              Vue.createElementVNode("span", _hoisted_3, [
                Vue.createTextVNode(Vue.toDisplayString(link.title) + " ", 1),
                link.count ? (Vue.openBlock(), Vue.createElementBlock("span", _hoisted_4, "(" + Vue.toDisplayString(link.count) + ")", 1)) : Vue.createCommentVNode("", true)
              ])
            ], 10, _hoisted_2);
          }), 128))
        ])
      ], 512)) : Vue.createCommentVNode("", true);
    };
  }
};
const FormAnchorNav = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-1a0c9e5c"]]);
exports.default = FormAnchorNav;
