const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_locales = require("../../utils/locales.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_useEngineContext = require("../../hooks/useEngineContext.js");
const require_index = require("../../utils/index.js");
const require_index$1 = require("../listComponent/index.js");
const require_index$2 = require("./ruleBorder/index.js");
const require_server = require("./server.js");
const require_locales$1 = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/components/ruleSetting/index.tsx
const createRuleItem = () => {
	return {
		operator: "contain",
		class: "type",
		value: ["all"],
		_id: (0, _pisell_utils.getUniqueId)()
	};
};
_pisell_utils.locales.init(require_locales$1.default, require_locales.getLocale());
const RuleSetting = (props) => {
	var _context$appHelper;
	const { value = {
		rule: [],
		type: "and"
	}, onChange, className, style } = props;
	console.log("valuevalue", value);
	const context = require_useEngineContext.default();
	if (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request) require_index.request.setRequest(context.appHelper.utils.request);
	const listRef = (0, react.useRef)(null);
	const [loading, setLoading] = (0, react.useState)(false);
	const [orderTypes, setOrderTypes] = (0, react.useState)([]);
	const [salesChannels, setSalesChannels] = (0, react.useState)([]);
	const [staffs, setStaffs] = (0, react.useState)([]);
	const [paymentMethods, setPaymentMethod] = (0, react.useState)([]);
	const init = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			let _getStaffList = require_server.getStaffList();
			const _getProductChannel = require_server.getProductChannel();
			const _getOrderTypes = require_server.getOrderTypes();
			const _getPaymentTypes = require_server.getPaymentTypes();
			const [staffs, clannels, orders, payments] = yield Promise.all([
				_getStaffList,
				_getProductChannel,
				_getOrderTypes,
				_getPaymentTypes
			]);
			console.log("staffs", staffs, clannels, orders, payments);
			setStaffs(staffs);
			setSalesChannels(clannels);
			setOrderTypes(orders);
			setPaymentMethod(payments);
		});
		return function init() {
			return _ref.apply(this, arguments);
		};
	}();
	(0, react.useEffect)(() => {
		document.body.id = "body";
		init();
	}, []);
	const config = (0, react.useMemo)(() => {
		return {
			type: {
				key: "type",
				style: { width: 200 },
				size: "large",
				options: [
					{
						label: _pisell_utils.locales.getText("pisell2.text.rule-setting.order_type"),
						value: "type",
						list: orderTypes
					},
					{
						label: _pisell_utils.locales.getText("pisell2.text.rule-setting.sales_channel"),
						value: "platform",
						list: salesChannels
					},
					{
						label: _pisell_utils.locales.getText("pisell2.text.rule-setting.staff"),
						value: "staff",
						list: staffs
					},
					{
						label: _pisell_utils.locales.getText("pisell2.text.rule-setting.payment_method"),
						value: "payment_method",
						list: paymentMethods
					},
					{
						label: _pisell_utils.locales.getText("pisell2.text.rule-setting.pisell_terminal_device"),
						value: "pos",
						list: [{
							label: _pisell_utils.locales.getText("pisell2.text.rule-setting.current_device"),
							value: "current"
						}]
					}
				]
			},
			rule: {
				key: "rule",
				style: { width: 100 },
				size: "large",
				options: [{
					label: _pisell_utils.locales.getText("pisell2.text.rule-setting.Contain"),
					value: "contain"
				}, {
					label: _pisell_utils.locales.getText("pisell2.text.rule-setting.Exclude"),
					value: "exclude"
				}]
			},
			options: {
				key: "options",
				style: { flex: 1 },
				size: "large",
				mode: "multiple",
				filterOption: false,
				showSearch: false
			}
		};
	}, [
		staffs,
		salesChannels,
		orderTypes,
		paymentMethods
	]);
	console.log("configconfig", config);
	const handleAddItem = (item) => {
		let _item = require_objectSpread2._objectSpread2({}, item);
		_item.rule.push(createRuleItem());
		return _item;
	};
	const updateRuleItem = (item, cId, val) => {
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { rule: item.rule.map((d) => {
			if (d._id === cId) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, d), val);
			return d;
		}) });
	};
	const filterAllOption = (list, current) => {
		let _list = [...list || []];
		if (!_list.includes("all")) return _list;
		if (_list.length === 1) return _list;
		if (!(current || []).includes("all")) return ["all"];
		return _list.filter((d) => d !== "all");
	};
	return /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
		round: true,
		active: true,
		loading,
		className,
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "rule-setting-wrap" }, value.rule.length > 1 ? /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		value: value.type,
		onChange: (e) => {
			onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { type: e }));
		},
		startDomClassName: `rule-setting-item-${value.rule[0]._id}`,
		endDomClassName: `rule-setting-item-${value.rule[value.rule.length - 1]._id}`
	}) : null, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		ref: listRef,
		value: value.rule,
		onChange: (rule) => {
			onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { rule }));
		},
		rowKey: "_id",
		wrapClassName: "rule-setting-list",
		createItem: (item) => {
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
				type: "and",
				rule: [{
					_id: "-1",
					class: "type",
					operator: "contain",
					value: ["all"]
				}]
			});
		},
		renderItem: (item, index, remove, update) => {
			return /* @__PURE__ */ react.default.createElement("div", {
				className: (0, classnames.default)("rule-setting-item", `rule-setting-item-${item._id}`),
				key: item._id
			}, /* @__PURE__ */ react.default.createElement("div", { className: "rule-setting-item-header" }, /* @__PURE__ */ react.default.createElement("div", { className: "rule-setting-item-title" }, _pisell_utils.locales.getText("pisell2.text.satisfy_any_condition")), /* @__PURE__ */ react.default.createElement("div", { className: "rule-setting-item-btns" }, /* @__PURE__ */ react.default.createElement(_ant_design_icons.PlusOutlined, {
				className: "rule-setting-item-btn",
				onClick: () => {
					update(item, handleAddItem(item));
				}
			}), value.rule.length > 1 ? /* @__PURE__ */ react.default.createElement(_ant_design_icons.CloseOutlined, {
				className: "rule-setting-item-btn",
				onClick: () => {
					remove(item);
				}
			}) : null)), /* @__PURE__ */ react.default.createElement("div", { className: "rule-setting-item-body" }, item.rule.length > 1 ? /* @__PURE__ */ react.default.createElement(require_index$2.default, {
				value: item.type,
				onChange: (e) => {
					update(item, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { type: e }));
				},
				startDomClassName: `rule-setting-item-body-item-${item.rule[0]._id}`,
				endDomClassName: `rule-setting-item-body-item-${item.rule[item.rule.length - 1]._id}`
			}) : null, /* @__PURE__ */ react.default.createElement("div", { className: "rule-setting-item-body-list" }, item.rule.map((rule) => {
				var _config$type$options$;
				return /* @__PURE__ */ react.default.createElement("div", {
					className: (0, classnames.default)("rule-setting-item-body-item", `rule-setting-item-body-item-${rule._id}`),
					key: rule._id
				}, /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config.type), {}, {
					value: rule.class,
					onChange: (e) => {
						update(item, updateRuleItem(item, rule._id, {
							class: e,
							value: ["all"]
						}));
					}
				})), /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config.rule), {}, {
					value: rule.operator,
					onChange: (e) => {
						update(item, updateRuleItem(item, rule._id, { operator: e }));
					}
				})), /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config.options), {}, {
					options: ((_config$type$options$ = config.type.options.find((d) => d.value === rule.class)) === null || _config$type$options$ === void 0 ? void 0 : _config$type$options$.list) || [],
					value: rule.value,
					onChange: (e, option) => {
						console.log("options", option);
						update(item, updateRuleItem(item, rule._id, { value: filterAllOption(e, rule.value) }));
					}
				})), item.rule.length > 1 ? /* @__PURE__ */ react.default.createElement(_ant_design_icons.CloseOutlined, { onClick: () => {
					update(item, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { rule: item.rule.filter((d) => d._id !== rule._id) }));
				} }) : null);
			}))));
		},
		renderAddButton: () => null
	})), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("rule-setting-add-button-wrap", { ["rule-setting-add-button-wrap-padding"]: value.rule.length > 1 }) }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		size: "large",
		onClick: () => {
			listRef.current.add();
		}
	}, _pisell_utils.locales.getText("pisell2.text.add"))));
};
//#endregion
exports.default = RuleSetting;
