import useEngineContext from "../../../../../../hooks/useEngineContext.js";
import { Context } from "../../../../model.js";
import { request } from "../../../../utils.js";
import { calcCalendarDataBySchedules } from "../../../../schedules/utils.js";
import ScheduleList from "../../../../components/ScheduleList/index.js";
import { getScheduleListByProductId } from "../../../serve.js";
import AddSchedules from "./AddSchedules/index.js";
import React, { useContext, useEffect, useRef } from "react";
import { locales } from "@pisell/utils";
import { useMemoizedFn, useRequest } from "ahooks";
import "./index.less";
//#region src/components/schedules/products/ProductDetailDrawer/ProductDetail/SchedulesModule/index.tsx
const SchedulesModule = ({ value, onChange, drawerContainer, ruleFn = () => true, tabKey, previewItemChange, isSelf = false, isTitle = true }) => {
	const context = useEngineContext();
	if (isSelf) request.setRequest(context.appHelper.utils.request);
	const { state, dispatch } = useContext(Context);
	const first = useRef(true);
	const _getScheduleList = useRequest(getScheduleListByProductId, {
		manual: true,
		onSuccess: (res) => {}
	});
	const handleDeleteSchedule = (item) => {
		onChange === null || onChange === void 0 || onChange((value || []).filter((id) => id !== item.id));
		_getScheduleList.mutate((data) => data === null || data === void 0 ? void 0 : data.filter((d) => d.id !== item.id));
	};
	const _onAddChange = (arr) => {
		onChange === null || onChange === void 0 || onChange(arr.map((d) => d.id));
		_getScheduleList.mutate(arr);
	};
	useEffect(() => {
		if (value && value.length && first.current === true) {
			_getScheduleList.run({
				ids: value || [],
				num: 999,
				skip: 1
			});
			first.current = false;
		}
	}, [value]);
	const setCalendarEditData = (editData) => {
		dispatch === null || dispatch === void 0 || dispatch({
			type: "setCalendar",
			payload: {
				isEdit: true,
				editData
			}
		});
	};
	useEffect(() => {
		if (ruleFn === null || ruleFn === void 0 ? void 0 : ruleFn()) setCalendarEditData(calcCalendarDataBySchedules(_getScheduleList.data || []));
	}, [_getScheduleList.data, ruleFn === null || ruleFn === void 0 ? void 0 : ruleFn()]);
	const onClickItem = useMemoizedFn((item) => {
		if (previewItemChange) return previewItemChange(item);
		dispatch === null || dispatch === void 0 || dispatch({
			type: "setState",
			payload: {
				tabKey: "schedules",
				schedulesDetail: {
					show: true,
					id: item.id,
					fromTabKey: tabKey,
					callback: (newItem) => {
						if ("isRemove" in newItem) newItem.isRemove && handleDeleteSchedule(newItem);
						else if (newItem) _getScheduleList.mutate((oldData) => (oldData || []).map((item) => item.id === newItem.id ? newItem : item));
					}
				}
			}
		});
	});
	return /* @__PURE__ */ React.createElement("div", { className: "product-detail-card-module" }, isTitle ? /* @__PURE__ */ React.createElement("div", { className: "product-detail-card-title" }, locales.getText("pisell2.text.schedules.schedules-title")) : null, /* @__PURE__ */ React.createElement(ScheduleList, {
		className: "product-detail-card-schedule-list",
		dataSource: _getScheduleList.data || [],
		loading: _getScheduleList.loading,
		showDeleteIcon: true,
		onDelete: handleDeleteSchedule,
		onItemClick: onClickItem
	}), /* @__PURE__ */ React.createElement(AddSchedules, {
		isSelf,
		value: _getScheduleList.data || [],
		onChange: _onAddChange,
		drawerContainer
	}));
};
//#endregion
export { SchedulesModule as default };
