/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement } from '@tylertech/forge-core';
import { BaseButtonAdapter } from '../button/base/base-button-adapter';
import { FLOATING_ACTION_BUTTON_CONSTANTS } from './floating-action-button-constants';
export class FloatingActionButtonAdapter extends BaseButtonAdapter {
    constructor(component) {
        super(component);
        this._labelSlotElement = getShadowElement(this._component, FLOATING_ACTION_BUTTON_CONSTANTS.selectors.LABEL_SLOT);
    }
    initialize() {
        super.initialize();
        if (!this._extendedObserver) {
            this._startExtendedWatcher();
        }
        this._detectExtendedState();
    }
    destroy() {
        var _a;
        (_a = this._extendedObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
        this._extendedObserver = undefined;
    }
    toggleDefaultPopoverIcon(value) {
        super.toggleDefaultPopoverIcon(value);
        this._detectExtendedState();
    }
    _startExtendedWatcher() {
        this._extendedObserver = new MutationObserver(() => this._detectExtendedState());
        this._extendedObserver.observe(this._component, { childList: true, subtree: true, characterData: true });
    }
    _detectExtendedState() {
        // If there are nodes in the "label" slot, then we always assume the extended state
        const hasLabel = this._labelSlotElement.assignedNodes().length > 0;
        if (hasLabel) {
            this._rootElement.classList.add(FLOATING_ACTION_BUTTON_CONSTANTS.classes.EXTENDED);
            return;
        }
        // Remove the extended class first so we can compare the width without it affecting the styles
        this._rootElement.classList.remove(FLOATING_ACTION_BUTTON_CONSTANTS.classes.EXTENDED);
        // When the width is greater than the height, we assume the button is in the extended state
        const isExtended = this._component.clientWidth > this._component.clientHeight;
        if (isExtended) {
            this._rootElement.classList.add(FLOATING_ACTION_BUTTON_CONSTANTS.classes.EXTENDED);
        }
    }
}
