import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import EmptyBooking from "../../../booking/components/emptyBooking/index.js";
import { formatProductListByCategory } from "../utils.js";
import { Context } from "../../model.js";
import Search from "../../components/Search/index.js";
import TabProduct from "../../../tabProduct/index.js";
import ProductItem from "./ProductItem/index.js";
import React, { useContext, useMemo, useState } from "react";
import { Skeleton } from "antd";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/schedules/products/list/index.tsx
const ProductList = ({ loading, list }) => {
	const [search, setSearch] = useState("");
	const { state, dispatch } = useContext(Context);
	const { products, currentSelectDayDetail } = state;
	const renderList = useMemo(() => {
		const isIncludes = (str) => {
			return str.toLowerCase().indexOf(search.toLowerCase()) !== -1;
		};
		if (currentSelectDayDetail) return list.filter((item) => isIncludes(item.title));
		return formatProductListByCategory(list, search ? (item) => isIncludes(item.title) : void 0);
	}, [
		search,
		list,
		currentSelectDayDetail
	]);
	console.log("state.scheduleMap", renderList);
	const searchChange = (e) => {
		setSearch(e);
	};
	const onClickItem = (item) => {
		let newData = { products: _objectSpread2(_objectSpread2({}, products), {}, {
			currentProduct: item,
			openProductDetailDrawer: !currentSelectDayDetail
		}) };
		if (currentSelectDayDetail) newData = {
			tabKey: "schedules",
			products: _objectSpread2(_objectSpread2({}, products), {}, {
				currentProduct: item,
				openProductDetailDrawer: !currentSelectDayDetail
			})
		};
		dispatch({
			type: "setState",
			payload: newData
		});
	};
	const empty = useMemo(() => {
		return /* @__PURE__ */ React.createElement(EmptyBooking, { description: locales.getText("pisell2.text.products.no-products") });
	}, []);
	const listDom = useMemo(() => {
		if (currentSelectDayDetail) return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-products-list-day" }, renderList.map((item) => {
			return /* @__PURE__ */ React.createElement(ProductItem, {
				key: item.id,
				item,
				onClickItem,
				layout: "day"
			});
		}));
		return /* @__PURE__ */ React.createElement(TabProduct, {
			scrollDom: ".pisell-lowcode__schedules-component-products",
			list: renderList,
			renderItem: (item) => {
				return /* @__PURE__ */ React.createElement(ProductItem, {
					key: item.id,
					item,
					onClickItem
				});
			}
		});
	}, [
		currentSelectDayDetail,
		renderList,
		onClickItem
	]);
	return /* @__PURE__ */ React.createElement(Skeleton, {
		className: "pisell-lowcode__schedules-component-products-list-wrap",
		round: true,
		active: true,
		loading
	}, !list.length ? empty : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Search, {
		value: search,
		onChange: searchChange,
		style: { marginTop: 16 }
	}), renderList.length ? listDom : empty));
};
//#endregion
export { ProductList as default };
