/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { FLOATING_LABEL_CONSTANTS } from './floating-label-constants';
import { Platform } from '@tylertech/forge-core';
export class FloatingLabelFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._floatAnimationEndHandler = () => this._handleFloatAnimationEnd();
    }
    initialize() {
        this._adapter.addLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOATING_LABEL);
        if (Platform.BLINK) {
            // Chromium has a bug where the label renders blurry due to a scale transform
            // so we only provide a fix for that when the Blink engine is used
            this._adapter.addLabelListener('transitionend', this._floatAnimationEndHandler);
        }
    }
    disconnect({ cancelFloat = false } = {}) {
        if (cancelFloat && this._activeFrame) {
            window.cancelAnimationFrame(this._activeFrame);
        }
        this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOATING_LABEL);
        this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE);
        this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE_END_KEYFRAME);
        this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME);
        if (this._floatAnimationEndHandler) {
            this._adapter.removeLabelListener('transitionend', this._floatAnimationEndHandler);
        }
    }
    float(shouldFloat, alwaysFloat = false) {
        if (shouldFloat) {
            if (this._adapter.hasLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME)) {
                this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME);
            }
            this._activeFrame = window.requestAnimationFrame(() => {
                this._activeFrame = window.requestAnimationFrame(() => {
                    if (alwaysFloat || this.isFloating) {
                        this._adapter.addLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE_END_KEYFRAME);
                    }
                    this._adapter.addLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE);
                });
            });
        }
        else {
            this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE);
            if (this._adapter.hasLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE_END_KEYFRAME)) {
                this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE_END_KEYFRAME);
                this._adapter.addLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME);
                this._activeFrame = window.requestAnimationFrame(() => {
                    this._activeFrame = window.requestAnimationFrame(() => {
                        this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME);
                    });
                });
            }
        }
    }
    getWidth() {
        return this._adapter.getWidth();
    }
    get isFloating() {
        return this._adapter.hasLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE);
    }
    _handleFloatAnimationEnd() {
        if (this._adapter.hasLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE) && !this._adapter.hasLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE_END_KEYFRAME)) {
            this._adapter.addLabelClass(FLOATING_LABEL_CONSTANTS.classes.FLOAT_ABOVE_END_KEYFRAME);
        }
        else if (this._adapter.hasLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME)) {
            this._adapter.removeLabelClass(FLOATING_LABEL_CONSTANTS.classes.UNFLOAT_ABOVE_START_KEYFRAME);
        }
    }
}
