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 { Fragment as _Fragment, createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
import { SearchOutlined } from "@ant-design/icons-vue";
import ProProvider, { useStyle } from "@antdv/pro-provider";
import { arrayType, classNames, omit, stringType, useDebounceFn, useState } from "@antdv/pro-utils";
import { Select } from "ant-design-vue";
import { selectProps } from "ant-design-vue/es/select";
import { useConfigContextInject } from "ant-design-vue/lib/config-provider/context";
import { defineComponent, h } from "vue";
import { ProHelpContext } from "./HelpProvide.mjs";
const Highlight = defineComponent({
  name: "Highlight",
  props: {
    label: stringType().isRequired,
    words: arrayType().isRequired
  },
  setup(props) {
    const {
      getPrefixCls
    } = useConfigContextInject();
    const lightCls = getPrefixCls("pro-help-search-list-item-content-light");
    const optionCls = getPrefixCls("pro-help-search-list-item-content");
    const {
      wrapSSR
    } = useStyle("Highlight", (token) => {
      return {
        [`.${lightCls}`]: {
          color: token.colorPrimary
        },
        [`.${optionCls}`]: {
          flex: "auto",
          overflow: "hidden",
          whiteSpace: "nowrap",
          textOverflow: "ellipsis"
        }
      };
    });
    return () => {
      const {
        words,
        label
      } = props;
      if (words.length === 0) return _createVNode(_Fragment, null, [label]);
      const matchKeywordsRE = new RegExp(words.map((word) => word.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&")).join("|"), "gi");
      let matchText = label;
      const elements = [];
      while (matchText.length) {
        const match = matchKeywordsRE.exec(matchText);
        if (!match) {
          elements.push(matchText);
          break;
        }
        const start = match.index;
        const matchLength = match[0].length + start;
        elements.push(matchText.slice(0, start), h("span", {
          className: lightCls
        }, matchText.slice(start, matchLength)));
        matchText = matchText.slice(matchLength);
      }
      return wrapSSR(h("div", {
        title: label,
        className: optionCls
      }, elements));
    };
  }
});
const ProHelpSelect = defineComponent({
  name: "ProHelpSelect",
  props: __spreadProps(__spreadValues({}, omit(selectProps(), ["onSearch", "optionFilterProp", "options", "filterOption"])), {
    className: stringType().isRequired,
    iconClassName: stringType()
  }),
  setup(props) {
    const {
      dataSource
    } = ProHelpContext.useContext();
    const {
      hashId
    } = ProProvider.useContext();
    const [keyWord, setKeyWork] = useState("");
    const [open, setOpen] = useState(false);
    const debounceSetKeyWork = useDebounceFn((key) => __async(this, null, function* () {
      return setKeyWork(key);
    }), 20);
    return () => {
      return _createVNode(_Fragment, null, [!open.value ? _createVNode("div", {
        "class": classNames(props.iconClassName, hashId)
      }, [_createVNode(SearchOutlined, {
        "title": "search panel",
        "onClick": () => {
          setOpen(true);
        }
      }, null)]) : null, open.value ? _createVNode(Select, _mergeProps({
        "placeholder": "please input search text",
        "showSearch": true,
        "bordered": false,
        "onBlur": () => {
          setOpen(false);
        },
        "size": "small"
      }, omit(props, ["size", "showSearch", "bordered", "onBlur", "placeholder", "choiceTransitionName"]), {
        "choiceTransitionName": props.choiceTransitionName,
        "onSearch": (value) => {
          debounceSetKeyWork.cancel();
          debounceSetKeyWork.run(value);
        },
        "filterOption": (input, option) => {
          var _a;
          return ((_a = option == null ? void 0 : option.title) != null ? _a : "").toLowerCase().includes(input.toLowerCase());
        },
        "dropdownMatchSelectWidth": false,
        "options": dataSource == null ? void 0 : dataSource.map((item) => {
          var _a;
          return {
            label: _createVNode(Highlight, {
              "label": item.title,
              "words": [keyWord.value].filter(Boolean)
            }, null),
            title: item.title,
            value: item.key,
            options: (_a = item.children) == null ? void 0 : _a.map((sunItem) => {
              return {
                label: _createVNode(Highlight, {
                  "label": sunItem.title,
                  "words": [keyWord.value].filter(Boolean)
                }, null),
                title: sunItem.title,
                value: sunItem.key,
                dataItemKey: item.key
              };
            })
          };
        })
      }), null) : null]);
    };
  }
});
export {
  Highlight,
  ProHelpSelect
};
