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("../../../booking/components/drawer/index.js");
const require_utils = require("../utils.js");
const require_model = require("../../model.js");
const require_utils$1 = require("../../utils.js");
const require_serve = require("../serve.js");
const require_index$1 = require("./ProductDetail/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/schedules/products/ProductDetailDrawer/index.tsx
const _excluded = ["isAutoChange"];
const ProductDetailDrawer = () => {
	var _products$currentProd;
	const [form] = antd.Form.useForm();
	const [isEdit, setIsEdit] = (0, react.useState)(false);
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const { products, currentSelectDayDetail } = state;
	const onClose = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (checkEdit = true) {
			if (checkEdit && isEdit) {
				if (!(yield require_utils$1.saveConfirm())) return;
			}
			setIsEdit(false);
			form.setFieldsValue({ isAutoChange: false });
			setTimeout(() => {
				form.resetFields();
			}, 10);
			dispatch({
				type: "setCalendar",
				payload: {
					isEdit: false,
					editData: []
				}
			});
			dispatch({
				type: "setProducts",
				payload: {
					openProductDetailDrawer: false,
					currentProduct: null
				}
			});
		});
		return function onClose() {
			return _ref.apply(this, arguments);
		};
	}();
	const updateProductList = (param) => {
		dispatch({
			type: "setProducts",
			payload: { list: require_utils.updateProductById({
				products: state.products.list,
				id: products.currentProduct.id,
				schedule: param.schedules.schedule,
				scheduleMap: state.schedules.scheduleMap
			}) }
		});
	};
	const drawerContent = (0, react.useMemo)(() => {
		if (!products.openProductDetailDrawer || !products.currentProduct) return null;
		return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			form,
			currentProduct: products.currentProduct
		});
	}, [
		form,
		products.openProductDetailDrawer,
		products.currentProduct,
		currentSelectDayDetail
	]);
	const _getProductDetail = (0, ahooks.useRequest)(require_serve.getProductDetail, {
		manual: true,
		onSuccess: ({ data }) => {
			form.setFieldsValue({
				schedule: (data === null || data === void 0 ? void 0 : data.schedule) || [],
				max_participants: (data === null || data === void 0 ? void 0 : data.max_participants) || [],
				isAutoChange: false
			});
		}
	});
	const _editProductDetail = (0, ahooks.useRequest)(require_serve.editProductDetail, {
		manual: true,
		onSuccess: ({ code }, param) => {
			updateProductList(param[0]);
			antd.message.success(_pisell_utils.locales.getText("pisell2.text.schedules.save-success"));
			setIsEdit(false);
			onClose(false);
		}
	});
	const _init = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
			_getProductDetail.run({
				item_id: products.currentProduct.id,
				item_type: "product"
			});
			setIsEdit(false);
		});
		return function _init() {
			return _ref2.apply(this, arguments);
		};
	}();
	(0, react.useEffect)(() => {
		if (products.openProductDetailDrawer) _init();
	}, [products.openProductDetailDrawer]);
	const checkValues = (values) => {
		let res = {
			success: true,
			message: ""
		};
		console.log("values.max_participants", values.max_participants);
		if (values.max_participants.length) {
			if (values.max_participants.some((d) => {
				if (!d.max_participants_type) return true;
				if (d.max_participants_type === "custom-limit" && !d.max_participants_limit) return true;
				return false;
			})) {
				res.success = false;
				res.message = _pisell_utils.locales.getText("pisell2.text.products.max-participants-error");
			}
		}
		return res;
	};
	const onFinish = (values) => {
		let isValid = checkValues(values);
		if (!isValid.success) return antd.message.warning(isValid.message);
		const { isAutoChange } = values, resetValues = require_objectWithoutProperties._objectWithoutProperties(values, _excluded);
		_editProductDetail.run({
			item_id: products.currentProduct.id,
			item_type: "product",
			schedules: resetValues
		});
	};
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		titleNoWrap: true,
		width: 480,
		open: products.openProductDetailDrawer,
		title: ((_products$currentProd = products.currentProduct) === null || _products$currentProd === void 0 ? void 0 : _products$currentProd.title) || "-",
		onClose: () => {
			onClose();
		},
		destroyOnClose: true,
		getContainer: () => document.querySelector(".pisell-lowcode__schedules-component-products") || document.body,
		rootStyle: { position: "absolute" },
		contentWrapperStyle: { boxShadow: "none" },
		footer: /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(antd.Button, {
			type: "primary",
			style: { width: "100%" },
			size: "large",
			disabled: _getProductDetail.loading || !isEdit,
			loading: _editProductDetail.loading,
			onClick: () => {
				form.submit();
			}
		}, _pisell_utils.locales.getText("pisell2.text.schedules.save")))
	}, /* @__PURE__ */ react.default.createElement("div", { className: "product-detail-drawer-body" }, /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
		round: true,
		active: true,
		loading: _getProductDetail.loading
	}, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		onFinish,
		onValuesChange: (a, b) => {
			console.log("wzw", a, b);
			setIsEdit(true);
		}
	}, drawerContent))));
};
//#endregion
exports.default = ProductDetailDrawer;
