import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import Drawer from "../../../booking/components/drawer/index.js";
import { Context } from "../../model.js";
import { saveConfirm } from "../../utils.js";
import { getResourceRelation } from "../serve.js";
import detailContent_default from "./detailContent.js";
import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState } from "react";
import { useRequest } from "ahooks";
import "./index.less";
//#region src/components/schedules/resources/detail/index.tsx
const ResourcesDetail = (props, refs) => {
	const { onSuccess } = props;
	const { state, dispatch } = useContext(Context);
	const { resources } = state;
	const cacheResource = resources === null || resources === void 0 ? void 0 : resources.cacheResource;
	const tabKey = state === null || state === void 0 ? void 0 : state.tabKey;
	const detailContentRef = useRef(null);
	const [open, setOpen] = useState(false);
	const [currentResource, setCurrentResource] = useState({});
	const [detail, setDetail] = useState({});
	const onClose = function() {
		var _ref = _asyncToGenerator(function* (isSuccess) {
			if (isSuccess) onSuccess === null || onSuccess === void 0 || onSuccess(isSuccess);
			if (!isSuccess && detailContentRef.current.getIsEdit()) {
				if (!(yield saveConfirm())) return;
			}
			setOpen(false);
			if (cacheResource) dispatch({
				type: "setState",
				payload: {
					tabKey: "products",
					resources: _objectSpread2(_objectSpread2({}, state.resources), {}, { cacheResource: null })
				}
			});
		});
		return function onClose(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useEffect(() => {
		if (tabKey !== "resources" && tabKey !== "openHours" || tabKey === "resources" && !open) dispatch({
			type: "setCalendar",
			payload: { isEdit: false }
		});
	}, [tabKey, open]);
	const onOpen = () => {
		setOpen(true);
	};
	const _getResourceRelation = useRequest(getResourceRelation, {
		manual: true,
		onSuccess: (res) => {
			var _detailContentRef$cur, _detailContentRef$cur2;
			const data = (res === null || res === void 0 ? void 0 : res.data) || {};
			setDetail(data);
			detailContentRef === null || detailContentRef === void 0 || (_detailContentRef$cur = detailContentRef.current) === null || _detailContentRef$cur === void 0 || (_detailContentRef$cur2 = _detailContentRef$cur.setFieldsValue) === null || _detailContentRef$cur2 === void 0 || _detailContentRef$cur2.call(_detailContentRef$cur, data);
		}
	});
	useImperativeHandle(refs, () => {
		return { open: (item) => {
			onOpen();
			setCurrentResource(item);
			_getResourceRelation.run({
				item_id: item.id,
				item_type: "form"
			});
		} };
	});
	return /* @__PURE__ */ React.createElement(Drawer, {
		width: "100%",
		placement: "right",
		open,
		getContainer: document.querySelector(".pisell-lowcode__schedules-component-resources") || document.body,
		titleNoWrap: true,
		headerStyle: { display: "none" },
		onClose: () => {
			onClose(false);
		},
		bodyStyle: { padding: 0 },
		title: currentResource === null || currentResource === void 0 ? void 0 : currentResource.title,
		rootStyle: { position: "absolute" },
		contentWrapperStyle: { boxShadow: "none" },
		destroyOnClose: true
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-resources-detail" }, /* @__PURE__ */ React.createElement(detailContent_default, {
		tabKey,
		isAutoSetCalendarData: tabKey === "resources" && open,
		ref: detailContentRef,
		loading: _getResourceRelation.loading,
		onClose,
		drawerContainer: document.querySelector(".pisell-lowcode__schedules-component-resources")
	})));
};
var detail_default = forwardRef(ResourcesDetail);
//#endregion
export { detail_default as default };
