require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_serve = require("../serve.js");
const require_utils = require("../utils.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/plus/saasDevice/devicePlanning/hooks/useDeviceModels.ts
const EMPTY_BRANDS = [];
/** 缓存各设备种类的型号和品牌数据，并生成表格筛选所需的 profile 名称映射。 */
const useDeviceModels = ({ activeKind, selectedWorkAreaId, devices, translation }) => {
	const [modelCacheByKind, setModelCacheByKind] = (0, react.useState)({});
	const modelCacheByKindRef = (0, react.useRef)({});
	const requestedModelKindsRef = (0, react.useRef)(/* @__PURE__ */ new Set());
	const deviceBrandRequest = (0, ahooks.useRequest)(require_serve.getDeviceBrands, { manual: true });
	(0, react.useEffect)(() => {
		if (!selectedWorkAreaId) return;
		if (modelCacheByKindRef.current[activeKind]) return;
		if (requestedModelKindsRef.current.has(activeKind)) return;
		requestedModelKindsRef.current.add(activeKind);
		require_serve.getDeviceTypes({
			kind: activeKind,
			with: [
				"activeModels",
				"activeModels.activeProfiles",
				"activeModels.brand"
			]
		}).then((types) => {
			const models = require_utils.flattenDeviceModels(types);
			modelCacheByKindRef.current = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modelCacheByKindRef.current), {}, { [activeKind]: models });
			setModelCacheByKind(modelCacheByKindRef.current);
		}).catch(() => {
			requestedModelKindsRef.current.delete(activeKind);
			modelCacheByKindRef.current = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modelCacheByKindRef.current), {}, { [activeKind]: [] });
			setModelCacheByKind(modelCacheByKindRef.current);
		});
	}, [activeKind, selectedWorkAreaId]);
	const activeModels = modelCacheByKind[activeKind] || [];
	const typeOptions = (0, react.useMemo)(() => activeModels.map((model) => ({
		label: translation(model.name) || model.code,
		value: model.code
	})), [activeModels, translation]);
	const profileNameMap = (0, react.useMemo)(() => {
		const map = /* @__PURE__ */ new Map();
		Object.values(modelCacheByKind).forEach((models) => {
			(models || []).forEach((model) => {
				(model.active_profiles || []).forEach((profile) => {
					map.set(profile.id, translation(profile.name) || String(profile.id));
				});
			});
		});
		devices.forEach((device) => {
			var _device$profile;
			if ((_device$profile = device.profile) === null || _device$profile === void 0 ? void 0 : _device$profile.id) map.set(device.profile.id, translation(device.profile.name) || String(device.profile.id));
		});
		return map;
	}, [
		devices,
		modelCacheByKind,
		translation
	]);
	/** 只有外设新增入口需要品牌筛选，按需加载避免无用请求。 */
	const loadBrandsIfNeeded = () => {
		if (activeKind === "dumb" && !deviceBrandRequest.data && !deviceBrandRequest.loading) deviceBrandRequest.run();
	};
	return {
		modelCacheByKind,
		activeModels,
		typeOptions,
		profileNameMap,
		brands: deviceBrandRequest.data || EMPTY_BRANDS,
		loadBrandsIfNeeded
	};
};
//#endregion
exports.useDeviceModels = useDeviceModels;
