import { getLocale } from "../../utils/locales.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { request } from "../../utils/index.js";
import { getProductExtensionFields } from "./server.js";
import { fields } from "./fields/index.js";
import LmCard from "./card/index.js";
import locales_default from "./locales.js";
import React, { useEffect, useMemo } from "react";
import { Form, Skeleton } from "antd";
import { locales } from "@pisell/utils";
import { useRequest } from "ahooks";
import "./index.less";
//#region src/components/productExtension/index.tsx
locales.init(locales_default, getLocale());
const ProductExtension = ({ onDataChange, type }) => {
	var _extension$filed;
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	const _getProductExtensionFields = useRequest(getProductExtensionFields, {
		manual: true,
		onSuccess: (res) => {
			onDataChange === null || onDataChange === void 0 || onDataChange(res);
		}
	});
	useEffect(() => {
		_getProductExtensionFields.run({ code: "event_item" });
	}, []);
	const extension = useMemo(() => {
		return _getProductExtensionFields.data || {};
	}, [_getProductExtensionFields.data]);
	const renderItem = (item, name) => {
		let { Component, defaultProps } = fields[item.key] || {};
		let _props = _objectSpread2({}, defaultProps);
		if (name) _props.name = name;
		return Component ? /* @__PURE__ */ React.createElement(Component, _props) : null;
	};
	return /* @__PURE__ */ React.createElement(Skeleton, {
		active: true,
		round: true,
		loading: _getProductExtensionFields.loading
	}, (_extension$filed = extension.filed) === null || _extension$filed === void 0 ? void 0 : _extension$filed.map((item, index) => {
		var _item$, _item$2, _item$3, _extension$extension;
		const isHidden = ((_item$ = item[0]) === null || _item$ === void 0 ? void 0 : _item$.key) === "event_item_type";
		if (type !== "all" && ((_item$2 = item[0]) === null || _item$2 === void 0 ? void 0 : _item$2.key) === "schedule.ids") return null;
		return /* @__PURE__ */ React.createElement(LmCard, {
			hidden: isHidden,
			key: index,
			header: item.length === 1 ? (_item$3 = item[0]) === null || _item$3 === void 0 ? void 0 : _item$3.name : extension === null || extension === void 0 || (_extension$extension = extension.extension) === null || _extension$extension === void 0 ? void 0 : _extension$extension.name
		}, item.map((child, childIndex) => {
			const name = `${child.id}|${child.key}`;
			return /* @__PURE__ */ React.createElement(Form.Item, {
				hidden: isHidden,
				label: item.length === 1 ? "" : child.name,
				name,
				rules: child.rules || child.key === "par_value" ? [{
					required: true,
					message: locales.getText("pisell2.text.product-extension.please-select")
				}] : [],
				key: childIndex,
				noStyle: true,
				initialValue: child.key === "event_item_capacity" ? 1 : void 0
			}, renderItem(child, name));
		}));
	}));
};
//#endregion
export { ProductExtension as default };
