/** * Copyright Aquera Inc 2025 * * This source code is licensed under the BSD-3-Clause license found in the * LICENSE file in the root directory of this source tree. */ import type { ComboboxOption } from './types.js'; export class ComboboxSelectionManager { static createOptionsFromValues( value: string | string[], data: any[], getDisplayText: (item: any) => string, getItemValue?: (item: any) => string ): ComboboxOption[] { if (!value || (Array.isArray(value) && value.length === 0)) { return []; } const values = Array.isArray(value) ? value : [value]; const valueFn = getItemValue || ((item: any) => item?.value ?? item); return values.map(valueItem => { const item = data.find((d: any) => { const iv = valueFn(d); return String(iv) === String(valueItem); }); const displayText = item ? getDisplayText(item) : valueItem; return { value: valueItem, selected: true, getTextLabel: () => { if (typeof displayText === 'string' && /<[^>]+>/.test(displayText)) { const div = document.createElement('div'); div.innerHTML = displayText; return div.textContent || div.innerText || ''; } return displayText; }, }; }); } static toggleMultiValue(currentValues: string[], optionValue: string): string[] { if (currentValues.includes(optionValue)) { return currentValues.filter(v => v !== optionValue); } return [...currentValues, optionValue]; } static removeValue(currentValues: string[], removeValue: string): string[] { return currentValues.filter(v => v !== removeValue); } }