var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellSalesManagement/components/ResourceSelector.tsx
var ResourceSelector_exports = {};
__export(ResourceSelector_exports, {
  default: () => ResourceSelector_default
});
module.exports = __toCommonJS(ResourceSelector_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_serve = require("../serve");
var formatResourceNode = (item) => {
  const key = item.id ?? item.value ?? item.code ?? item.name ?? item.title ?? item.label ?? "";
  return {
    key,
    value: key,
    title: item.name ?? item.title ?? item.label,
    children: formatResourceNodes(item.resources ?? item.children)
  };
};
var formatResourceNodes = (items) => {
  if (!items) return [];
  return items.map(formatResourceNode);
};
var buildTreeData = (list) => {
  console.log("xxxxxx", list);
  const formatted = (list || []).map((item) => ({
    ...item,
    resources: item.resources ?? item.children
  }));
  formatted.unshift({
    id: 0,
    name: import_utils.locales.getText(
      "pisell2.salesManagement.booking.filter.resource.none"
    ),
    resources: []
  });
  return formatResourceNodes(formatted);
};
var normalizeValue = (value) => {
  if (Array.isArray(value)) return value;
  if (value === void 0 || value === null) return [];
  return [value];
};
var extractLeafKeys = (nodes) => {
  if (!(nodes == null ? void 0 : nodes.length)) return [];
  return nodes.filter((node) => !(node == null ? void 0 : node.children) || node.children.length === 0).map((node) => node.key ?? node.value).filter((key) => key !== void 0 && key !== null);
};
var ResourceSelector = (props) => {
  const { value, onChange, className, style } = props;
  const { data, loading } = (0, import_ahooks.useRequest)(import_serve.getResourceTypes);
  const treeData = (0, import_react.useMemo)(() => buildTreeData(data || []), [data]);
  const checkedKeys = (0, import_react.useMemo)(() => normalizeValue(value), [value]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className, style }, loading ? /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "small" }) : treeData.length ? /* @__PURE__ */ import_react.default.createElement(
    import_antd.TreeSelect,
    {
      maxTagCount: "responsive",
      value: checkedKeys,
      treeData,
      treeCheckable: true,
      showCheckedStrategy: import_antd.TreeSelect.SHOW_CHILD,
      treeNodeFilterProp: "title",
      allowClear: true,
      multiple: true,
      style: { width: "100%" },
      dropdownStyle: { maxHeight: 320, overflow: "auto" },
      onChange: (nextValue, _label, extra) => {
        const leafKeys = extractLeafKeys(extra == null ? void 0 : extra.allCheckedNodes);
        const normalizedKeys = Array.isArray(nextValue) ? nextValue : nextValue != null ? [nextValue] : [];
        onChange == null ? void 0 : onChange(leafKeys.length ? leafKeys : normalizedKeys);
      },
      placeholder: import_utils.locales.getText(
        "pisell2.salesManagement.booking.filter.resource.placeholder"
      )
    }
  ) : /* @__PURE__ */ import_react.default.createElement(import_antd.Empty, { image: import_antd.Empty.PRESENTED_IMAGE_SIMPLE }));
};
var ResourceSelector_default = ResourceSelector;
