/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { deepQuerySelectorAll, isDeepEqual } from '@tylertech/forge-core';
import { BaseAdapter } from '../../core/base/base-adapter';
import { LIST_ITEM_CONSTANTS } from '../list-item/list-item-constants';
import { LIST_CONSTANTS } from './list-constants';
export class ListAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
    }
    /** Select all list items that match values in the provided array of values. */
    setSelectedListItems(value) {
        const listItems = this._getOwnListItems();
        if (listItems.length) {
            const values = Array.isArray(value) ? value : [value];
            for (const item of listItems) {
                item.selected = values.some(val => isDeepEqual(val, item.value));
            }
        }
    }
    setListItemsProperty(property, value) {
        this._getOwnListItems().forEach(listItem => listItem[property] = value);
    }
    _getOwnListItems() {
        // Find all deeply nested list items
        const allChildListItems = deepQuerySelectorAll(this._component, LIST_ITEM_CONSTANTS.elementName);
        // Get all list items that are scoped to this component only (not within sub-lists).
        const scopedListItems = [];
        const listener = evt => {
            const composedPath = evt.composedPath();
            const composedBeforeUs = composedPath.slice(0, composedPath.indexOf(this._component));
            if (!composedBeforeUs.some((el) => el.localName === LIST_CONSTANTS.elementName.toLowerCase())) {
                scopedListItems.push(evt.target);
            }
        };
        this._component.addEventListener(LIST_CONSTANTS.events.SCOPE_TEST, listener);
        allChildListItems.forEach(li => li.dispatchEvent(new CustomEvent(LIST_CONSTANTS.events.SCOPE_TEST, { bubbles: true, composed: true })));
        this._component.removeEventListener(LIST_CONSTANTS.events.SCOPE_TEST, listener);
        return scopedListItems;
    }
}
