import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useComponents from "../../../hooks/useComponents.js";
import { Context } from "../model.js";
import { calcMinTimeMaxTimeBySchedules } from "./utils.js";
import Schedules from "./Schedules.js";
import { deleteSchedule } from "./serve.js";
import useSchedulesDataSource from "../hooks/useSchedulesDataSource.js";
import React, { useContext, useEffect, useMemo, useRef } from "react";
import { Modal } from "antd";
import { locales } from "@pisell/utils";
import ArrowLeft from "@pisell/icon/es/ArrowLeft";
import { ExclamationCircleOutlined } from "@ant-design/icons";
//#region src/components/schedules/schedules/index.tsx
const SchedulesConnect = () => {
	const { state, dispatch } = useContext(Context);
	const { schedulesDetail, currentSelectDayDetail, products, schedules } = state;
	const { scheduleAllMap, scheduleMap, list } = schedules || {};
	const { date, scheduleIds } = currentSelectDayDetail || {};
	const { currentProduct } = products || {};
	const isProductSchedule = currentProduct && currentSelectDayDetail;
	const { dataSource, loading, refresh } = useSchedulesDataSource({ scheduleIds: isProductSchedule ? currentProduct["schedule.ids"] : scheduleIds });
	const currentSelectDateRef = useRef(date);
	const scheduleAllMapRef = useRef(scheduleAllMap);
	currentSelectDateRef.current = date;
	scheduleAllMapRef.current = scheduleAllMap;
	useEffect(() => {
		const _scheduleMap = calcMinTimeMaxTimeBySchedules(dataSource, scheduleAllMapRef.current, currentSelectDateRef.current);
		let newData = {
			list: dataSource,
			scheduleMap: _scheduleMap
		};
		if (!Object.keys(scheduleAllMapRef.current).length && !currentSelectDateRef.current) newData = _objectSpread2(_objectSpread2({}, newData), {}, { scheduleAllMap: _scheduleMap });
		dispatch({
			type: "setSchedules",
			payload: newData
		});
	}, [dataSource]);
	const createSchedules = useRef();
	const [ScheduleCreateType] = useComponents({ components: ["ScheduleCreateType"] });
	const setState = (payload) => {
		dispatch({
			type: "setState",
			payload: _objectSpread2({}, payload)
		});
	};
	const onOpenDetail = (id) => {
		if (!id) {
			var _createSchedules$curr, _createSchedules$curr2;
			return (_createSchedules$curr = createSchedules.current) === null || _createSchedules$curr === void 0 || (_createSchedules$curr2 = _createSchedules$curr.open) === null || _createSchedules$curr2 === void 0 ? void 0 : _createSchedules$curr2.call(_createSchedules$curr);
		}
		setState({ schedulesDetail: {
			show: true,
			id
		} });
	};
	const onCloseSchedulesDetail = (data) => {
		if (data) {
			var _schedulesDetail$call;
			refresh();
			schedulesDetail === null || schedulesDetail === void 0 || (_schedulesDetail$call = schedulesDetail.callback) === null || _schedulesDetail$call === void 0 || _schedulesDetail$call.call(schedulesDetail, data);
		}
		setState({
			tabKey: (schedulesDetail === null || schedulesDetail === void 0 ? void 0 : schedulesDetail.fromTabKey) || state.tabKey,
			schedulesDetail: {
				show: false,
				id: void 0
			},
			calendarData: []
		});
		dispatch({
			type: "setCalendar",
			payload: {
				editData: [],
				isEdit: false
			}
		});
	};
	const handleCreateSchedule = (type) => {
		setState({ schedulesDetail: {
			show: true,
			id: void 0,
			type
		} });
	};
	const onCalendarDataChange = (data) => {
		dispatch({
			type: "setCalendar",
			payload: {
				editData: data,
				isEdit: true
			}
		});
	};
	const onDelete = (detail) => {
		let deleteText = "";
		if ((detail === null || detail === void 0 ? void 0 : detail.relation_count) > 0 && (detail === null || detail === void 0 ? void 0 : detail.order_count) === 0) deleteText = locales.getText("pisell2.text.schedules.delete-schedules-text2");
		else if ((detail === null || detail === void 0 ? void 0 : detail.relation_count) > 0 && (detail === null || detail === void 0 ? void 0 : detail.order_count) > 0) deleteText = locales.getText("pisell2.text.schedules.delete-schedules-text3");
		else deleteText = locales.getText("pisell2.text.schedules.delete-schedules-text1");
		Modal.confirm({
			title: deleteText,
			icon: /* @__PURE__ */ React.createElement(ExclamationCircleOutlined, null),
			onOk() {
				deleteSchedule({ id: detail.id }).then(() => {
					onCloseSchedulesDetail({
						id: detail.id,
						isRemove: true
					});
				});
			}
		});
	};
	const handleCloseDay = () => {
		dispatch({
			type: "setState",
			payload: { currentSelectDayDetail: null }
		});
	};
	const handleGoBackProducts = () => {
		dispatch({
			type: "setState",
			payload: {
				tabKey: "products",
				products: _objectSpread2(_objectSpread2({}, products), {}, { currentProduct: null })
			}
		});
	};
	const schedulesForDaySlot = useMemo(() => {
		if (!isProductSchedule) return null;
		return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-by-day-title" }, /* @__PURE__ */ React.createElement(ArrowLeft, {
			className: "pisell-lowcode__schedules-by-day-go-back",
			onClick: handleGoBackProducts
		}), currentProduct.title);
	}, [isProductSchedule, currentProduct]);
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-schedules-wrap" }, /* @__PURE__ */ React.createElement(Schedules, {
		loading,
		scheduleMap,
		dataSource: list,
		type: currentSelectDayDetail ? "forADay" : "all",
		currentSelectDayDetail,
		detail: schedulesDetail,
		onClose: onCloseSchedulesDetail,
		onDelete,
		onOpenDetail,
		onCalendarDataChange,
		onCloseDay: handleCloseDay,
		schedulesForDaySlot
	}), ScheduleCreateType && /* @__PURE__ */ React.createElement(ScheduleCreateType, {
		ref: createSchedules,
		mode: "event",
		onClickType: handleCreateSchedule
	}));
};
//#endregion
export { SchedulesConnect as default };
