const UNKNOWN_ICON = {
    name: 'question',
    color: 'rgb(var(--color-glaucous-light))',
};
/**
 * Class map for a rule-node container. Adds the type modifier and an
 * `invalid` flag when validation issues attach to this node.
 * @param type
 * @param issues
 */
export function nodeClasses(type, issues) {
    return {
        'rule-node': true,
        [`rule-node--${type}`]: true,
        'rule-node--invalid': issues.length > 0,
    };
}
/**
 * Build the searchable list shown by the primitive pickers. Cached on
 * the render context so it isn't recomputed per node. `meta.title` and
 * `meta.description` are passed through the translator so callers can
 * register translation keys as metadata — plain strings travel through
 * unchanged.
 * @param metadataById
 * @param translator
 */
export function createPrimitiveOptions(metadataById, translator) {
    return [...metadataById.values()]
        .map((meta) => ({
        value: meta.id,
        text: meta.title ? translator.get(meta.title) : meta.id,
        secondaryText: meta.description
            ? translator.get(meta.description)
            : '',
        icon: meta.icon,
    }))
        .sort((a, b) => a.text.localeCompare(b.text));
}
/**
 * Find the picker item for `id`, falling back to a placeholder marked
 * with the unknown-primitive icon when the id isn't in the registry.
 * @param allItems
 * @param id
 */
export function resolveItem(allItems, id) {
    var _a;
    return ((_a = allItems.find((item) => item.value === id)) !== null && _a !== void 0 ? _a : {
        value: id,
        text: id,
        icon: UNKNOWN_ICON,
    });
}
/**
 * Build a picker `searcher` bound to a fixed item list. Both the ref
 * picker and the chip picker want exactly this — wrapping
 * {@link searchItems} in a promise — and deserve to share it.
 * @param items
 */
export function createSearcher(items) {
    return (query) => Promise.resolve(searchItems(items, query));
}
function searchItems(allItems, query) {
    if (!query) {
        return allItems;
    }
    const parts = query.toLocaleLowerCase().split(/\s+/);
    return allItems.filter((item) => parts.every((part) => {
        var _a, _b;
        return item.text.toLocaleLowerCase().includes(part) ||
            ((_a = item.secondaryText) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase().includes(part)) ||
            ((_b = item.value) === null || _b === void 0 ? void 0 : _b.toLocaleLowerCase().includes(part));
    }));
}
