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 Drawer$1 from "../../../booking/components/drawer/index.js";
import { updateProductById } from "../utils.js";
import { Context } from "../../model.js";
import { saveConfirm } from "../../utils.js";
import { editProductDetail, getProductDetail } from "../serve.js";
import ProductDetail from "./ProductDetail/index.js";
import React, { useContext, useEffect, useMemo, useState } from "react";
import { Button, Form, Skeleton, message } from "antd";
import { locales } from "@pisell/utils";
import { useRequest } from "ahooks";
import "./index.less";
//#region src/components/schedules/products/ProductDetailDrawer/index.tsx
const _excluded = ["isAutoChange"];
const ProductDetailDrawer = () => {
	var _products$currentProd;
	const [form] = Form.useForm();
	const [isEdit, setIsEdit] = useState(false);
	const { state, dispatch } = useContext(Context);
	const { products, currentSelectDayDetail } = state;
	const onClose = function() {
		var _ref = _asyncToGenerator(function* (checkEdit = true) {
			if (checkEdit && isEdit) {
				if (!(yield 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: updateProductById({
				products: state.products.list,
				id: products.currentProduct.id,
				schedule: param.schedules.schedule,
				scheduleMap: state.schedules.scheduleMap
			}) }
		});
	};
	const drawerContent = useMemo(() => {
		if (!products.openProductDetailDrawer || !products.currentProduct) return null;
		return /* @__PURE__ */ React.createElement(ProductDetail, {
			form,
			currentProduct: products.currentProduct
		});
	}, [
		form,
		products.openProductDetailDrawer,
		products.currentProduct,
		currentSelectDayDetail
	]);
	const _getProductDetail = useRequest(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 = useRequest(editProductDetail, {
		manual: true,
		onSuccess: ({ code }, param) => {
			updateProductList(param[0]);
			message.success(locales.getText("pisell2.text.schedules.save-success"));
			setIsEdit(false);
			onClose(false);
		}
	});
	const _init = function() {
		var _ref2 = _asyncToGenerator(function* () {
			_getProductDetail.run({
				item_id: products.currentProduct.id,
				item_type: "product"
			});
			setIsEdit(false);
		});
		return function _init() {
			return _ref2.apply(this, arguments);
		};
	}();
	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 = locales.getText("pisell2.text.products.max-participants-error");
			}
		}
		return res;
	};
	const onFinish = (values) => {
		let isValid = checkValues(values);
		if (!isValid.success) return message.warning(isValid.message);
		const { isAutoChange } = values, resetValues = _objectWithoutProperties(values, _excluded);
		_editProductDetail.run({
			item_id: products.currentProduct.id,
			item_type: "product",
			schedules: resetValues
		});
	};
	return /* @__PURE__ */ React.createElement(Drawer$1, {
		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.createElement("div", null, /* @__PURE__ */ React.createElement(Button, {
			type: "primary",
			style: { width: "100%" },
			size: "large",
			disabled: _getProductDetail.loading || !isEdit,
			loading: _editProductDetail.loading,
			onClick: () => {
				form.submit();
			}
		}, locales.getText("pisell2.text.schedules.save")))
	}, /* @__PURE__ */ React.createElement("div", { className: "product-detail-drawer-body" }, /* @__PURE__ */ React.createElement(Skeleton, {
		round: true,
		active: true,
		loading: _getProductDetail.loading
	}, /* @__PURE__ */ React.createElement(Form, {
		form,
		onFinish,
		onValuesChange: (a, b) => {
			console.log("wzw", a, b);
			setIsEdit(true);
		}
	}, drawerContent))));
};
//#endregion
export { ProductDetailDrawer as default };
