/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getLightElement, notChildEventListener, removeAllChildren, toggleAttribute } from '@tylertech/forge-core';
import { IconComponentDelegate } from '../../icon';
import { AVATAR_CONSTANTS } from '../../avatar';
import { BaseAdapter } from '../../core/base/base-adapter';
import { PROFILE_CARD_CONSTANTS } from '../../profile-card';
import { APP_BAR_PROFILE_BUTTON_CONSTANTS } from './app-bar-profile-button-constants';
import { ICON_BUTTON_CONSTANTS } from '../../icon-button';
import { forwardAttributes } from '../../core/utils/reflect-utils';
export class AppBarProfileButtonAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
    }
    get popupElement() {
        return this._popupElement;
    }
    initialize() {
        this._avatarElement = getLightElement(this._component, AVATAR_CONSTANTS.elementName);
        this._iconButtonElement = getLightElement(this._component, ICON_BUTTON_CONSTANTS.elementName);
        const originalAriaLabelledBy = this._iconButtonElement.getAttribute('aria-labelledby'); // Set by tooltip
        this._forwardObserver = forwardAttributes(this._component, APP_BAR_PROFILE_BUTTON_CONSTANTS.forwardedAttributes, (name, value) => {
            if (name === 'aria-labelledby' && !value) {
                value = originalAriaLabelledBy;
            }
            toggleAttribute(this._iconButtonElement, !!value, name, value !== null && value !== void 0 ? value : undefined);
        });
    }
    destroy() {
        var _a;
        (_a = this._forwardObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
        this._forwardObserver = undefined;
        if (this._popupElement) {
            this._popupElement.remove();
            this._popupElement = undefined;
            this._profileCardElement = undefined;
        }
    }
    setClickListener(listener) {
        this._component.addEventListener('click', listener);
    }
    removeClickListener(listener) {
        this._component.removeEventListener('click', listener);
    }
    openPopup(profileCardConfig, dismissListener, profileListener, signOutListener, profileCardContent) {
        var _a;
        if ((_a = this._popupElement) === null || _a === void 0 ? void 0 : _a.isConnected) {
            this._popupElement.remove();
        }
        this._profileCardElement = document.createElement(PROFILE_CARD_CONSTANTS.elementName);
        this._profileCardElement.fullName = profileCardConfig.fullName;
        this._profileCardElement.email = profileCardConfig.email;
        this._profileCardElement.signOut = profileCardConfig.signOut;
        this._profileCardElement.profile = profileCardConfig.profile;
        this._profileCardElement.signOutText = profileCardConfig.signOutButtonText;
        this._profileCardElement.profileText = profileCardConfig.profileButtonText;
        this._profileCardElement.avatarText = profileCardConfig.avatarText;
        this._profileCardElement.avatarIcon = profileCardConfig.avatarIcon;
        this._profileCardElement.avatarImageUrl = profileCardConfig.avatarImageUrl;
        this._profileCardElement.avatarLetterCount = profileCardConfig.avatarLetterCount;
        this._profileCardElement.addEventListener(PROFILE_CARD_CONSTANTS.events.PROFILE, () => profileListener());
        this._profileCardElement.addEventListener(PROFILE_CARD_CONSTANTS.events.SIGN_OUT, () => signOutListener());
        if (profileCardContent) {
            this._profileCardElement.appendChild(profileCardContent);
        }
        this._popupElement = document.createElement('forge-popover');
        this._popupElement.anchorElement = this._iconButtonElement;
        this._popupElement.placement = 'bottom-end';
        this._popupElement.arrow = true;
        this._popupElement.persistent = true;
        this._popupElement.appendChild(this._profileCardElement);
        this._component.ownerDocument.body.appendChild(this._popupElement);
        this._popupElement.open = true;
        this._profileCardElement.tabIndex = -1;
        this._profileCardElement.focus();
        return notChildEventListener(this._popupElement, () => {
            if (!this._popupElement) {
                dismissListener();
                return;
            }
            if (!this._popupElement.matches(':focus-within') && !this._component.matches(':focus-within')) {
                dismissListener();
            }
        }, true);
    }
    async closePopup() {
        if (this._popupElement) {
            await this._popupElement.hideAsync();
            this._popupElement.remove();
            this._popupElement = undefined;
            this._profileCardElement = undefined;
        }
    }
    focusButtonElement() {
        this._iconButtonElement.focus();
    }
    setAvatarText(value) {
        this._avatarElement.text = value;
        removeAllChildren(this._avatarElement);
    }
    setAvatarIcon(value) {
        if (value) {
            const iconDelegate = new IconComponentDelegate({ props: { name: value } });
            this._avatarElement.replaceChildren(iconDelegate.element);
        }
        else {
            removeAllChildren(this._avatarElement);
        }
    }
    setAvatarLetterCount(value) {
        this._avatarElement.letterCount = value;
    }
    setAvatarImageUrl(value) {
        this._avatarElement.imageUrl = value;
    }
    setSignOutButtonText(value) {
        var _a;
        if (this._profileCardElement) {
            this._profileCardElement.signOutText = value;
            (_a = this._popupElement) === null || _a === void 0 ? void 0 : _a.position();
        }
    }
    setProfileButtonText(value) {
        var _a;
        if (this._profileCardElement) {
            this._profileCardElement.profileText = value;
            (_a = this._popupElement) === null || _a === void 0 ? void 0 : _a.position();
        }
    }
}
