import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { useRequest } from "../../hooks.js";
import { formItemsConfig } from "./const.js";
import React, { useEffect, useState } from "react";
import { Form } from "antd";
import { locales } from "@pisell/utils";
import { FormItemInputNumber, PisellModal, PisellToast } from "@pisell/materials";
import "./index.less";
//#region src/components/ecocup/components/SettingModal/index.tsx
const _excluded = ["expire"];
const SettingModal = (props) => {
	const { visible, onCancel } = props;
	const [form] = Form.useForm();
	const { post, get } = useRequest();
	const [loading, setLoading] = useState(false);
	const _onCancel = () => {
		if (loading) return;
		onCancel === null || onCancel === void 0 || onCancel();
		form.resetFields();
	};
	const _onOk = function() {
		var _ref = _asyncToGenerator(function* () {
			if (loading) return;
			try {
				if (!(yield postSetting(yield form.validateFields()))) return;
				_onCancel();
			} catch (error) {
				console.error(error);
			}
		});
		return function _onOk() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @description: 获取设置信息
	* @return {*}
	*/
	const getSetting = function() {
		var _ref2 = _asyncToGenerator(function* () {
			const res = yield get("/shop/metadata/many", { keys: ["eco_cup_customer_rent_config", "eco_cup_rent_expire_config"] });
			if ((res === null || res === void 0 ? void 0 : res.code) == 200) {
				const { data } = res;
				const expireConfig = (data === null || data === void 0 ? void 0 : data.eco_cup_rent_expire_config) || {};
				const rentConfig = (data === null || data === void 0 ? void 0 : data.eco_cup_customer_rent_config) || [];
				const formValues = { expire: (expireConfig === null || expireConfig === void 0 ? void 0 : expireConfig.expire) || 30 };
				rentConfig.forEach((item) => {
					if (item === null || item === void 0 ? void 0 : item.name) formValues[item.name] = Number(item === null || item === void 0 ? void 0 : item.max_rent_count) || void 0;
				});
				form.setFieldsValue(formValues);
			}
		});
		return function getSetting() {
			return _ref2.apply(this, arguments);
		};
	}();
	useEffect(() => {
		getSetting();
	}, []);
	/**
	* @description: 提交设置信息
	* @param {Record<string, any>} values - 表单值
	* @return {Promise<boolean>}
	*/
	const postSetting = function() {
		var _ref3 = _asyncToGenerator(function* (values) {
			const { expire } = values, membershipValues = _objectWithoutProperties(values, _excluded);
			const params = {
				eco_cup_customer_rent_config: formItemsConfig.map((config) => ({
					name: config.name,
					max_rent_count: membershipValues[config.name]
				})),
				eco_cup_rent_expire_config: {
					expire,
					unit: "day"
				}
			};
			try {
				setLoading(true);
				const res = yield post("/shop/metadata/many", params);
				if ((res === null || res === void 0 ? void 0 : res.code) == 200) {
					PisellToast({ content: locales.getText("pisell2.text.ecocup.setting.success") });
					return true;
				}
				return false;
			} catch (error) {
				console.error("提交设置失败:", error);
				return false;
			} finally {
				setLoading(false);
			}
		});
		return function postSetting(_x) {
			return _ref3.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(PisellModal, {
		open: visible,
		headerDivider: true,
		title: locales.getText("pisell2.text.cup-project.footer.button.setting"),
		onCancel: _onCancel,
		onOk: _onOk,
		okText: locales.getText("pisell2.text.ecocup.button.confirm"),
		cancelText: locales.getText("pisell2.text.ecocup.button.cancel"),
		width: 680,
		centered: true,
		destroyOnClose: true,
		wrapClassName: "pisell2-ecocup-add-customer-modal",
		okButtonProps: { loading }
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-add-customer-modal-content" }, /* @__PURE__ */ React.createElement(Form, {
		form,
		size: "large"
	}, formItemsConfig.map((config) => /* @__PURE__ */ React.createElement(FormItemInputNumber, {
		key: config.name,
		renderMode: "edit",
		name: config.name,
		label: locales.getText(config.labelKey),
		placeholder: locales.getText(config.placeholderKey),
		maxobj: {
			enabled: true,
			max: 9999,
			message: locales.getText("pisell2.text.ecocup.setting.max-rent-count-max")
		},
		minobj: {
			enabled: true,
			min: 0,
			message: locales.getText("pisell2.text.ecocup.setting.max-rent-count-min")
		},
		requiredobj: {
			required: true,
			message: locales.getText(config.requiredMessageKey)
		}
	})), /* @__PURE__ */ React.createElement(FormItemInputNumber, {
		renderMode: "edit",
		name: "expire",
		label: locales.getText("pisell2.text.ecocup.setting.expire-time"),
		placeholder: locales.getText("pisell2.text.ecocup.setting.expire-time-placeholder"),
		maxobj: {
			enabled: true,
			max: 365,
			message: locales.getText("pisell2.text.ecocup.setting.expire-time-max")
		},
		minobj: {
			enabled: true,
			min: 0,
			message: locales.getText("pisell2.text.ecocup.setting.expire-time-min")
		},
		suffix: locales.getText("pisell2.text.ecocup.setting.days"),
		requiredobj: {
			required: true,
			message: locales.getText("pisell2.text.ecocup.setting.expire-time-required")
		}
	}))));
};
//#endregion
export { SettingModal as default };
