import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getResourceTypes } from "../serve.js";
import React, { useMemo } from "react";
import { Empty, Spin, TreeSelect } from "antd";
import { locales } from "@pisell/utils";
import { useRequest } from "ahooks";
//#region src/plus/pisellSalesManagement/components/ResourceSelector.tsx
const formatResourceNode = (item) => {
	var _ref, _ref2, _ref3, _ref4, _ref5, _item$id, _ref6, _item$name, _item$resources;
	const key = (_ref = (_ref2 = (_ref3 = (_ref4 = (_ref5 = (_item$id = item.id) !== null && _item$id !== void 0 ? _item$id : item.value) !== null && _ref5 !== void 0 ? _ref5 : item.code) !== null && _ref4 !== void 0 ? _ref4 : item.name) !== null && _ref3 !== void 0 ? _ref3 : item.title) !== null && _ref2 !== void 0 ? _ref2 : item.label) !== null && _ref !== void 0 ? _ref : "";
	return {
		key,
		value: key,
		title: (_ref6 = (_item$name = item.name) !== null && _item$name !== void 0 ? _item$name : item.title) !== null && _ref6 !== void 0 ? _ref6 : item.label,
		children: formatResourceNodes((_item$resources = item.resources) !== null && _item$resources !== void 0 ? _item$resources : item.children)
	};
};
const formatResourceNodes = (items) => {
	if (!items) return [];
	return items.map(formatResourceNode);
};
const buildTreeData = (list) => {
	console.log("xxxxxx", list);
	const formatted = (list || []).map((item) => {
		var _item$resources2;
		return _objectSpread2(_objectSpread2({}, item), {}, { resources: (_item$resources2 = item.resources) !== null && _item$resources2 !== void 0 ? _item$resources2 : item.children });
	});
	formatted.unshift({
		id: 0,
		name: locales.getText("pisell2.salesManagement.booking.filter.resource.none"),
		resources: []
	});
	return formatResourceNodes(formatted);
};
const normalizeValue = (value) => {
	if (Array.isArray(value)) return value;
	if (value === void 0 || value === null) return [];
	return [value];
};
const extractLeafKeys = (nodes) => {
	if (!(nodes === null || nodes === void 0 ? void 0 : nodes.length)) return [];
	return nodes.filter((node) => !(node === null || node === void 0 ? void 0 : node.children) || node.children.length === 0).map((node) => {
		var _node$key;
		return (_node$key = node.key) !== null && _node$key !== void 0 ? _node$key : node.value;
	}).filter((key) => key !== void 0 && key !== null);
};
const ResourceSelector = (props) => {
	const { value, onChange, className, style } = props;
	const { data, loading } = useRequest(getResourceTypes);
	const treeData = useMemo(() => buildTreeData(data || []), [data]);
	const checkedKeys = useMemo(() => normalizeValue(value), [value]);
	return /* @__PURE__ */ React.createElement("div", {
		className,
		style
	}, loading ? /* @__PURE__ */ React.createElement(Spin, { size: "small" }) : treeData.length ? /* @__PURE__ */ React.createElement(TreeSelect, {
		maxTagCount: "responsive",
		value: checkedKeys,
		treeData,
		treeCheckable: true,
		showCheckedStrategy: 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 || extra === void 0 ? void 0 : extra.allCheckedNodes);
			const normalizedKeys = Array.isArray(nextValue) ? nextValue : nextValue != null ? [nextValue] : [];
			onChange === null || onChange === void 0 || onChange(leafKeys.length ? leafKeys : normalizedKeys);
		},
		placeholder: locales.getText("pisell2.salesManagement.booking.filter.resource.placeholder")
	}) : /* @__PURE__ */ React.createElement(Empty, { image: Empty.PRESENTED_IMAGE_SIMPLE }));
};
//#endregion
export { ResourceSelector as default };
