import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import Drawer$1 from "../../../booking/components/drawer/index.js";
import useComponents from "../../../../hooks/useComponents.js";
import { saveConfirm } from "../../utils.js";
import { calcCalendarDataBySchedule } from "../utils.js";
import React, { useEffect, useRef, useState } from "react";
import { Button } from "antd";
import { locales } from "@pisell/utils";
import { useMemoizedFn } from "ahooks";
import Delete_wght300_24px from "@pisell/icon/es/Delete_wght300_24px";
import "./index.less";
//#region src/components/schedules/schedules/ScheduleDetailDrawer/index.tsx
/**
* 日程详情抽屉
* @param props
* @constructor
*/
const ScheduleDetailDrawer = (props) => {
	const { onClose, open, getContainer, onDelete, scheduleId, type, onCalendarDataChange } = props;
	const [title, setTitle] = useState("");
	const [isEdit, setEdit] = useState(false);
	const [AllCreateScheduleRefReady, setAllCreateScheduleRefReady] = useState(false);
	const AllCreateScheduleRef = useRef(null);
	const [AllCreateSchedule] = useComponents({ components: ["AllCreateSchedule"] });
	const edit = useMemoizedFn(_asyncToGenerator(function* () {
		var _AllCreateScheduleRef, _AllCreateScheduleRef2;
		yield (_AllCreateScheduleRef = AllCreateScheduleRef.current) === null || _AllCreateScheduleRef === void 0 || (_AllCreateScheduleRef2 = _AllCreateScheduleRef.open) === null || _AllCreateScheduleRef2 === void 0 ? void 0 : _AllCreateScheduleRef2.call(_AllCreateScheduleRef, { id: scheduleId });
		const { formValues, depend } = AllCreateScheduleRef.current.getCurrentSetting();
		handleValueChange({}, formValues, depend);
	}));
	useEffect(() => {
		if (open) if (scheduleId) edit();
		else {
			var _AllCreateScheduleRef3, _AllCreateScheduleRef4;
			(_AllCreateScheduleRef3 = AllCreateScheduleRef.current) === null || _AllCreateScheduleRef3 === void 0 || (_AllCreateScheduleRef4 = _AllCreateScheduleRef3.open) === null || _AllCreateScheduleRef4 === void 0 || _AllCreateScheduleRef4.call(_AllCreateScheduleRef3, { type });
		}
		else setTitle("");
	}, [
		open,
		scheduleId,
		edit,
		type
	]);
	const handleValueChange = useMemoizedFn((changedValues, value, others) => {
		var _value$name, _value$name2;
		if (AllCreateScheduleRefReady) setEdit(true);
		setTitle(((_value$name = value.name) === null || _value$name === void 0 ? void 0 : _value$name[locales.locale]) || ((_value$name2 = value.name) === null || _value$name2 === void 0 ? void 0 : _value$name2["original"]));
		const data = calcCalendarDataBySchedule(value, others, type || others.type);
		onCalendarDataChange === null || onCalendarDataChange === void 0 || onCalendarDataChange(data);
	});
	const resetState = () => {
		setEdit(false);
		setAllCreateScheduleRefReady(false);
	};
	const handleClose = function() {
		var _ref = _asyncToGenerator(function* (data) {
			if (isEdit && !data) {
				if (!(yield saveConfirm())) return;
			}
			resetState();
			onClose === null || onClose === void 0 || onClose(data);
		});
		return function handleClose(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleChangeResData = (data) => {
		handleClose(data);
	};
	return /* @__PURE__ */ React.createElement(Drawer$1, {
		width: "100%",
		placement: "right",
		closable: false,
		titleNoWrap: true,
		onClose: () => {
			handleClose();
		},
		open,
		getContainer: getContainer !== null && getContainer !== void 0 ? getContainer : false,
		title,
		contentWrapperStyle: { boxShadow: "none" },
		destroyOnClose: true
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-detail-wrap" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-detail-content" }, AllCreateSchedule && /* @__PURE__ */ React.createElement(AllCreateSchedule, {
		showMode: "content",
		ref: AllCreateScheduleRef,
		onSettingChange: handleValueChange,
		onChange: handleChangeResData,
		onData: () => {
			setTimeout(() => {
				setAllCreateScheduleRefReady(true);
			}, 500);
		}
	})), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-detail-footer" }, scheduleId && /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode__schedules-detail-footer-delete-wrap",
		onClick: () => {
			var _AllCreateScheduleRef5;
			return onDelete === null || onDelete === void 0 ? void 0 : onDelete((_AllCreateScheduleRef5 = AllCreateScheduleRef.current) === null || _AllCreateScheduleRef5 === void 0 ? void 0 : _AllCreateScheduleRef5.getCurrentDetail());
		}
	}, /* @__PURE__ */ React.createElement(Delete_wght300_24px, { className: "pisell-lowcode__schedules-detail-footer-delete-icon" })), /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		className: "pisell-lowcode__schedules-detail-footer-save",
		onClick: () => {
			var _AllCreateScheduleRef6;
			(_AllCreateScheduleRef6 = AllCreateScheduleRef.current) === null || _AllCreateScheduleRef6 === void 0 || _AllCreateScheduleRef6.submit();
		},
		disabled: !isEdit
	}, locales.getText("pisell2.text.schedules.save")))));
};
//#endregion
export { ScheduleDetailDrawer as default };
