/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, toggleClass } from '@tylertech/forge-core';
import { MDCTabIndicator } from '@material/tab-indicator';
import { BaseAdapter } from '../../core/base/base-adapter';
import { TAB_CONSTANTS } from './tab-constants';
import { ForgeRipple, ForgeRippleFoundation } from '../../ripple';
class TabRippleSurface {
    constructor(_root) {
        this._root = _root;
    }
    get root() {
        return this._root;
    }
    get unbounded() {
        return false;
    }
    get disabled() {
        return this._root.disabled;
    }
}
export class TabAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._rootElement = getShadowElement(this._component, TAB_CONSTANTS.selectors.ROOT);
        this._content = getShadowElement(this._component, TAB_CONSTANTS.selectors.CONTENT);
        this._rippleElement = getShadowElement(this._component, TAB_CONSTANTS.selectors.RIPPLE);
        this._tabIndicatorElement = getShadowElement(this._component, TAB_CONSTANTS.selectors.INDICATOR);
    }
    initialize() {
        if (!this._component.hasAttribute('role')) {
            this._component.setAttribute('role', 'tab');
        }
    }
    initializeRipple() {
        const rippleCapableSurface = new TabRippleSurface(this._component);
        const rippleAdapter = Object.assign(Object.assign({}, ForgeRipple.createAdapter(rippleCapableSurface)), { addClass: (className) => this._rippleElement.classList.add(className), removeClass: (className) => this._rippleElement.classList.remove(className), updateCssVariable: (varName, value) => this._rippleElement.style.setProperty(varName, value) });
        const rippleFoundation = new ForgeRippleFoundation(rippleAdapter);
        this._rippleInstance = new ForgeRipple(this._component, rippleFoundation);
    }
    destroyRipple() {
        if (this._rippleInstance) {
            this._rippleInstance.destroy();
        }
    }
    initializeIndicator() {
        this._tabIndicator = new MDCTabIndicator(this._tabIndicatorElement);
    }
    destroyIndicator() {
        if (this._tabIndicator) {
            this._tabIndicator.destroy();
            this._tabIndicator = undefined;
        }
    }
    activateIndicator(previousIndicatorClientRect) {
        if (this._tabIndicator) {
            this._tabIndicator.activate(previousIndicatorClientRect);
        }
    }
    deactivateIndicator() {
        if (this._tabIndicator) {
            this._tabIndicator.deactivate();
        }
    }
    computeIndicatorBounds() {
        return this._tabIndicator ? this._tabIndicator.computeContentClientRect() : undefined;
    }
    addButtonListener(type, listener) {
        this._component.addEventListener(type, listener);
    }
    removeButtonListener(type, listener) {
        this._component.removeEventListener(type, listener);
    }
    setDisabled(value) {
        toggleClass(this._rootElement, value, TAB_CONSTANTS.classes.DISABLED);
        this.setTabIndex(!value ? 0 : -1);
        this._component.setAttribute('aria-disabled', value.toString());
    }
    setActive(value) {
        toggleClass(this._rootElement, value, TAB_CONSTANTS.classes.ACTIVE);
        this.setTabIndex(value ? 0 : -1);
        this._component.setAttribute('aria-selected', value.toString());
    }
    getOffsetLeft() {
        return this._rootElement.offsetLeft;
    }
    getOffsetWidth() {
        return this._rootElement.offsetWidth;
    }
    getContentOffsetLeft() {
        return this._content.offsetLeft;
    }
    getContentOffsetWidth() {
        return this._content.offsetWidth;
    }
    setTabIndex(value) {
        this._component.tabIndex = value;
    }
}
