import { formatProductSchedules } from "./utils.js";
import { Context } from "../model.js";
import CurrentDay from "../components/CurrentDay/index.js";
import ProductList from "./list/index.js";
import { getProductList } from "./serve.js";
import ProductDetailDrawer from "./ProductDetailDrawer/index.js";
import React, { useContext, useEffect } from "react";
import { useRequest } from "ahooks";
import "./index.less";
//#region src/components/schedules/products/index.tsx
const Products = () => {
	const { state, dispatch } = useContext(Context);
	const { products, currentSelectDayDetail } = state;
	const _getProducts = useRequest(getProductList, { manual: true });
	useEffect(() => {
		dispatch({
			type: "setProducts",
			payload: { list: formatProductSchedules(_getProducts.data || [], state.schedules.scheduleMap) }
		});
	}, [_getProducts.data, state.schedules.scheduleMap]);
	const _init = () => {
		dispatch({
			type: "setProducts",
			payload: {
				search: "",
				currentProduct: null,
				openProductDetailDrawer: false,
				openAddSchedulesDrawer: false
			}
		});
		if (currentSelectDayDetail && (!currentSelectDayDetail.scheduleIds || !currentSelectDayDetail.scheduleIds.length)) _getProducts.mutate([]);
		else _getProducts.run({
			skip: 1,
			num: 500,
			with: ["category"],
			status: "published",
			open_quotation: 1,
			with_schedule: true,
			schedule: 1,
			schedule_ids: currentSelectDayDetail ? currentSelectDayDetail.scheduleIds || [] : void 0
		});
	};
	useEffect(() => {
		_init();
	}, [currentSelectDayDetail]);
	const handleClose = () => {
		dispatch({
			type: "setState",
			payload: { currentSelectDayDetail: null }
		});
	};
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-products" }, /* @__PURE__ */ React.createElement(CurrentDay, {
		onClose: handleClose,
		currentDay: currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.date
	}), /* @__PURE__ */ React.createElement(ProductList, {
		loading: _getProducts.loading,
		list: products.list
	}), /* @__PURE__ */ React.createElement(ProductDetailDrawer, null));
};
//#endregion
export { Products as default };
