var __defProp = Object.defineProperty;
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;
};
import { createVNode as _createVNode, Fragment as _Fragment } from "vue";
import { CloseOutlined, ProfileOutlined } from "@ant-design/icons-vue";
import { createContext, isNeedOpenHash, ProProvider } from "@antdv/pro-provider";
import { boolType, funcType, isNumeric, oneOfType, stringType, useState, vNodeType } from "@antdv/pro-utils";
import { Card, ConfigProvider, Menu } from "ant-design-vue";
import { useConfigContextInject } from "ant-design-vue/lib/config-provider/context";
import { computed, defineComponent, reactive, ref, watch } from "vue";
import { ProHelpContext } from "./HelpProvide.mjs";
import { ProHelpContentPanel } from "./ProHelpContentPanel.mjs";
import { ProHelpSelect } from "./Search.mjs";
import { useStyle } from "./style.mjs";
const SelectKeyContext = createContext(reactive({
  selectedKey: void 0
}));
const propHelpPanelProps = {
  /**
   * 帮助面板的标题
   */
  title: stringType(),
  /**
   * 帮助面板首次打开时的默认选中文档的键名
   */
  defaultSelectedKey: stringType(),
  /**
   * 当前选中的帮助文档的键名。如果提供了这个 prop，那么该组件将是一个受控组件，其状态将由父组件管理。如果未提供，那么该组件将是一个非受控组件，其状态将在组件内部管理。
   */
  selectedKey: stringType(),
  /**
   * 当选中的文档键名发生变化时调用的回调函数。新的键名将作为参数传递给该函数。
   */
  onSelectedKeyChange: funcType(),
  /**
   *控制左侧面板是否能够打开
   */
  showLeftPanel: boolType().def(void 0),
  /**
   * 当左侧面板打开状态发生变化时调用的回调函数。新的打开状态将作为参数传递给该函数。
   */
  onShowLeftPanelChange: funcType(),
  /**
   * 是否显示边框
   */
  bordered: boolType(),
  /**
   * 当帮助面板关闭时调用的回调函数。
   */
  onClose: funcType(),
  /**
   * 帮助面板的高度，可以是数字或字符串类型。
   */
  height: oneOfType([Number, String]),
  /**
   * 帮助面板的页脚
   */
  footer: vNodeType(),
  /**
   * 在一页内加载所有的 children 内容
   */
  infiniteScrollFull: boolType(),
  /**
   * 自定义渲染 extra 部分的内容
   *
   * @param {VueNode} collapsePannelAction - 折叠收起的左侧按钮
   * @param {VueNode} helpSelectAction - 默认的帮助筛选按钮
   * @param {VueNode} closeAction - 关闭操作按钮
   * @returns {VueNode} - 返回自定义渲染的 extra 操作按钮
   *
   */
  extraRender: funcType()
};
const ProHelpPanel = defineComponent({
  name: "ProHelpPanel",
  inheritAttrs: false,
  props: propHelpPanelProps,
  slots: Object,
  emits: {
    "update:selectedKey": (val) => true,
    "update:showLeftPanel": (val) => true
  },
  setup(props, {
    emit,
    slots
  }) {
    const {
      getPrefixCls
    } = useConfigContextInject();
    const className = computed(() => getPrefixCls("pro-help"));
    const {
      wrapSSR,
      hashId
    } = useStyle(className);
    const {
      dataSource
    } = ProHelpContext.useContext();
    const {
      token
    } = ProProvider.useContext();
    const selectedKey = ref(props.selectedKey !== void 0 ? props.selectedKey : props.defaultSelectedKey);
    const showLeftPanel = ref(props.showLeftPanel !== void 0 ? props.showLeftPanel : true);
    watch(() => props.selectedKey, () => {
      selectedKey.value = props.selectedKey;
    });
    watch(() => selectedKey.value, (val) => {
      var _a;
      (_a = props.onSelectedKeyChange) == null ? void 0 : _a.call(props, val);
      emit("update:selectedKey", val);
    });
    const [openKey, setOpenKey] = useState("");
    const dataSourceKeyMap = computed(() => {
      const map = /* @__PURE__ */ new Map();
      dataSource == null ? void 0 : dataSource.forEach((page) => {
        var _a;
        map.set(page.key, page);
        (_a = page.children) == null ? void 0 : _a.forEach((item) => {
          map.set(item.key || item.title, __spreadValues({
            parentKey: page.key
          }, item));
        });
      });
      return map;
    });
    const parentKey = computed(() => {
      var _a;
      return (_a = dataSourceKeyMap.value.get(selectedKey.value)) == null ? void 0 : _a.parentKey;
    });
    return () => {
      var _a;
      const {
        title = "\u5E2E\u52A9\u4E2D\u5FC3",
        bordered = true,
        onClose,
        footer = (_a = slots.footer) == null ? void 0 : _a.call(slots),
        height,
        extraRender
      } = props;
      const defaultExtraActions = {
        collapsePanelAction: _createVNode("div", {
          "class": `${className.value}-actions-item ${hashId.value}`.trim()
        }, [_createVNode(ProfileOutlined, {
          "title": "collapse panel",
          "onClick": () => {
            var _a2;
            const newVal = !showLeftPanel.value;
            showLeftPanel.value = newVal;
            emit("update:showLeftPanel", newVal);
            (_a2 = props.onShowLeftPanelChange) == null ? void 0 : _a2.call(props, newVal);
          }
        }, null)]),
        helpSelectAction: _createVNode(ProHelpSelect, {
          "iconClassName": `${className.value}-actions-item`,
          "className": `${hashId.value} ${className.value}-actions-input`,
          "value": selectedKey.value,
          "onChange": (value, item) => {
            selectedKey.value = value;
            setOpenKey(item == null ? void 0 : item.dataItemKey);
          }
        }, null),
        closeAction: _createVNode("div", {
          "class": `${className.value}-actions-item ${hashId.value}`.trim()
        }, [_createVNode(CloseOutlined, {
          "title": "close panel",
          "onClick": () => {
            onClose == null ? void 0 : onClose();
          }
        }, null)])
      };
      const extraDomList = () => {
        return _createVNode("div", {
          "class": `${className.value}-actions ${hashId.value}`.trim()
        }, [extraRender ? extraRender(defaultExtraActions.collapsePanelAction, defaultExtraActions.helpSelectAction, defaultExtraActions.closeAction) : _createVNode(_Fragment, null, [defaultExtraActions.collapsePanelAction, defaultExtraActions.helpSelectAction, onClose ? defaultExtraActions.closeAction : null])]);
      };
      return wrapSSR(_createVNode(SelectKeyContext.Provider, {
        "value": {
          selectedKey: selectedKey.value
        }
      }, {
        default: () => [_createVNode(Card, {
          "bordered": bordered,
          "title": title,
          "bodyStyle": {
            display: "flex",
            padding: 0,
            margin: 0,
            height: "100%",
            width: "100%"
          },
          "size": "small",
          "extra": extraDomList()
        }, {
          default: () => {
            var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
            return [showLeftPanel.value ? _createVNode("div", {
              "class": `${hashId.value} ${className.value}-left-panel `,
              "style": {
                height: isNumeric(height) ? `${height}px` : height
              }
            }, [_createVNode(ConfigProvider, {
              "theme": {
                hashed: isNeedOpenHash(),
                token: {
                  lineHeight: 1.2,
                  fontSize: 12,
                  controlHeightLG: 26
                },
                components: {
                  Menu: {
                    radiusItem: token == null ? void 0 : token.borderRadius,
                    colorActiveBarWidth: 0,
                    colorActiveBarBorderSize: 0,
                    colorItemBgSelected: ((_b = (_a2 = token == null ? void 0 : token.layout) == null ? void 0 : _a2.sider) == null ? void 0 : _b.colorBgMenuItemSelected) || "rgba(0, 0, 0, 0.04)",
                    colorItemBgActive: ((_d = (_c = token == null ? void 0 : token.layout) == null ? void 0 : _c.sider) == null ? void 0 : _d.colorBgMenuItemHover) || "rgba(0, 0, 0, 0.04)",
                    colorItemText: ((_f = (_e = token == null ? void 0 : token.layout) == null ? void 0 : _e.sider) == null ? void 0 : _f.colorTextMenu) || "rgba(0, 0, 0, 0.65)",
                    colorItemTextHover: ((_h = (_g = token == null ? void 0 : token.layout) == null ? void 0 : _g.sider) == null ? void 0 : _h.colorTextMenuActive) || "rgba(0, 0, 0, 0.85)",
                    colorItemTextSelected: ((_j = (_i = token == null ? void 0 : token.layout) == null ? void 0 : _i.sider) == null ? void 0 : _j.colorTextMenuSelected) || "rgba(0, 0, 0, 1)",
                    colorItemBg: "transparent",
                    colorSubItemBg: "transparent",
                    // @ts-expect-error
                    popupBg: token == null ? void 0 : token.colorBgElevated,
                    darkPopupBg: token == null ? void 0 : token.colorBgElevated
                  }
                }
              }
            }, {
              default: () => [_createVNode(Menu, {
                "class": `${hashId.value} ${className.value}-left-panel-menu`,
                "openKeys": [parentKey.value, openKey.value],
                "onOpenChange": (keys) => {
                  setOpenKey(keys.at(-1) || "");
                },
                "selectedKeys": selectedKey.value ? [selectedKey.value] : [],
                "onSelect": ({
                  selectedKeys
                }) => {
                  selectedKey.value = selectedKeys.at(-1) || "";
                },
                "mode": "inline",
                "items": dataSource == null ? void 0 : dataSource.map((item) => {
                  return {
                    key: item.key,
                    label: item.title,
                    children: item.children.map((child) => {
                      return {
                        key: child.key,
                        label: child.title
                      };
                    })
                  };
                })
              }, null)]
            })]) : null, _createVNode("div", {
              "class": `${hashId.value} ${className.value}-content-panel`,
              "style": {
                height: isNumeric(height) ? `${height}px` : height
              }
            }, [selectedKey.value ? _createVNode(ProHelpContentPanel, {
              "parentItem": dataSourceKeyMap.value.get(parentKey.value),
              "class": `${className.value}-content-render`,
              "selectedKey": selectedKey.value,
              "onScroll": (key) => {
                selectedKey.value = key;
              }
            }, null) : null, footer ? _createVNode("div", {
              "class": `${hashId.value} ${className.value}-footer`
            }, [footer]) : null])];
          }
        })]
      }));
    };
  }
});
export {
  ProHelpPanel,
  SelectKeyContext,
  propHelpPanelProps
};
