var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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());
  });
};
var stdin_exports = {};
__export(stdin_exports, {
  ProHelpContentPanel: () => ProHelpContentPanel,
  proHelpContentPanelProps: () => proHelpContentPanelProps
});
module.exports = __toCommonJS(stdin_exports);
var import_vue = require("vue");
var import_pro_provider = require("@antdv/pro-provider");
var import_pro_utils = require("@antdv/pro-utils");
var import_vue2 = require("vue");
var import_AsyncContentPanel = require("./AsyncContentPanel");
var import_HelpProvide = require("./HelpProvide");
var import_RenderContentPanel = require("./RenderContentPanel");
const proHelpContentPanelProps = {
  /**
   * 控制当前选中的帮助文档
   */
  selectedKey: (0, import_pro_utils.oneOfType)([String, Number]).isRequired,
  parentItem: (0, import_pro_utils.objectType)(),
  onScroll: (0, import_pro_utils.funcType)()
};
const ProHelpContentPanel = (0, import_vue2.defineComponent)({
  name: "ProHelpContentPanel",
  inheritAttrs: false,
  props: proHelpContentPanelProps,
  setup(props, {
    attrs
  }) {
    const {
      dataSource
    } = import_HelpProvide.ProHelpContext.useContext();
    const {
      hashId
    } = import_pro_provider.ProProvider.useContext();
    const scrollHeightMap = (0, import_vue2.ref)(/* @__PURE__ */ new Map());
    const divRef = (0, import_vue2.ref)(null);
    (0, import_vue2.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 = (0, import_pro_utils.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);
    (0, import_pro_utils.useEffect)(() => {
      var _a;
      if (!((_a = props.parentItem) == null ? void 0 : _a.infiniteScrollFull)) return;
      (0, import_vue2.nextTick)(() => {
        var _a2;
        onScrollEvent.cancel();
        (_a2 = divRef.value) == null ? void 0 : _a2.addEventListener("scroll", onScrollEvent.run, false);
      });
      return () => {
        (0, import_vue2.nextTick)(() => {
          var _a2;
          onScrollEvent.cancel();
          (_a2 = divRef.value) == null ? void 0 : _a2.removeEventListener("scroll", onScrollEvent.run, false);
        });
      };
    }, (0, import_vue2.computed)(() => {
      var _a;
      return [(_a = props.parentItem) == null ? void 0 : _a.infiniteScrollFull, props.selectedKey];
    }));
    const dataSourceMap = (0, import_vue2.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 (0, import_vue.createVNode)("div", {
            "class": (0, import_pro_utils.classNames)(attrs.class || "", hashId),
            "id": item.title
          }, [(0, import_vue.createVNode)(import_AsyncContentPanel.AsyncContentPanel, {
            "key": item == null ? void 0 : item.key,
            "item": item,
            "onInit": (_ref) => {
              if (!scrollHeightMap.value) return;
              scrollHeightMap.value.set(item.key, _ref);
            }
          }, null)]);
        }
        return (0, import_vue.createVNode)("div", {
          "class": (0, import_pro_utils.classNames)(attrs.class || "", hashId),
          "id": item.title
        }, [(0, import_vue.createVNode)(import_RenderContentPanel.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 (0, import_vue.createVNode)("div", {
          "ref": divRef,
          "class": (0, import_pro_utils.classNames)(`${attrs.class || ""}-infinite-scroll`, hashId),
          "style": {
            overflow: "auto"
          }
        }, [(_a = parentItem.children) == null ? void 0 : _a.map((item) => {
          return (0, import_vue.createVNode)(import_vue.Fragment, null, [renderItem(item)]);
        })]);
      }
      return renderItem(dataSourceMap.value.get(selectedKey));
    };
  }
});
