import { getLocale } from "../../utils/locales.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { request } from "../../utils/index.js";
import listComponent_default from "../listComponent/index.js";
import RuleBorder from "./ruleBorder/index.js";
import { getOrderTypes, getPaymentTypes, getProductChannel, getStaffList } from "./server.js";
import locales_default from "./locales.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { Button, Select, Skeleton } from "antd";
import { getUniqueId, locales } from "@pisell/utils";
import classNames from "classnames";
import { CloseOutlined, PlusOutlined } from "@ant-design/icons";
import "./index.less";
//#region src/components/ruleSetting/index.tsx
const createRuleItem = () => {
	return {
		operator: "contain",
		class: "type",
		value: ["all"],
		_id: getUniqueId()
	};
};
locales.init(locales_default, getLocale());
const RuleSetting = (props) => {
	var _context$appHelper;
	const { value = {
		rule: [],
		type: "and"
	}, onChange, className, style } = props;
	console.log("valuevalue", value);
	const context = useEngineContext();
	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) request.setRequest(context.appHelper.utils.request);
	const listRef = useRef(null);
	const [loading, setLoading] = useState(false);
	const [orderTypes, setOrderTypes] = useState([]);
	const [salesChannels, setSalesChannels] = useState([]);
	const [staffs, setStaffs] = useState([]);
	const [paymentMethods, setPaymentMethod] = useState([]);
	const init = function() {
		var _ref = _asyncToGenerator(function* () {
			let _getStaffList = getStaffList();
			const _getProductChannel = getProductChannel();
			const _getOrderTypes = getOrderTypes();
			const _getPaymentTypes = 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);
		};
	}();
	useEffect(() => {
		document.body.id = "body";
		init();
	}, []);
	const config = useMemo(() => {
		return {
			type: {
				key: "type",
				style: { width: 200 },
				size: "large",
				options: [
					{
						label: locales.getText("pisell2.text.rule-setting.order_type"),
						value: "type",
						list: orderTypes
					},
					{
						label: locales.getText("pisell2.text.rule-setting.sales_channel"),
						value: "platform",
						list: salesChannels
					},
					{
						label: locales.getText("pisell2.text.rule-setting.staff"),
						value: "staff",
						list: staffs
					},
					{
						label: locales.getText("pisell2.text.rule-setting.payment_method"),
						value: "payment_method",
						list: paymentMethods
					},
					{
						label: locales.getText("pisell2.text.rule-setting.pisell_terminal_device"),
						value: "pos",
						list: [{
							label: locales.getText("pisell2.text.rule-setting.current_device"),
							value: "current"
						}]
					}
				]
			},
			rule: {
				key: "rule",
				style: { width: 100 },
				size: "large",
				options: [{
					label: locales.getText("pisell2.text.rule-setting.Contain"),
					value: "contain"
				}, {
					label: 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 = _objectSpread2({}, item);
		_item.rule.push(createRuleItem());
		return _item;
	};
	const updateRuleItem = (item, cId, val) => {
		return _objectSpread2(_objectSpread2({}, item), {}, { rule: item.rule.map((d) => {
			if (d._id === cId) return _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.createElement(Skeleton, {
		round: true,
		active: true,
		loading,
		className,
		style
	}, /* @__PURE__ */ React.createElement("div", { className: "rule-setting-wrap" }, value.rule.length > 1 ? /* @__PURE__ */ React.createElement(RuleBorder, {
		value: value.type,
		onChange: (e) => {
			onChange(_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.createElement(listComponent_default, {
		ref: listRef,
		value: value.rule,
		onChange: (rule) => {
			onChange(_objectSpread2(_objectSpread2({}, value), {}, { rule }));
		},
		rowKey: "_id",
		wrapClassName: "rule-setting-list",
		createItem: (item) => {
			return _objectSpread2(_objectSpread2({}, item), {}, {
				type: "and",
				rule: [{
					_id: "-1",
					class: "type",
					operator: "contain",
					value: ["all"]
				}]
			});
		},
		renderItem: (item, index, remove, update) => {
			return /* @__PURE__ */ React.createElement("div", {
				className: classNames("rule-setting-item", `rule-setting-item-${item._id}`),
				key: item._id
			}, /* @__PURE__ */ React.createElement("div", { className: "rule-setting-item-header" }, /* @__PURE__ */ React.createElement("div", { className: "rule-setting-item-title" }, locales.getText("pisell2.text.satisfy_any_condition")), /* @__PURE__ */ React.createElement("div", { className: "rule-setting-item-btns" }, /* @__PURE__ */ React.createElement(PlusOutlined, {
				className: "rule-setting-item-btn",
				onClick: () => {
					update(item, handleAddItem(item));
				}
			}), value.rule.length > 1 ? /* @__PURE__ */ React.createElement(CloseOutlined, {
				className: "rule-setting-item-btn",
				onClick: () => {
					remove(item);
				}
			}) : null)), /* @__PURE__ */ React.createElement("div", { className: "rule-setting-item-body" }, item.rule.length > 1 ? /* @__PURE__ */ React.createElement(RuleBorder, {
				value: item.type,
				onChange: (e) => {
					update(item, _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.createElement("div", { className: "rule-setting-item-body-list" }, item.rule.map((rule) => {
				var _config$type$options$;
				return /* @__PURE__ */ React.createElement("div", {
					className: classNames("rule-setting-item-body-item", `rule-setting-item-body-item-${rule._id}`),
					key: rule._id
				}, /* @__PURE__ */ React.createElement(Select, _objectSpread2(_objectSpread2({}, config.type), {}, {
					value: rule.class,
					onChange: (e) => {
						update(item, updateRuleItem(item, rule._id, {
							class: e,
							value: ["all"]
						}));
					}
				})), /* @__PURE__ */ React.createElement(Select, _objectSpread2(_objectSpread2({}, config.rule), {}, {
					value: rule.operator,
					onChange: (e) => {
						update(item, updateRuleItem(item, rule._id, { operator: e }));
					}
				})), /* @__PURE__ */ React.createElement(Select, _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.createElement(CloseOutlined, { onClick: () => {
					update(item, _objectSpread2(_objectSpread2({}, item), {}, { rule: item.rule.filter((d) => d._id !== rule._id) }));
				} }) : null);
			}))));
		},
		renderAddButton: () => null
	})), /* @__PURE__ */ React.createElement("div", { className: classNames("rule-setting-add-button-wrap", { ["rule-setting-add-button-wrap-padding"]: value.rule.length > 1 }) }, /* @__PURE__ */ React.createElement(Button, {
		size: "large",
		onClick: () => {
			listRef.current.add();
		}
	}, locales.getText("pisell2.text.add"))));
};
//#endregion
export { RuleSetting as default };
