const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_index = require("../../../../pro/compensatedPisellContainer/index.js");
const require_withResponsive = require("../../../../responsive/withResponsive.js");
const require_hooks = require("../../hooks.js");
const require_const = require("./const.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/ecocup/components/SettingModal/index.tsx
const _excluded = ["expire"];
const SettingModal = (props) => {
	const { visible, onCancel, variant } = props;
	const currentVariant = variant || "desktop";
	const isPhoneVariant = currentVariant === "phone";
	const [form] = antd.Form.useForm();
	const { post, get } = require_hooks.useRequest();
	const [loading, setLoading] = (0, react.useState)(false);
	const _onCancel = () => {
		if (loading) return;
		onCancel === null || onCancel === void 0 || onCancel();
		form.resetFields();
	};
	const _onOk = function() {
		var _ref = require_asyncToGenerator._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 = require_asyncToGenerator._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);
		};
	}();
	(0, react.useEffect)(() => {
		getSetting();
	}, []);
	/**
	* @description: 提交设置信息
	* @param {Record<string, any>} values - 表单值
	* @return {Promise<boolean>}
	*/
	const postSetting = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (values) {
			const { expire } = values, membershipValues = require_objectWithoutProperties._objectWithoutProperties(values, _excluded);
			const params = {
				eco_cup_customer_rent_config: require_const.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) {
					(0, _pisell_materials.PisellToast)({ content: _pisell_utils.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);
		};
	}();
	const renderFooter = () => /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-setting-modal-footer" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		size: "large",
		onClick: _onCancel,
		disabled: loading
	}, _pisell_utils.locales.getText("pisell2.text.ecocup.button.cancel")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		size: "large",
		type: "primary",
		onClick: _onOk,
		loading
	}, _pisell_utils.locales.getText("pisell2.text.ecocup.button.confirm")));
	const title = _pisell_utils.locales.getText("pisell2.text.cup-project.footer.button.setting");
	const containerClassName = (0, classnames.default)("pisell2-ecocup-setting-modal", `pisell2-ecocup-setting-modal--${currentVariant}`);
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		renderType: isPhoneVariant ? "drawer" : "modal",
		showHandlerBar: isPhoneVariant,
		onHandlerBarClick: _onCancel,
		props: isPhoneVariant ? {
			open: visible,
			placement: "bottom",
			width: "100%",
			height: "auto",
			style: { maxHeight: "95dvh" },
			title,
			onClose: _onCancel,
			destroyOnClose: true,
			footer: renderFooter(),
			rootClassName: containerClassName,
			className: containerClassName,
			bodyStyle: { padding: "0 24px" }
		} : {
			open: visible,
			title,
			onCancel: _onCancel,
			width: 680,
			centered: true,
			destroyOnClose: true,
			footer: renderFooter(),
			className: containerClassName,
			wrapClassName: containerClassName,
			bodyStyle: {
				padding: "0 24px",
				maxHeight: "min(560px, calc(100dvh - 220px))",
				overflowY: "auto"
			}
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-setting-modal-content" }, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		size: "large"
	}, require_const.formItemsConfig.map((config) => /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemInputNumber, {
		key: config.name,
		renderMode: "edit",
		name: config.name,
		label: _pisell_utils.locales.getText(config.labelKey),
		placeholder: _pisell_utils.locales.getText(config.placeholderKey),
		maxobj: {
			enabled: true,
			max: 9999,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.max-rent-count-max")
		},
		minobj: {
			enabled: true,
			min: 0,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.max-rent-count-min")
		},
		requiredobj: {
			required: true,
			message: _pisell_utils.locales.getText(config.requiredMessageKey)
		}
	})), /* @__PURE__ */ react.default.createElement(_pisell_materials.FormItemInputNumber, {
		renderMode: "edit",
		name: "expire",
		label: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.expire-time"),
		placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.expire-time-placeholder"),
		maxobj: {
			enabled: true,
			max: 365,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.expire-time-max")
		},
		minobj: {
			enabled: true,
			min: 0,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.expire-time-min")
		},
		suffix: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.days"),
		requiredobj: {
			required: true,
			message: _pisell_utils.locales.getText("pisell2.text.ecocup.setting.expire-time-required")
		}
	}))));
};
SettingModal.displayName = "SettingModal";
var SettingModal_default = require_withResponsive.default(SettingModal);
//#endregion
exports.default = SettingModal_default;
