var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
import { createVNode as _createVNode, Fragment as _Fragment } from "vue";
import { ProProvider } from "@antdv/pro-provider";
import { classNames, funcType, objectType, oneOfType, useDebounceFn, useEffect } from "@antdv/pro-utils";
import { computed, defineComponent, nextTick, ref, watch } from "vue";
import { AsyncContentPanel } from "./AsyncContentPanel.mjs";
import { ProHelpContext } from "./HelpProvide.mjs";
import { RenderContentPanel } from "./RenderContentPanel.mjs";
const proHelpContentPanelProps = {
  /**
   * 控制当前选中的帮助文档
   */
  selectedKey: oneOfType([String, Number]).isRequired,
  parentItem: objectType(),
  onScroll: funcType()
};
const ProHelpContentPanel = defineComponent({
  name: "ProHelpContentPanel",
  inheritAttrs: false,
  props: proHelpContentPanelProps,
  setup(props, {
    attrs
  }) {
    const {
      dataSource
    } = ProHelpContext.useContext();
    const {
      hashId
    } = ProProvider.useContext();
    const scrollHeightMap = ref(/* @__PURE__ */ new Map());
    const divRef = ref(null);
    watch(() => props.selectedKey, () => {
      var _a;
      if (!props.selectedKey || !((_a = props.parentItem) == null ? void 0 : _a.infiniteScrollFull)) return;
      const div = scrollHeightMap.value.get(props.selectedKey);
      if ((div == null ? void 0 : div.offsetTop) && divRef.value) {
        if (Math.abs(divRef.value.scrollTop - (div == null ? void 0 : div.offsetTop) + 40) > (div == null ? void 0 : div.clientHeight)) {
          divRef.value.scrollTop = (div == null ? void 0 : div.offsetTop) - 40;
        }
      }
    });
    const onScrollEvent = useDebounceFn((e) => __async(this, null, function* () {
      var _a;
      const dom = e == null ? void 0 : e.target;
      const list = Array.from(scrollHeightMap.value.entries()).find(([, value]) => {
        if ((dom == null ? void 0 : dom.scrollTop) < value.offsetTop) {
          return true;
        }
        return false;
      });
      if (!list) {
        return;
      }
      if (list.at(0) !== props.selectedKey) {
        (_a = props.onScroll) == null ? void 0 : _a.call(props, list.at(0));
      }
    }), 200);
    useEffect(() => {
      var _a;
      if (!((_a = props.parentItem) == null ? void 0 : _a.infiniteScrollFull)) return;
      nextTick(() => {
        var _a2;
        onScrollEvent.cancel();
        (_a2 = divRef.value) == null ? void 0 : _a2.addEventListener("scroll", onScrollEvent.run, false);
      });
      return () => {
        nextTick(() => {
          var _a2;
          onScrollEvent.cancel();
          (_a2 = divRef.value) == null ? void 0 : _a2.removeEventListener("scroll", onScrollEvent.run, false);
        });
      };
    }, computed(() => {
      var _a;
      return [(_a = props.parentItem) == null ? void 0 : _a.infiniteScrollFull, props.selectedKey];
    }));
    const dataSourceMap = computed(() => {
      const map = /* @__PURE__ */ new Map();
      dataSource == null ? void 0 : dataSource.forEach((page) => {
        page.children.forEach((item) => {
          map.set(item.key || item.title, __spreadProps(__spreadValues({}, item), {
            parentKey: page.key
          }));
        });
      });
      return map;
    });
    return () => {
      var _a;
      const {
        parentItem,
        selectedKey
      } = props;
      const renderItem = (item) => {
        if (item == null ? void 0 : item.asyncLoad) {
          return _createVNode("div", {
            "class": classNames(attrs.class || "", hashId),
            "id": item.title
          }, [_createVNode(AsyncContentPanel, {
            "key": item == null ? void 0 : item.key,
            "item": item,
            "onInit": (_ref) => {
              if (!scrollHeightMap.value) return;
              scrollHeightMap.value.set(item.key, _ref);
            }
          }, null)]);
        }
        return _createVNode("div", {
          "class": classNames(attrs.class || "", hashId),
          "id": item.title
        }, [_createVNode(RenderContentPanel, {
          "onInit": (_ref) => {
            if (!scrollHeightMap.value) return;
            scrollHeightMap.value.set(item.key, _ref);
          },
          "dataSourceChildren": (item == null ? void 0 : item.children) || []
        }, null)]);
      };
      if (parentItem && parentItem.infiniteScrollFull) {
        return _createVNode("div", {
          "ref": divRef,
          "class": classNames(`${attrs.class || ""}-infinite-scroll`, hashId),
          "style": {
            overflow: "auto"
          }
        }, [(_a = parentItem.children) == null ? void 0 : _a.map((item) => {
          return _createVNode(_Fragment, null, [renderItem(item)]);
        })]);
      }
      return renderItem(dataSourceMap.value.get(selectedKey));
    };
  }
});
export {
  ProHelpContentPanel,
  proHelpContentPanelProps
};
