const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_server = require("./server.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/productExtension/fields/SelectResourcesTable/index.tsx
const sortArrayByOrder = (arr1, arr2, reverse) => {
	return (arr1 || []).sort((a, b) => {
		let _a = arr2.findIndex((item) => item.form_id === a.form_id);
		let _b = arr2.findIndex((item) => item.form_id === b.form_id);
		return reverse ? _b - _a : _a - _b;
	});
};
const SelectResourcesTable = ({ value, onChange }) => {
	const Dom = window.BaseMaterials.Table;
	const [data, setData] = (0, react.useState)([]);
	/**
	* @title: 获取数据
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-11-04 17:07
	*/
	const _getData = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			let _getFormTypes = require_server.getFormTypes({
				type: "manual",
				num: 500,
				skip: 1,
				status: "published"
			}, { useCache: true });
			let _getAllResource = require_server.getAllResource({
				num: 500,
				skip: 1
			}, { useCache: true });
			const [forms, resources] = yield Promise.all([_getFormTypes, _getAllResource]);
			let defaultResourceId = getDefaultResourceId();
			setData((forms || []).reduce((arr, item) => {
				const currentResource = (resources || []).filter((relation) => relation.form_id === item.id);
				if (currentResource.length > 0) {
					if (defaultResourceId && currentResource.some((d) => d.id == defaultResourceId)) {
						_onChange([defaultResourceId], item.id);
						removeDefaultResourceId();
						defaultResourceId = void 0;
					}
					arr.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
						label: item.title,
						key: item.id,
						value: item.id,
						options: currentResource
					}, item), {}, { form_id: item.id }));
				}
				return arr;
			}, []));
		});
		return function _getData() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 获取默认选择的资源
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-11-04 17:09
	*/
	const getDefaultResourceId = () => {
		let serviceParams = sessionStorage.getItem("pisell2.eventBooking.serviceParams");
		if (serviceParams) serviceParams = JSON.parse(serviceParams);
		let resource_id = serviceParams === null || serviceParams === void 0 ? void 0 : serviceParams.resource_id;
		if (resource_id) resource_id = Number(resource_id);
		return resource_id;
	};
	const removeDefaultResourceId = () => {
		sessionStorage.setItem("pisell2.eventBooking.serviceParams", "");
	};
	const _dataSource = (0, react.useMemo)(() => {
		if (value) {
			let sortOrder = {};
			let arr1Copy = data.slice();
			value.forEach((item, index) => {
				sortOrder[item.form_id] = index;
			});
			arr1Copy.sort((a, b) => {
				if (sortOrder[a.form_id] !== void 0 && sortOrder[b.form_id] !== void 0) return sortOrder[a.form_id] - sortOrder[b.form_id];
				if (sortOrder[a.form_id] !== void 0) return -1;
				if (sortOrder[b.form_id] !== void 0) return 1;
				return 0;
			});
			return arr1Copy;
		}
		return data;
	}, [data, value]);
	const _onChange = (val, form_id) => {
		let _value = JSON.parse(JSON.stringify(value || []));
		let cur = _value.find((d) => d.form_id === form_id);
		if (cur) cur.form_record_ids = val;
		else _value.push({
			form_id,
			form_record_ids: val
		});
		onChange === null || onChange === void 0 || onChange(_value);
	};
	console.log("value", value);
	const columns = (0, react.useMemo)(() => {
		return [{
			title: _pisell_utils.locales.getText("pisell2.text.product-extension.resource-type"),
			dataIndex: "title",
			value,
			data,
			width: 100
		}, {
			title: _pisell_utils.locales.getText("pisell2.text.product-extension.options"),
			dataIndex: "form_id",
			render: (form_id, record, index) => {
				let cur = value === null || value === void 0 ? void 0 : value.find((d) => d.form_id === form_id);
				return /* @__PURE__ */ react.default.createElement(antd.Select, {
					style: { width: "100%" },
					mode: "multiple",
					value: (cur === null || cur === void 0 ? void 0 : cur.form_record_ids) || [],
					options: record.options,
					popupMatchSelectWidth: false,
					fieldNames: {
						label: "title",
						value: "id"
					},
					onChange: (val) => {
						_onChange(val, form_id);
					}
				});
			}
		}];
	}, [value, _dataSource]);
	const sortChange = (detail) => {
		if (value && value.length) onChange === null || onChange === void 0 || onChange(sortArrayByOrder(value, detail.dataSource));
	};
	(0, react.useEffect)(() => {
		_getData();
	}, []);
	return /* @__PURE__ */ react.default.createElement(Dom, {
		className: "",
		dragSort: true,
		onSortChange: sortChange,
		rowKey: "form_id",
		columns,
		dataSource: _dataSource,
		pagination: false,
		virtual: false,
		scroll: { x: "auto" }
	});
};
//#endregion
exports.default = SelectResourcesTable;
