const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useComponents = require("../../../hooks/useComponents.js");
const require_model = require("../model.js");
const require_utils = require("./utils.js");
const require_Schedules = require("./Schedules.js");
const require_serve = require("./serve.js");
const require_useSchedulesDataSource = require("../hooks/useSchedulesDataSource.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_icon_es_ArrowLeft = require("@pisell/icon/es/ArrowLeft");
_pisell_icon_es_ArrowLeft = require_runtime.__toESM(_pisell_icon_es_ArrowLeft);
let _ant_design_icons = require("@ant-design/icons");
//#region src/components/schedules/schedules/index.tsx
const SchedulesConnect = () => {
	const { state, dispatch } = (0, react.useContext)(require_model.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 } = require_useSchedulesDataSource.default({ scheduleIds: isProductSchedule ? currentProduct["schedule.ids"] : scheduleIds });
	const currentSelectDateRef = (0, react.useRef)(date);
	const scheduleAllMapRef = (0, react.useRef)(scheduleAllMap);
	currentSelectDateRef.current = date;
	scheduleAllMapRef.current = scheduleAllMap;
	(0, react.useEffect)(() => {
		const _scheduleMap = require_utils.calcMinTimeMaxTimeBySchedules(dataSource, scheduleAllMapRef.current, currentSelectDateRef.current);
		let newData = {
			list: dataSource,
			scheduleMap: _scheduleMap
		};
		if (!Object.keys(scheduleAllMapRef.current).length && !currentSelectDateRef.current) newData = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, newData), {}, { scheduleAllMap: _scheduleMap });
		dispatch({
			type: "setSchedules",
			payload: newData
		});
	}, [dataSource]);
	const createSchedules = (0, react.useRef)();
	const [ScheduleCreateType] = require_useComponents.default({ components: ["ScheduleCreateType"] });
	const setState = (payload) => {
		dispatch({
			type: "setState",
			payload: require_objectSpread2._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 = _pisell_utils.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 = _pisell_utils.locales.getText("pisell2.text.schedules.delete-schedules-text3");
		else deleteText = _pisell_utils.locales.getText("pisell2.text.schedules.delete-schedules-text1");
		antd.Modal.confirm({
			title: deleteText,
			icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.ExclamationCircleOutlined, null),
			onOk() {
				require_serve.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: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, products), {}, { currentProduct: null })
			}
		});
	};
	const schedulesForDaySlot = (0, react.useMemo)(() => {
		if (!isProductSchedule) return null;
		return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__schedules-by-day-title" }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ArrowLeft.default, {
			className: "pisell-lowcode__schedules-by-day-go-back",
			onClick: handleGoBackProducts
		}), currentProduct.title);
	}, [isProductSchedule, currentProduct]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__schedules-component-schedules-wrap" }, /* @__PURE__ */ react.default.createElement(require_Schedules.default, {
		loading,
		scheduleMap,
		dataSource: list,
		type: currentSelectDayDetail ? "forADay" : "all",
		currentSelectDayDetail,
		detail: schedulesDetail,
		onClose: onCloseSchedulesDetail,
		onDelete,
		onOpenDetail,
		onCalendarDataChange,
		onCloseDay: handleCloseDay,
		schedulesForDaySlot
	}), ScheduleCreateType && /* @__PURE__ */ react.default.createElement(ScheduleCreateType, {
		ref: createSchedules,
		mode: "event",
		onClickType: handleCreateSchedule
	}));
};
//#endregion
exports.default = SchedulesConnect;
