/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement } from '@tylertech/forge-core';
import { replaceElement } from '../../core/utils/utils';
import { BaseAdapter } from '../../core/base/base-adapter';
import { APP_BAR_CONSTANTS } from './app-bar-constants';
import { STATE_LAYER_CONSTANTS } from '../../state-layer';
import { FOCUS_INDICATOR_CONSTANTS } from '../../focus-indicator';
export class AppBarAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._rootElement = getShadowElement(component, APP_BAR_CONSTANTS.selectors.ROOT);
        this._titleElement = getShadowElement(component, APP_BAR_CONSTANTS.selectors.TITLE);
        this._logoTitleContainerElement = getShadowElement(component, APP_BAR_CONSTANTS.selectors.LOGO_TITLE_CONTAINER);
        this._centerSectionElement = getShadowElement(component, APP_BAR_CONSTANTS.selectors.CENTER_SECTION);
        this._centerSlotElement = getShadowElement(component, APP_BAR_CONSTANTS.selectors.CENTER_SLOT);
    }
    initialize() {
        if (!this._component.hasAttribute('role')) {
            this._component.setAttribute('role', 'banner');
        }
    }
    setTitleText(value) {
        this._titleElement.textContent = value;
    }
    setHref(value, listener) {
        var _a, _b;
        const isAnchor = this._logoTitleContainerElement.tagName === 'A';
        if (value) {
            if (!isAnchor) {
                this._createAnchorElement();
            }
            this._logoTitleContainerElement.href = value;
            this._logoTitleContainerElement.addEventListener('click', listener, { capture: true });
        }
        else if (isAnchor) {
            this._logoTitleContainerElement.removeEventListener('click', listener, { capture: true });
            this._logoTitleContainerElement = replaceElement(this._logoTitleContainerElement, document.createElement('div'));
            this._logoTitleContainerElement.classList.add(APP_BAR_CONSTANTS.classes.LOGO_TITLE_CONTAINER);
            (_a = this._logoTitleContainerElement.querySelector(STATE_LAYER_CONSTANTS.elementName)) === null || _a === void 0 ? void 0 : _a.remove();
            (_b = this._logoTitleContainerElement.querySelector(FOCUS_INDICATOR_CONSTANTS.elementName)) === null || _b === void 0 ? void 0 : _b.remove();
        }
    }
    setTarget(value) {
        if (this._logoTitleContainerElement.tagName === 'A') {
            this._logoTitleContainerElement.target = value;
        }
    }
    addCenterSlotListener(listener) {
        this._centerSlotElement.addEventListener('slotchange', listener);
    }
    setCenterSlotVisibility() {
        if (this._centerSlotElement.assignedNodes().length) {
            this._centerSectionElement.style.removeProperty('display');
            this._rootElement.classList.remove(APP_BAR_CONSTANTS.classes.NO_CENTER);
        }
        else {
            this._centerSectionElement.style.display = 'none';
            this._rootElement.classList.add(APP_BAR_CONSTANTS.classes.NO_CENTER);
        }
    }
    _createAnchorElement() {
        this._logoTitleContainerElement = replaceElement(this._logoTitleContainerElement, document.createElement('a'));
        this._logoTitleContainerElement.classList.add(APP_BAR_CONSTANTS.classes.LOGO_TITLE_CONTAINER);
        const stateLayer = document.createElement('forge-state-layer');
        const focusIndicator = document.createElement('forge-focus-indicator');
        focusIndicator.inward = true;
        this._logoTitleContainerElement.append(stateLayer, focusIndicator);
    }
}
