/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { FOCUS_INDICATOR_CONSTANTS } from './focus-indicator-constants';
export class FocusIndicatorFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._target = null;
        this._active = false;
        this._inward = false;
        this._circular = false;
        this._allowFocus = false;
        this._focusMode = FOCUS_INDICATOR_CONSTANTS.defaults.FOCUS_MODE;
        this._interactionListener = evt => this._onInteraction(evt);
    }
    initialize() {
        if (!this._adapter.hasTargetElement()) {
            this._adapter.trySetTarget(this._target);
        }
        this._addListeners();
    }
    destroy() {
        this._removeListeners(); // Must be called before destroying adapter
        this._adapter.destroy();
    }
    _addListeners() {
        this._adapter.addTargetListener(this._focusMode, this._interactionListener);
        this._adapter.addTargetListener('focusout', this._interactionListener);
        this._adapter.addTargetListener('pointerdown', this._interactionListener);
    }
    _removeListeners() {
        this._adapter.removeTargetListener(this._focusMode, this._interactionListener);
        this._adapter.removeTargetListener('focusout', this._interactionListener);
        this._adapter.removeTargetListener('pointerdown', this._interactionListener);
    }
    _onInteraction(evt) {
        const target = evt.target;
        switch (evt.type) {
            case this._focusMode:
                this.active = this._adapter.isActive(this._allowFocus ? ':focus' : ':focus-visible', target);
                break;
            case 'focusout':
                this.active = false;
                break;
            case 'pointerdown':
                this.active = this._allowFocus ? this._adapter.isActive(':focus', target) : this._adapter.isActive(':focus-visible', target);
                break;
        }
    }
    get targetElement() {
        return this._adapter.getTargetElement();
    }
    set targetElement(value) {
        this._removeListeners();
        this._adapter.setTargetElement(value);
        this._addListeners();
    }
    get target() {
        return this._adapter.getHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.TARGET);
    }
    set target(value) {
        if (this._target !== value) {
            this._target = value;
            if (this._adapter.isConnected) {
                this._adapter.trySetTarget(value);
            }
            this._adapter.toggleHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.TARGET, Boolean(this._target), this._target);
        }
    }
    get active() {
        return this._active;
    }
    set active(value) {
        value = Boolean(value);
        if (this._active !== value) {
            this._active = value;
            this._adapter.toggleHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.ACTIVE, this._active);
        }
    }
    get inward() {
        return this._inward;
    }
    set inward(value) {
        value = Boolean(value);
        if (this._inward !== value) {
            this._inward = value;
            this._adapter.toggleHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.INWARD, this._inward);
        }
    }
    get circular() {
        return this._circular;
    }
    set circular(value) {
        value = Boolean(value);
        if (this._circular !== value) {
            this._circular = value;
            this._adapter.toggleHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.CIRCULAR, this._circular);
        }
    }
    get allowFocus() {
        return this._allowFocus;
    }
    set allowFocus(value) {
        value = Boolean(value);
        if (this._allowFocus !== value) {
            this._allowFocus = value;
            this._adapter.toggleHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.ALLOW_FOCUS, this._allowFocus);
        }
    }
    get focusMode() {
        return this._focusMode;
    }
    set focusMode(value) {
        value !== null && value !== void 0 ? value : (value = FOCUS_INDICATOR_CONSTANTS.defaults.FOCUS_MODE);
        if (this._focusMode !== value) {
            if (this._adapter.isConnected) {
                this._removeListeners();
            }
            this._focusMode = value;
            if (this._adapter.isConnected) {
                this._addListeners();
            }
            const hasFocusMode = this._focusMode !== FOCUS_INDICATOR_CONSTANTS.defaults.FOCUS_MODE;
            this._adapter.toggleHostAttribute(FOCUS_INDICATOR_CONSTANTS.attributes.FOCUS_MODE, hasFocusMode, this._focusMode);
        }
    }
}
