const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../../../../hooks/useEngineContext.js");
const require_model = require("../../../../model.js");
const require_utils = require("../../../../utils.js");
const require_utils$1 = require("../../../../schedules/utils.js");
const require_index = require("../../../../components/ScheduleList/index.js");
const require_serve = require("../../../serve.js");
const require_index$1 = require("./AddSchedules/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
require("./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 = require_useEngineContext.default();
	if (isSelf) require_utils.request.setRequest(context.appHelper.utils.request);
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const first = (0, react.useRef)(true);
	const _getScheduleList = (0, ahooks.useRequest)(require_serve.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);
	};
	(0, react.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
			}
		});
	};
	(0, react.useEffect)(() => {
		if (ruleFn === null || ruleFn === void 0 ? void 0 : ruleFn()) setCalendarEditData(require_utils$1.calcCalendarDataBySchedules(_getScheduleList.data || []));
	}, [_getScheduleList.data, ruleFn === null || ruleFn === void 0 ? void 0 : ruleFn()]);
	const onClickItem = (0, ahooks.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.default.createElement("div", { className: "product-detail-card-module" }, isTitle ? /* @__PURE__ */ react.default.createElement("div", { className: "product-detail-card-title" }, _pisell_utils.locales.getText("pisell2.text.schedules.schedules-title")) : null, /* @__PURE__ */ react.default.createElement(require_index.default, {
		className: "product-detail-card-schedule-list",
		dataSource: _getScheduleList.data || [],
		loading: _getScheduleList.loading,
		showDeleteIcon: true,
		onDelete: handleDeleteSchedule,
		onItemClick: onClickItem
	}), /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		isSelf,
		value: _getScheduleList.data || [],
		onChange: _onAddChange,
		drawerContainer
	}));
};
//#endregion
exports.default = SchedulesModule;
