const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_IntersectionObserver = require("./components/IntersectionObserver.js");
const require_CategorySkeleton = require("./CategoryTabs/CategorySkeleton.js");
const require_gridComponents = require("./ProductList/gridComponents.js");
const require_index = require("./ProductList/index.js");
const require_useToken = require("./hooks/useToken.js");
const require_utils = require("./utils.js");
const require_locales = require("./locales.js");
const require_ProductCardSkeleton = require("./ProductCard/ProductCardSkeleton.js");
const require_index$1 = require("./ProductCard/index.js");
const require_ProductCardV2 = require("./ProductCard/ProductCardV2.js");
const require_index$2 = require("./CategoryTabs/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let _pisell_icon_es_SearchLg = require("@pisell/icon/es/SearchLg");
_pisell_icon_es_SearchLg = require_runtime.__toESM(_pisell_icon_es_SearchLg);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/plus/productSelect/index.tsx
const _excluded = [
	"dataSource",
	"emptyDescription",
	"showSearch",
	"scrollDom",
	"offsetDom",
	"style",
	"token",
	"onChange",
	"value",
	"selectedItems",
	"productListSetting",
	"customScrollParent",
	"loading",
	"layout",
	"hideCategoryWhenOne",
	"version",
	"cartElementId"
];
const MOCK_DATA = (() => {
	const mockData = [];
	mockData.push({
		__typename: "category",
		id: "_c1_1"
	});
	for (let productIndex = 0; productIndex < 4; productIndex++) mockData.push({
		__typename: "product",
		id: "_pi" + productIndex
	});
	return mockData;
})();
const PREFIX = "pisell-product-select";
const CONTAINER_CLASS = `${PREFIX}-wrapper`;
const index = (_ref) => {
	var _context$appHelper$ut;
	let { dataSource = [], emptyDescription, showSearch = true, scrollDom, offsetDom, style = {}, token: customToken, onChange, value, selectedItems, productListSetting = {}, customScrollParent, loading = false, layout, hideCategoryWhenOne = false, version = "v1", cartElementId } = _ref;
	require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get("umi_locale")) || "en");
	const searchRef = (0, react.useRef)(null);
	const [keyword, setKeyword] = (0, react.useState)("");
	const [activeKey, setActiveKey] = (0, react.useState)("");
	const [scrollParent, setScrollParent] = (0, react.useState)(null);
	const isIndicatorVisibleRef = (0, react.useRef)(false);
	const containerRef = (0, react.useRef)(null);
	const _customScrollParent = customScrollParent || scrollDom;
	const gridComponents = (0, react.useMemo)(() => {
		return require_gridComponents.createGridComponents(layout);
	}, [layout]);
	const _filteredList = (0, react.useMemo)(() => {
		if (!keyword) return dataSource;
		return dataSource.filter((product) => {
			if (typeof (product === null || product === void 0 ? void 0 : product.title) !== "string") return false;
			return product.title.toLowerCase().indexOf(keyword.toLowerCase()) !== -1;
		});
	}, [keyword, dataSource]);
	const _groupedList = (0, react.useMemo)(() => {
		return require_utils.groupProductsByCategory(_filteredList || []);
	}, [_filteredList]);
	const _categories = (0, react.useMemo)(() => {
		return _groupedList.filter((item) => item.__typename === "category").map((item) => {
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
				key: String(item.id),
				label: item.name
			});
		});
	}, [_groupedList]);
	/**
	* @title: 是否隐藏分类
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-10-09 15:13
	*/
	const isHideCategory = (0, react.useMemo)(() => {
		if ((productListSetting === null || productListSetting === void 0 ? void 0 : productListSetting.layout) === 0) return true;
		if (hideCategoryWhenOne) return (_categories === null || _categories === void 0 ? void 0 : _categories.length) === 1 || !(_categories === null || _categories === void 0 ? void 0 : _categories.length);
		return false;
	}, [
		_categories,
		hideCategoryWhenOne,
		productListSetting === null || productListSetting === void 0 ? void 0 : productListSetting.layout
	]);
	(0, react.useEffect)(() => {
		if (!(_categories === null || _categories === void 0 ? void 0 : _categories.length)) {
			setActiveKey("");
			return;
		}
		if (!activeKey) switchCategory(_categories[0].key);
		else if (!_categories.find((category) => category.key === activeKey)) switchCategory(_categories[0].key);
	}, [_categories]);
	(0, react.useLayoutEffect)(() => {
		if (_customScrollParent) setScrollParent(require_utils.getElementByIdentifier(_customScrollParent));
	}, [_customScrollParent]);
	const token = require_useToken.useToken(customToken);
	const cssVars = (0, react.useMemo)(() => require_useToken.genCSSVars(token), [token]);
	const mergedStyle = (0, react.useMemo)(() => {
		const baseStyle = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cssVars), style);
		if (!scrollParent) return baseStyle;
		if (scrollParent !== containerRef.current) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, baseStyle), {}, {
			overflow: "unset",
			height: "unset"
		});
		return baseStyle;
	}, [
		cssVars,
		style,
		scrollParent
	]);
	const disabled = (0, react.useMemo)(() => {
		return !!(productListSetting === null || productListSetting === void 0 ? void 0 : productListSetting.list_max_length) && Array.isArray(selectedItems) && (selectedItems === null || selectedItems === void 0 ? void 0 : selectedItems.length) > 0;
	}, [productListSetting, selectedItems]);
	const _onChange = (0, react.useCallback)((value, type) => {
		if (disabled) return;
		onChange === null || onChange === void 0 || onChange(value, type);
	}, [disabled, onChange]);
	const debounceIndicator = (0, react.useCallback)((delay) => {
		isIndicatorVisibleRef.current = true;
		const timeoutId = setTimeout(() => {
			isIndicatorVisibleRef.current = false;
		}, delay);
		return () => clearTimeout(timeoutId);
	}, []);
	const lastActiveKeyRef = (0, react.useRef)("");
	const handleItemIntersectionIn = (0, react.useCallback)((item) => {
		const _key = String(item.__categoryId);
		if (!isIndicatorVisibleRef.current && _key && _key !== lastActiveKeyRef.current) {
			setActiveKey(_key);
			lastActiveKeyRef.current = _key;
		}
	}, []);
	const _listContext = (0, react.useMemo)(() => {
		return {
			onItemIntersectionIn: handleItemIntersectionIn,
			onItemIntersectionOut: () => {}
		};
	}, [handleItemIntersectionIn]);
	const switchCategory = (key) => {
		setActiveKey(key);
		lastActiveKeyRef.current = key;
		debounceIndicator(300);
		const categoryIndex = _groupedList.findIndex((item) => item.__typename === "category" && String(item.id) === key);
		const _scrollParent = scrollParent || containerRef.current;
		if (!_scrollParent || !containerRef.current) return;
		const target = document.getElementById(`ps_category_${key}`);
		if (!target) return;
		const productSelectWrapper = document.querySelector(`.${PREFIX}-wrapper`);
		const tabsWrapper = document.querySelector(`.${PREFIX}__tabs-wrapper`);
		const tabsHeight = (tabsWrapper === null || tabsWrapper === void 0 ? void 0 : tabsWrapper.offsetHeight) || 0;
		const offsetElement = require_utils.getElementByIdentifier(offsetDom);
		const extraOffset = offsetElement ? offsetElement.clientHeight : 0;
		if (categoryIndex === 0) {
			if (productSelectWrapper) _scrollParent.scrollTop = require_utils.getOffsetTopRelativeToScrollContainer(productSelectWrapper, _scrollParent) - extraOffset;
			return;
		} else _scrollParent.scrollTop = require_utils.getOffsetTopRelativeToScrollContainer(target, _scrollParent) - tabsHeight - extraOffset;
	};
	return /* @__PURE__ */ react.default.createElement("div", {
		ref: containerRef,
		className: (0, classnames.default)(PREFIX, CONTAINER_CLASS, { disabled }),
		style: mergedStyle
	}, showSearch ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}__search-wrapper` }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Input, {
		ref: searchRef,
		prefix: /* @__PURE__ */ react.default.createElement(_pisell_icon_es_SearchLg.default, null),
		style: { height: 44 },
		placeholder: _pisell_utils.locales.getText("pisell2.product-select.search"),
		allowClear: true,
		value: keyword,
		onChange: (e) => {
			setKeyword(e.target.value);
		}
	})) : null, !isHideCategory ? /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		activeKey,
		items: _categories,
		onChange: switchCategory,
		loading,
		skeleton: /* @__PURE__ */ react.default.createElement(require_CategorySkeleton.default, null)
	}) : null, !loading && !(_groupedList === null || _groupedList === void 0 ? void 0 : _groupedList.length) ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellEmpty, { description: emptyDescription || _pisell_utils.locales.getText("pisell2.product-select.empty") }) : /* @__PURE__ */ react.default.createElement(require_IntersectionObserver.default, {
		rootMargin: "-270px 0px -270px 0px",
		priorityMode: "top"
	}, /* @__PURE__ */ react.default.createElement(require_index.default, {
		className: (0, classnames.default)(`${PREFIX}__product-list`, { [`${PREFIX}__product-list_hide_category`]: isHideCategory }),
		loading,
		data: loading ? MOCK_DATA : _groupedList,
		_customScrollParent: scrollParent,
		context: _listContext,
		components: gridComponents,
		itemContent: (index, item, context) => {
			if (loading) return /* @__PURE__ */ react.default.createElement(require_ProductCardSkeleton.default, {
				index,
				item
			});
			if (version === "v2") return /* @__PURE__ */ react.default.createElement(require_ProductCardV2.default, {
				index,
				item,
				context,
				value,
				onChange,
				decorateConfig: productListSetting,
				cartElementId
			});
			return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
				index,
				item,
				context,
				onSelectCover: (e) => {
					_onChange(e, "cover");
				},
				onSelectInfo: (e) => {
					_onChange(e, "info");
				},
				isSelected: item.id === value,
				decorateConfig: productListSetting
			});
		}
	})));
};
//#endregion
exports.PREFIX = PREFIX;
exports.default = index;
