const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../selectModal/index.js");
const require_server = require("./server.js");
const require_index$1 = require("./ProductItem/index.js");
const require_index$2 = require("./ProductItemPreview/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/productExtension/fields/SelectProducts/index.tsx
const SelectProducts = ({ value, onChange }) => {
	const [listMap, setListMap] = (0, react.useState)({});
	const _getProductList = (0, ahooks.useRequest)(require_server.getProductList, {
		manual: true,
		onSuccess: (res) => {
			let obj = {};
			res.forEach((d) => {
				obj[d.id] = d;
			});
			setListMap(obj);
		}
	});
	const _value = (0, react.useMemo)(() => {
		if (value && value.length) return value.map((d) => listMap[d.product_id] || d);
		return value;
	}, [value, listMap]);
	(0, react.useEffect)(() => {
		_getProductList.run({
			skip: 1,
			num: 500,
			with: ["category"],
			open_quotation: 1,
			exclude_extension_type: ["event_item"],
			status: "published"
		});
	}, []);
	const onRemove = (item) => {
		onChange === null || onChange === void 0 || onChange((value || []).filter((d) => (d.id || d.product_id) !== item.id));
	};
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		value: _value,
		loading: _getProductList.loading,
		onChange: (arr) => {
			onChange === null || onChange === void 0 || onChange(arr.map((d) => {
				return {
					product_collection_id: 0,
					product_id: d.id || d.product_id,
					product_variant_id: 0
				};
			}));
		},
		dataSource: _getProductList.data,
		renderValueItem: (item, onSelect) => {
			return /* @__PURE__ */ react.default.createElement(require_index$2.default, {
				key: item === null || item === void 0 ? void 0 : item.id,
				item,
				onClick: onSelect,
				onRemove
			});
		},
		renderOptionItem: (item, onSelect) => {
			return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
				key: item === null || item === void 0 ? void 0 : item.id,
				item,
				onClick: onSelect
			});
		},
		buttonProps: {
			showSub: !!(value === null || value === void 0 ? void 0 : value.length),
			title: _pisell_utils.locales.getText("pisell2.text.product-extension.add-product"),
			subTitle: _pisell_utils.locales.getText("pisell2.text.product-extension.add-other-product")
		},
		drawerProps: {
			width: 500,
			title: _pisell_utils.locales.getText("pisell2.text.product-extension.add-product"),
			rootClassName: "select-product-drawer"
		},
		tabProductProps: {
			warpClassName: "select-product-warp",
			stickyTop: 188,
			scrollDom: ".select-product-drawer .pisell-lowcode-drawer-body"
		}
	});
};
//#endregion
exports.default = SelectProducts;
