const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_mock = require("../../mock.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_utils = require("../../utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_icon_es_AlertTriangle = require("@pisell/icon/es/AlertTriangle");
_pisell_icon_es_AlertTriangle = require_runtime.__toESM(_pisell_icon_es_AlertTriangle);
//#region src/components/booking/materiels/resources/index.tsx
const _excluded = [
	"locale",
	"resources",
	"watch",
	"getResources",
	"resourcesChange",
	"currentDate",
	"cacheItem",
	"selectProps"
];
const Resources = (0, react.forwardRef)((props, ref) => {
	const { locale = "en", resources = [], watch, getResources, resourcesChange, currentDate, cacheItem, selectProps } = props, resetProps = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const [loading, setLoading] = (0, react.useState)(false);
	const [_resources, set_resources] = (0, react.useState)([]);
	const renderOptions = (0, react.useCallback)((array, value = "value", label = "label", type) => {
		return array.map((d) => {
			let props = {};
			if (type === "resource") props.labelText = d.labelText;
			return /* @__PURE__ */ react.default.createElement(antd.Select.Option, require_objectSpread2._objectSpread2({
				value: d[value],
				key: d[value]
			}, props), d[label]);
		});
	}, []);
	(0, react.useEffect)(() => {
		_getResources();
	}, [watch]);
	/**
	* @title: 获取资源列表
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-09 13:38
	*/
	const _getResources = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			setLoading(true);
			let data;
			if (getResources) data = yield getResources({
				skip: 1,
				num: 1e3,
				with_date_times: currentDate,
				codes: [
					"resources",
					"therapist",
					"table"
				]
			});
			else data = resources;
			let _list = [require_mock.getNoResource()].concat((data === null || data === void 0 ? void 0 : data.list) || []);
			const _cacheItem = (0, _pisell_utils.isFunction)(cacheItem) ? cacheItem() : cacheItem;
			if (!(_cacheItem === null || _cacheItem === void 0 ? void 0 : _cacheItem._extend)) _list = _list.map((d) => {
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, d), {}, {
					label: d.room_name || d.name,
					labelText: d.room_name || d.name
				});
			});
			else {
				_list = _list.map((d) => require_utils.formatResource(d, _cacheItem, (item) => {
					return /* @__PURE__ */ react.default.createElement("div", { className: "resource-option" }, /* @__PURE__ */ react.default.createElement("div", { className: "like-wrap" }), item.labelText, !item.usable && /* @__PURE__ */ react.default.createElement(_pisell_icon_es_AlertTriangle.default, { className: "resource-option-alert-icon" }));
				}));
				_list = _list.sort((a, b) => {
					if (!a.usable && !b.usable) return 0;
					else if (!b.usable) return -1;
					else if (!a.usable) return 1;
					return 0;
				});
			}
			resourcesChange === null || resourcesChange === void 0 || resourcesChange(_list);
			set_resources(_list);
			setLoading(false);
		});
		return function _getResources() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
		allowClear: true,
		showSearch: true,
		filterOption: (str, option) => {
			return (option.labelText || "").toLowerCase().includes((str || "").toLowerCase());
		},
		loading
	}, selectProps), resetProps), {}, { ref }), renderOptions(_resources || [], "form_record_id", "label", "resource"));
});
//#endregion
exports.default = Resources;
