/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BUTTON_CONSTANTS } from '../button';
import { BaseAdapter } from '../core/base/base-adapter';
export class SplitButtonAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
    }
    startButtonObserver() {
        // This observer is used to keep the buttons in sync with the split button state when they are added to DOM
        this._buttonChangeObserver = new MutationObserver(mutations => {
            // Find all `<forge-button>` elements that are contained within the added nodes
            const addedButtons = mutations.reduce((buttons, { addedNodes }) => {
                const addedButtonNodes = Array.from(addedNodes)
                    .filter(node => node.nodeType === Node.ELEMENT_NODE)
                    .map((node) => {
                    if (node.nodeName.toLowerCase() === BUTTON_CONSTANTS.elementName) {
                        return node;
                    }
                    return node.querySelector(BUTTON_CONSTANTS.elementName);
                })
                    .filter(node => !!node);
                return buttons.concat(addedButtonNodes);
            }, []);
            if (!addedButtons.length) {
                return;
            }
            addedButtons.forEach(button => {
                button.variant = this._component.variant;
                button.theme = this._component.theme;
                button.disabled = this._component.disabled;
                button.dense = this._component.dense;
            });
            this.setPill(this._component.pill);
        });
        this._buttonChangeObserver.observe(this._component, { childList: true, subtree: true });
    }
    destroyButtonObserver() {
        var _a;
        (_a = this._buttonChangeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
        this._buttonChangeObserver = undefined;
    }
    setVariant(variant) {
        const buttons = this._getButtons();
        buttons.forEach(button => button.variant = variant);
    }
    setTheme(theme) {
        const buttons = this._getButtons();
        buttons.forEach(button => button.theme = theme);
    }
    setDisabled(value) {
        const buttons = this._getButtons();
        buttons.forEach(button => button.disabled = value);
    }
    setDense(value) {
        const buttons = this._getButtons();
        buttons.forEach(button => button.dense = value);
    }
    setPill(value) {
        const buttons = this._getButtons();
        // First we reset all the middle buttons to not be pill buttons
        if (buttons.length > 2) {
            Array.from(buttons)
                .slice(1, buttons.length - 1)
                .filter(({ pill }) => pill)
                .forEach(button => button.pill = false);
        }
        // Only the first and last buttons need to be pill shaped
        const firstButton = buttons[0];
        if (firstButton) {
            firstButton.pill = value;
        }
        const lastButton = buttons[buttons.length - 1];
        if (lastButton) {
            lastButton.pill = value;
        }
    }
    _getButtons() {
        return this._component.querySelectorAll(BUTTON_CONSTANTS.elementName);
    }
}
