/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { addClass, listenOwnProperty, getActiveElement, createElementAttributeObserver } from '@tylertech/forge-core';
import { FieldAdapter } from '../field/field-adapter';
import { TEXT_FIELD_CONSTANTS } from './text-field-constants';
import { FIELD_CONSTANTS } from '../field/field-constants';
export class TextFieldAdapter extends FieldAdapter {
    constructor(component) {
        super(component);
        this._inputElements = [];
        this._inputMutationObserverInstances = [];
    }
    initialize() {
        super.initialize(TEXT_FIELD_CONSTANTS.selectors.ROOT);
        this._inputElements = Array.from(this._component.querySelectorAll('input:not([type=checkbox]):not([type=radio]), textarea'));
        if (this._inputElements.length > 1) {
            this._handleMultiInputs();
        }
    }
    destroy() {
        super.destroy();
        if (this._inputMutationObserverInstances) {
            this._inputMutationObserverInstances.forEach(mo => mo.disconnect());
        }
    }
    detectTextarea() {
        this._applyToInputs(input => {
            if (input instanceof HTMLTextAreaElement) {
                this._rootElement.classList.add(TEXT_FIELD_CONSTANTS.classes.TEXTAREA);
            }
            else {
                this._rootElement.classList.remove(TEXT_FIELD_CONSTANTS.classes.TEXTAREA);
            }
        });
    }
    addInputListener(type, listener) {
        this._applyToInputs(input => input.addEventListener(type, listener));
    }
    removeInputListener(type, listener) {
        if (this._inputElements && this._inputElements.length > 0) {
            this._applyToInputs(input => input.removeEventListener(type, listener));
        }
    }
    setValueChangedListener(context, listener) {
        this.destroyValueChangeListener();
        this._applyToInputs(input => {
            const destroyListener = listenOwnProperty(context, input, 'value', listener);
            this._valueChangeListeners.push(destroyListener);
        });
    }
    inputHasValue() {
        return this._inputsSome(input => input.value ? input.value.trim().length > 0 : false);
    }
    hasPlaceholder() {
        return this._inputsSome(input => input.placeholder ? input.placeholder.trim().length > 0 : false);
    }
    inputHasFocus(target) {
        const activeElement = getActiveElement(this._component.ownerDocument);
        return this._inputsSome(input => input === target || input === activeElement);
    }
    setInputClass(className) {
        this._applyToInputs(input => input.classList.add(className));
    }
    removeInputClass(className) {
        this._applyToInputs(input => input.classList.remove(className));
    }
    setInputAttributeObserver(listener) {
        this._applyToInputs(input => {
            const observer = createElementAttributeObserver(input, listener, FIELD_CONSTANTS.observedInputAttributes);
            this._inputMutationObserverInstances.push(observer);
        });
    }
    isDisabled() {
        return this._inputsSome(input => input.hasAttribute('disabled'));
    }
    isReadonly() {
        return this._inputsSome(input => input.hasAttribute('readonly'));
    }
    _inputsSome(action) {
        return this._inputElements.some(action);
    }
    _applyToInputs(action) {
        this._inputElements.forEach(action);
    }
    _handleMultiInputs() {
        addClass(TEXT_FIELD_CONSTANTS.classes.MULTI_INPUT, this._rootElement);
        this._applyToInputs((input, index) => {
            input.setAttribute(`${TEXT_FIELD_CONSTANTS.attributes.MULTI_INPUT}-${index}`, '');
            if (index % 2 !== 1) {
                Promise.resolve().then(() => input.insertAdjacentElement('afterend', this._createSeperatorElement()));
            }
        });
    }
    _createSeperatorElement() {
        const divider = document.createElement('span');
        divider.setAttribute(TEXT_FIELD_CONSTANTS.attributes.MULTI_INPUT_SEPARATOR, '');
        divider.textContent = '-';
        return divider;
    }
}
