const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../info2/service/addService/utils.js");
const require_utils$1 = require("../components/footer/utils.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../utils/index.js");
const require_index$1 = require("../info2/service/editService/ResourcesV2/index.js");
const require_serve = require("./serve.js");
const require_locales = require("./locales.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
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");
require("./index.less");
//#region src/components/booking/editBookingModal/index.tsx
const EditBookingModal = (props, ref) => {
	const [open, setOpen] = (0, react.useState)(false);
	const [params, setParams] = (0, react.useState)({});
	const [loading, setLoading] = (0, react.useState)(false);
	const [updating, setUpdating] = (0, react.useState)(false);
	const [data, setData] = (0, react.useState)(null);
	const context = require_useEngineContext.default();
	const [form] = antd.Form.useForm();
	const useToDate = antd.Form.useWatch("toDate", form);
	const useResource = antd.Form.useWatch("resource", form);
	(0, react.useImperativeHandle)(ref, () => {
		return { open: (params) => {
			console.log("change", params);
			form.resetFields();
			setParams(params);
			setOpen(true);
		} };
	});
	const getInitData = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (params) {
			var _data$fromResource, _data$toResource;
			let mockProps = params;
			const _data = yield require_serve.init(mockProps);
			setData(_data);
			let _initResourceVal = _data.childrenBooking._extend.resource;
			const fromResourceFormId = (_data$fromResource = _data.fromResource) === null || _data$fromResource === void 0 ? void 0 : _data$fromResource.form_id;
			const _form_id = ((_data$toResource = _data.toResource) === null || _data$toResource === void 0 ? void 0 : _data$toResource.form_id) || fromResourceFormId;
			if (params.fromResourceId !== params.toResourceId && !!_form_id) {
				var _data$toResource2;
				const { list, maps } = require_utils.formatResources({
					cacheItem: _data.childrenBooking,
					resourcesOrigin: _data.resourcesOrigin
				});
				const currentForm = maps[_form_id];
				const relation_id = (_data$toResource2 = _data.toResource) === null || _data$toResource2 === void 0 ? void 0 : _data$toResource2.id;
				if (currentForm && (relation_id || relation_id === 0)) {
					var _currentForm$renderLi, _currentForm$renderLi2, _currentForm$renderLi3, _currentForm$renderLi4;
					let _targetResource = {
						relation_type: ((_currentForm$renderLi = currentForm.renderListMaps[relation_id]) === null || _currentForm$renderLi === void 0 || (_currentForm$renderLi = _currentForm$renderLi.usable) === null || _currentForm$renderLi === void 0 ? void 0 : _currentForm$renderLi.relation_type) || "form",
						form_id: _form_id,
						relation_id,
						like_status: "common",
						children: (_currentForm$renderLi2 = currentForm.renderListMaps) === null || _currentForm$renderLi2 === void 0 || (_currentForm$renderLi2 = _currentForm$renderLi2[relation_id]) === null || _currentForm$renderLi2 === void 0 ? void 0 : _currentForm$renderLi2.children,
						metadata: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_currentForm$renderLi3 = currentForm.renderListMaps) === null || _currentForm$renderLi3 === void 0 || (_currentForm$renderLi3 = _currentForm$renderLi3[relation_id]) === null || _currentForm$renderLi3 === void 0 ? void 0 : _currentForm$renderLi3.metadata), {}, {
							form_name: currentForm.title,
							resource_name: (_currentForm$renderLi4 = currentForm.renderListMaps[relation_id]) === null || _currentForm$renderLi4 === void 0 ? void 0 : _currentForm$renderLi4.main_field
						})
					};
					if (Array.isArray(_initResourceVal[_form_id])) {
						const _cloned = [..._initResourceVal[_form_id]];
						const _idx = _cloned.findIndex((rs) => {
							var _rs$children;
							return rs.relation_id == params.fromResourceId || ((_rs$children = rs.children) === null || _rs$children === void 0 ? void 0 : _rs$children.find((child) => child.relation_id == params.fromResourceId));
						});
						if (_idx !== -1) if (_cloned.find((_clonedItem) => _clonedItem.relation_id === (_targetResource === null || _targetResource === void 0 ? void 0 : _targetResource.relation_id))) _cloned.splice(_idx, 1);
						else _cloned[_idx] = _targetResource;
						else if (_cloned.findIndex((rs) => {
							return rs.relation_id == (_targetResource === null || _targetResource === void 0 ? void 0 : _targetResource.relation_id);
						}) === -1) _cloned.push(_targetResource);
						_initResourceVal = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _initResourceVal), {}, { [_form_id]: _cloned });
					} else _initResourceVal = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _initResourceVal), {}, { [_form_id]: [_targetResource] });
				}
			}
			setTimeout(() => {
				form.setFieldsValue({
					resource: _initResourceVal,
					fromDate: [(0, dayjs.default)(`${mockProps.event.start_date} ${mockProps.event.start_time}`), (0, dayjs.default)(`${mockProps.event.end_date} ${mockProps.event.end_time}`)],
					toDate: [(0, dayjs.default)(mockProps.toStartDate), (0, dayjs.default)(mockProps.toEndDate)]
				});
			}, 100);
		});
		return function getInitData(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	(0, react.useLayoutEffect)(() => {
		var _context$appHelper$ut;
		_pisell_utils.locales.init(require_locales.default, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get("umi_locale")) || "en");
		require_index.request.setRequest(context.appHelper.utils.request);
	}, []);
	(0, react.useEffect)(() => {
		if (!open) return;
		getInitData(params);
	}, [params, open]);
	const resourceData = (0, react.useMemo)(() => {
		if (!data) return;
		return require_utils.formatResources({
			cacheItem: data.childrenBooking,
			resourcesOrigin: data.resourcesOrigin
		});
	}, [data]);
	const updateResource = (resource) => {
		form.setFieldValue("resource", resource);
	};
	const originCacheItem = (0, react.useMemo)(() => {
		if (!data) return null;
		const _cacheItem = require_serve.createCacheItem(data.childrenBooking, {
			toDate: [data.childrenBooking._extend.start_date, data.childrenBooking._extend.end_date],
			resource: data.childrenBooking._extend.resource
		});
		console.log("_origin_cacheItem", _cacheItem);
		const { list, maps } = require_utils.formatResources({
			cacheItem: _cacheItem,
			resourcesOrigin: data.resourcesOrigin
		});
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _cacheItem), {}, { _data: {
			resources: list,
			resourceMaps: maps
		} });
	}, [data]);
	const cacheItem = (0, react.useMemo)(() => {
		if (!data) return null;
		const _cacheItem = require_serve.createCacheItem(data.childrenBooking, {
			toDate: useToDate,
			resource: useResource
		});
		console.log("_cacheItem", _cacheItem);
		const { list, maps } = require_utils.formatResources({
			cacheItem: _cacheItem,
			resourcesOrigin: data.resourcesOrigin
		});
		updateResource(require_serve.updateResourceErrors(maps, _cacheItem));
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _cacheItem), {}, { _data: {
			resources: list,
			resourceMaps: maps
		} });
	}, [
		data,
		useToDate,
		useResource
	]);
	const initState = () => {
		setData(null);
		setOpen(false);
		form.resetFields();
	};
	const onCancel = () => {
		initState();
		props === null || props === void 0 || props.onCancel();
	};
	const onOk = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
			const values = form.getFieldsValue();
			setUpdating(true);
			try {
				const { _extend, order_id } = data.childrenBooking;
				const start_date = values.toDate[0].format("YYYY-MM-DD");
				const start_time = values.toDate[0].format("HH:mm");
				const end_date = values.toDate[1].format("YYYY-MM-DD");
				const end_time = values.toDate[1].format("HH:mm");
				console.log("extend data", _extend);
				const { id, relation_id, like_status, relation_type, is_all, schedule_id, sub_type, select_date, number, metadata } = _extend;
				yield require_serve.updateChildBooking(order_id, {
					id,
					relation_id,
					like_status,
					relation_type,
					is_all,
					schedule_id,
					sub_type,
					select_date,
					number,
					metadata,
					resources: Object.values(values.resource).reduce((pre, cur) => {
						const newList = require_utils$1.checkResourcesFormat(cur, cacheItem);
						pre = pre.concat(newList);
						return pre;
					}, []),
					start_date,
					start_time,
					end_date,
					end_time,
					duration: values.toDate[1].diff(values.toDate[0], "minutes")
				});
				initState();
				props === null || props === void 0 || props.onOk();
			} catch (error) {
				console.error(error);
			}
			setUpdating(false);
		});
		return function onOk() {
			return _ref2.apply(this, arguments);
		};
	}();
	console.log("data", data);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellContainer, {
		renderType: "modal",
		props: {
			title: _pisell_utils.locales.getText("pisell2.text.booking.confirm-changes"),
			open,
			cancelVisible: false,
			bodyStyle: { padding: 24 },
			width: 680,
			onCancel,
			okButtonProps: {},
			onOk: () => {},
			footer: null,
			className: "pisell-edit-booking-modal"
		}
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Spin, { spinning: !data }, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical"
	}, /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		alignItems: "center",
		gap: 8,
		color: "#1b1b1b",
		fontWeight: "500"
	} }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
		type: "pisell2-copy-03",
		style: { fontSize: 16 }
	}), _pisell_utils.locales.getText("pisell2.text.booking.time-change")), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "fromDate",
		label: "From"
	}, /* @__PURE__ */ react.default.createElement(antd.DatePicker.RangePicker, {
		format: "YYYY-MM-DD HH:mm",
		showTime: true,
		presets: [],
		disabled: true,
		style: { width: "100%" },
		size: "large"
	})), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "toDate",
		label: "To"
	}, /* @__PURE__ */ react.default.createElement(antd.DatePicker.RangePicker, {
		format: "YYYY-MM-DD HH:mm",
		showTime: true,
		presets: [],
		style: { width: "100%" },
		size: "large"
	})), /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		alignItems: "center",
		gap: 8,
		color: "#1b1b1b",
		fontWeight: "500"
	} }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
		type: "pisell2-mark",
		style: { fontSize: 16 }
	}), _pisell_utils.locales.getText("pisell2.text.booking.resource-change.detail")), data ? /* @__PURE__ */ react.default.createElement("div", { style: { marginTop: 16 } }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, { name: "resource" }, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		state: {},
		resourceData,
		cacheItem,
		getCurrentCacheItem: () => {
			return cacheItem;
		},
		config: {
			autoAllocation: false,
			labelCol: { span: 24 },
			resourceErrorIcon: true,
			disabled: false
		},
		originCacheItem,
		originValue: originCacheItem._extend.resource
	}))) : null)), /* @__PURE__ */ react.default.createElement("div", {
		className: "",
		style: {
			display: "flex",
			gap: 12
		}
	}, /* @__PURE__ */ react.default.createElement(antd.Button, {
		onClick: onCancel,
		block: true,
		size: "large",
		style: { borderRadius: 20 }
	}, _pisell_utils.locales.getText("pisell2.text.cancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		onClick: onOk,
		block: true,
		size: "large",
		disabled: !data,
		loading: updating,
		style: { borderRadius: 20 }
	}, _pisell_utils.locales.getText("pisell2.text.confirm"))));
};
var editBookingModal_default = (0, react.forwardRef)(EditBookingModal);
//#endregion
exports.default = editBookingModal_default;
