import { _objectSpread2 } from "../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getErrorList } from "../../../utilsByBooking.js";
import { getAutoAllocationStorage, setAutoAllocationStorage } from "../utils.js";
import Like from "../../../../info/service/like/index.js";
import MaxLengthSelect_default from "./MaxLengthSelect/index.js";
import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useState } from "react";
import { Alert, Button, Checkbox, Col, Form, Row, Select, Tooltip } from "antd";
import { locales } from "@pisell/utils";
import { isArray } from "lodash";
import { Iconfont } from "@pisell/materials";
import classNames from "classnames";
import { useUpdate } from "ahooks";
import { QuestionCircleOutlined } from "@ant-design/icons";
import "./index.less";
//#region src/components/booking/info2/service/editService/ResourcesV2/index.tsx
const Resources = ({ state, cacheItem, originCacheItem, getCurrentCacheItem, onChange, value, originValue, config = {
	autoAllocation: true,
	resourceErrorIcon: true,
	disabled: false,
	size: "large"
} }, ref) => {
	var _cacheItem$_data2, _originCacheItem$_dat, _cacheItem$_data5;
	const [showError, setShowError] = useState(false);
	const update = useUpdate();
	const data = useMemo(() => {
		var _cacheItem$_data;
		return (cacheItem === null || cacheItem === void 0 || (_cacheItem$_data = cacheItem._data) === null || _cacheItem$_data === void 0 ? void 0 : _cacheItem$_data.resources) || [];
	}, [cacheItem === null || cacheItem === void 0 || (_cacheItem$_data2 = cacheItem._data) === null || _cacheItem$_data2 === void 0 ? void 0 : _cacheItem$_data2.resources]);
	const originData = useMemo(() => {
		var _cacheItem$_data3;
		return (cacheItem === null || cacheItem === void 0 || (_cacheItem$_data3 = cacheItem._data) === null || _cacheItem$_data3 === void 0 ? void 0 : _cacheItem$_data3.resources) || [];
	}, [originCacheItem === null || originCacheItem === void 0 || (_originCacheItem$_dat = originCacheItem._data) === null || _originCacheItem$_dat === void 0 ? void 0 : _originCacheItem$_dat.resources]);
	const resourceMaps = useMemo(() => {
		var _cacheItem$_data4;
		return (cacheItem === null || cacheItem === void 0 || (_cacheItem$_data4 = cacheItem._data) === null || _cacheItem$_data4 === void 0 ? void 0 : _cacheItem$_data4.resourceMaps) || [];
	}, [cacheItem === null || cacheItem === void 0 || (_cacheItem$_data5 = cacheItem._data) === null || _cacheItem$_data5 === void 0 ? void 0 : _cacheItem$_data5.resourceMaps]);
	const defaultValue = useMemo(() => {
		return data.reduce((pre, cur) => {
			pre[cur.form_id] = [];
			return pre;
		}, {});
	}, [data]);
	const _value = useMemo(() => {
		return value || defaultValue;
	}, [value, defaultValue]);
	const handleCheckAutoAllocation = (id, checked) => {
		setAutoAllocationStorage({ [id]: checked });
		update();
	};
	/**
	* @title: 当选择资源时
	* @description:
	* @param {any} val
	* @param {number} form_id
	* @param {string} key
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-09-19 21:04
	*/
	const _onChange = (val, form_id, key) => {
		let _val = _objectSpread2({}, _value);
		let maps = _val[form_id] ? _val[form_id].reduce((pre, cur) => {
			pre[cur.relation_id] = cur;
			return pre;
		}, {}) : {};
		_val[form_id] = val.map((d) => {
			var _currentForm$renderLi, _currentForm$renderLi2, _currentForm$renderLi3, _currentForm$renderLi4, _currentForm$renderLi5;
			if (maps[d]) {
				if (key === "like_status") d[key].like_status = val;
			}
			const currentForm = (resourceMaps === null || resourceMaps === void 0 ? void 0 : resourceMaps[form_id]) || {};
			return maps[d] || {
				relation_type: ((_currentForm$renderLi = currentForm.renderListMaps[d]) === null || _currentForm$renderLi === void 0 || (_currentForm$renderLi = _currentForm$renderLi.usable) === null || _currentForm$renderLi === void 0 ? void 0 : _currentForm$renderLi.relation_type) || "form",
				form_id,
				relation_id: d,
				like_status: "common",
				children: (_currentForm$renderLi2 = currentForm.renderListMaps) === null || _currentForm$renderLi2 === void 0 || (_currentForm$renderLi2 = _currentForm$renderLi2[d]) === null || _currentForm$renderLi2 === void 0 ? void 0 : _currentForm$renderLi2.children,
				errors: getErrorList((_currentForm$renderLi3 = currentForm.renderListMaps) === null || _currentForm$renderLi3 === void 0 ? void 0 : _currentForm$renderLi3[d], getCurrentCacheItem()),
				metadata: _objectSpread2(_objectSpread2({}, (_currentForm$renderLi4 = currentForm.renderListMaps) === null || _currentForm$renderLi4 === void 0 || (_currentForm$renderLi4 = _currentForm$renderLi4[d]) === null || _currentForm$renderLi4 === void 0 ? void 0 : _currentForm$renderLi4.metadata), {}, {
					form_name: currentForm.title,
					resource_name: (_currentForm$renderLi5 = currentForm.renderListMaps[d]) === null || _currentForm$renderLi5 === void 0 ? void 0 : _currentForm$renderLi5.main_field
				})
			};
		});
		setShowError(false);
		onChange === null || onChange === void 0 || onChange(_val);
	};
	const setLikeStatus = (val, form_id, key) => {
		var _val$form_id;
		let _val = _objectSpread2({}, _value);
		if (_val === null || _val === void 0 || (_val$form_id = _val[form_id]) === null || _val$form_id === void 0 ? void 0 : _val$form_id[0]) {
			_val[form_id][0][key] = val;
			onChange === null || onChange === void 0 || onChange(_val);
		}
	};
	/**
	* @title: 渲染下拉框
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-09 14:02
	*/
	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(_objectSpread2({
				value: d[value],
				key: d[value]
			}, props), {}, { relation_type: d.relation_type }), d[label]);
		});
	}, [config === null || config === void 0 ? void 0 : config.resourceErrorIcon]);
	/**
	* @title: 检查当前资源最小选择数量
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-09-19 21:08
	*/
	const check = () => {
		for (let item of data) {
			var _value$item$form_id;
			let _length = (_value$item$form_id = _value[item.form_id]) === null || _value$item$form_id === void 0 ? void 0 : _value$item$form_id.length;
			if (item.select_type.min > _length) {
				setShowError(true);
				return false;
			}
		}
		return true;
	};
	useImperativeHandle(ref, () => {
		return { check };
	});
	const getIsAutoAllocation = (id) => {
		return getAutoAllocationStorage(id);
	};
	const renderItem = (item, isOrigin) => {
		var _cur$2;
		if (!item) return null;
		const disabled = isOrigin || (config === null || config === void 0 ? void 0 : config.disabled);
		const cur = isOrigin ? originValue[item.form_id] : _value[item.form_id];
		return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Col, { flex: "1 0" }, /* @__PURE__ */ React.createElement("div", { className: "resource-v2 resource-col" }, item.select_type.type === "multiple" ? null : /* @__PURE__ */ React.createElement(Button, {
			className: classNames("resource-col-like-wrap", { "resource-col-like-wrap-disabled": disabled }, (config === null || config === void 0 ? void 0 : config.size) || "large"),
			size: config === null || config === void 0 ? void 0 : config.size,
			onClick: () => {
				var _cur$;
				if (disabled) return;
				setLikeStatus((cur === null || cur === void 0 || (_cur$ = cur[0]) === null || _cur$ === void 0 ? void 0 : _cur$.like_status) === "like" ? "common" : "like", item.form_id, "like_status");
			}
		}, /* @__PURE__ */ React.createElement(Like, {
			status: cur === null || cur === void 0 || (_cur$2 = cur[0]) === null || _cur$2 === void 0 ? void 0 : _cur$2.like_status,
			className: `${disabled ? "like-icon-disabled" : ""}`
		})), /* @__PURE__ */ React.createElement(MaxLengthSelect_default, {
			className: "resource-col-select",
			size: (config === null || config === void 0 ? void 0 : config.size) || "large",
			maxLength: item.select_type.max,
			mode: item.select_type.type === "multiple" ? "multiple" : void 0,
			onChange: (e) => {
				let val = e;
				if (!isArray(e)) {
					if (e || e === 0) val = [e];
				}
				_onChange(val, item.form_id, "value");
			},
			value: cur === null || cur === void 0 ? void 0 : cur.map((d) => d.relation_id),
			showSearch: true,
			filterOption: (str, option) => {
				return ((option === null || option === void 0 ? void 0 : option.labelText) || "").toLowerCase().includes((str || "").toLowerCase());
			},
			disabled
		}, renderOptions(item.renderList, "id", (config === null || config === void 0 ? void 0 : config.resourceErrorIcon) ? "label" : "labelText", "resource")), (config === null || config === void 0 ? void 0 : config.autoAllocation) ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Checkbox, {
			checked: getIsAutoAllocation(item.form_id),
			onChange: (e) => {
				handleCheckAutoAllocation(item.form_id, e.target.checked);
			}
		}), /* @__PURE__ */ React.createElement(Tooltip, { title: locales.getText("page.booking.text.resource.check-auto-allocation-tip") }, /* @__PURE__ */ React.createElement(QuestionCircleOutlined, null))) : null)));
	};
	const renderErrors = (item) => {
		const errors = (_value[item.form_id] || []).reduce((pre, d) => {
			return pre.concat((d === null || d === void 0 ? void 0 : d.errors) || []);
		}, []);
		return state.renderType !== "eventBooking" && (errors || []).map((d) => {
			return /* @__PURE__ */ React.createElement(Alert, {
				key: d.type,
				message: d.label,
				type: "warning",
				showIcon: true,
				style: { marginBottom: 12 }
			});
		});
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, data.map((item) => {
		var _cur;
		const originItem = originData === null || originData === void 0 ? void 0 : originData.find((originDataItem) => originDataItem.form_id === item.form_id);
		return /* @__PURE__ */ React.createElement(React.Fragment, { key: item.form_id }, /* @__PURE__ */ React.createElement(Form.Item, {
			label: item.title,
			required: true,
			labelCol: config === null || config === void 0 ? void 0 : config.labelCol,
			extra: showError && item.select_type.min > ((_cur = cur) === null || _cur === void 0 ? void 0 : _cur.length) ? /* @__PURE__ */ React.createElement("span", { style: { color: "#ff4d4f" } }, locales.getText("page.booking.text.resource.min-length-error")(item.select_type.min, item.title)) : void 0
		}, /* @__PURE__ */ React.createElement(Row, {
			gutter: [16, 16],
			wrap: false
		}, renderItem(originItem, true), !!originItem ? /* @__PURE__ */ React.createElement(Col, {
			flex: "none",
			style: {
				display: "flex",
				alignItems: "center"
			}
		}, /* @__PURE__ */ React.createElement(Iconfont, {
			type: "pisell2-arrow-block-right",
			style: {
				fontSize: 24,
				color: "#1b1b1b"
			}
		})) : null, renderItem(item, false))), renderErrors(item));
	}));
};
var ResourcesV2_default = forwardRef(Resources);
//#endregion
export { ResourcesV2_default as default };
