/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getLightElement, notChildEventListener, getActiveElement, removeAllChildren } from '@tylertech/forge-core';
import { IconComponentDelegate } from '../../icon';
import { AVATAR_CONSTANTS } from '../../avatar';
import { BaseAdapter } from '../../core/base/base-adapter';
import { PopupAnimationType, POPUP_CONSTANTS } from '../../popup';
import { PROFILE_CARD_CONSTANTS } from '../../profile-card';
import { APP_BAR_PROFILE_BUTTON_CONSTANTS } from './app-bar-profile-button-constants';
export class AppBarProfileButtonAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
    }
    initialize() {
        this._avatarElement = getLightElement(this._component, AVATAR_CONSTANTS.elementName);
        this._buttonElement = getLightElement(this._component, APP_BAR_PROFILE_BUTTON_CONSTANTS.selectors.BUTTON);
    }
    setClickListener(listener) {
        this._component.addEventListener('click', listener);
    }
    removeClickListener(listener) {
        this._component.removeEventListener('click', listener);
    }
    openPopup(profileCardConfig, dismissListener, profileListener, signOutListener, profileCardContent) {
        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(POPUP_CONSTANTS.elementName);
        this._popupElement.targetElement = this._component;
        this._popupElement.placement = 'bottom-end';
        this._popupElement.animationType = PopupAnimationType.Menu;
        this._popupElement.appendChild(this._profileCardElement);
        this._popupElement.open = true;
        return notChildEventListener(this._popupElement, activeElement => {
            if (!this._popupElement) {
                return;
            }
            if (!this._component.contains(getActiveElement(this._component.ownerDocument))) {
                dismissListener();
            }
        }, true);
    }
    closePopup() {
        if (this._popupElement) {
            this._popupElement.open = false;
            this._popupElement = undefined;
            this._profileCardElement = undefined;
        }
    }
    requestFocus() {
        this._buttonElement.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();
        }
    }
}
