import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getNoResource } from "../../mock.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { formatResource } from "../../utils.js";
import React, { forwardRef, useCallback, useEffect, useState } from "react";
import { Select } from "antd";
import { isFunction } from "@pisell/utils";
import AlertTriangle from "@pisell/icon/es/AlertTriangle";
//#region src/components/booking/materiels/resources/index.tsx
const _excluded = [
	"locale",
	"resources",
	"watch",
	"getResources",
	"resourcesChange",
	"currentDate",
	"cacheItem",
	"selectProps"
];
const Resources = forwardRef((props, ref) => {
	const { locale = "en", resources = [], watch, getResources, resourcesChange, currentDate, cacheItem, selectProps } = props, resetProps = _objectWithoutProperties(props, _excluded);
	const [loading, setLoading] = useState(false);
	const [_resources, set_resources] = useState([]);
	const renderOptions = useCallback((array, value = "value", label = "label", type) => {
		return array.map((d) => {
			let props = {};
			if (type === "resource") props.labelText = d.labelText;
			return /* @__PURE__ */ React.createElement(Select.Option, _objectSpread2({
				value: d[value],
				key: d[value]
			}, props), d[label]);
		});
	}, []);
	useEffect(() => {
		_getResources();
	}, [watch]);
	/**
	* @title: 获取资源列表
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-09 13:38
	*/
	const _getResources = function() {
		var _ref = _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 = [getNoResource()].concat((data === null || data === void 0 ? void 0 : data.list) || []);
			const _cacheItem = isFunction(cacheItem) ? cacheItem() : cacheItem;
			if (!(_cacheItem === null || _cacheItem === void 0 ? void 0 : _cacheItem._extend)) _list = _list.map((d) => {
				return _objectSpread2(_objectSpread2({}, d), {}, {
					label: d.room_name || d.name,
					labelText: d.room_name || d.name
				});
			});
			else {
				_list = _list.map((d) => formatResource(d, _cacheItem, (item) => {
					return /* @__PURE__ */ React.createElement("div", { className: "resource-option" }, /* @__PURE__ */ React.createElement("div", { className: "like-wrap" }), item.labelText, !item.usable && /* @__PURE__ */ React.createElement(AlertTriangle, { 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.createElement(Select, _objectSpread2(_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
export { Resources as default };
