/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { emitEvent, listenOwnProperty, getActiveElement } from '@tylertech/forge-core';
import { CalendarDropdown } from '../calendar/calendar-dropdown/calendar-dropdown';
import { DateInputMask } from '../core/mask/date-input-mask';
import { BaseDatePickerAdapter } from './base/base-date-picker-adapter';
import { DATE_PICKER_CONSTANTS } from './date-picker-constants';
export class DatePickerAdapter extends BaseDatePickerAdapter {
    constructor(component) {
        super(component);
        this._dropdownIdentifier = `forge-date-picker-${this._identifier}`;
    }
    _initializeInput() {
        this._inputElement = this._component.querySelector(DATE_PICKER_CONSTANTS.selectors.INPUT);
    }
    _initializeCalendarDropdown() {
        const targetElement = this._getDefaultTargetElement();
        this._calendarDropdown = new CalendarDropdown(targetElement, this._dropdownIdentifier);
    }
    initializeMask(options) {
        var _a;
        (_a = this._inputMask) === null || _a === void 0 ? void 0 : _a.destroy();
        this._inputMask = new DateInputMask(this._inputElement, options);
    }
    destroyMask() {
        var _a;
        (_a = this._inputMask) === null || _a === void 0 ? void 0 : _a.destroy();
        this._inputMask = undefined;
    }
    initializeAccessibility() {
        this._inputElement.setAttribute('autocomplete', 'off');
        this._inputElement.setAttribute('autocorrect', 'off');
        this._inputElement.setAttribute('autocapitalize', 'off');
        this._inputElement.setAttribute('spellcheck', 'false');
        this._inputElement.setAttribute('role', 'combobox');
        this._inputElement.setAttribute('aria-live', 'assertive');
        this._inputElement.setAttribute('aria-atomic', 'true');
        this._inputElement.setAttribute('aria-haspopup', 'true');
        this._inputElement.setAttribute('aria-expanded', 'false');
        this._inputElement.setAttribute('aria-owns', this._dropdownIdentifier);
    }
    addInputListener(type, listener, capture) {
        var _a;
        (_a = this._inputElement) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener, { capture });
    }
    removeInputListener(type, listener, capture) {
        var _a;
        (_a = this._inputElement) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener, { capture });
    }
    setInputValueChangedListener(context, listener) {
        this.destroyValueChangeListener();
        const destroyListenerCb = listenOwnProperty(context, this._inputElement, 'value', listener);
        this._valueChangeListeners.push(destroyListenerCb);
    }
    hasInputElement() {
        return !!this._inputElement;
    }
    attachCalendar(calendarConfig, dropdownConfig) {
        super.attachCalendar(calendarConfig, dropdownConfig);
        this._inputElement.setAttribute('aria-expanded', 'true');
    }
    detachCalendar() {
        super.detachCalendar();
        if (this._inputElement) {
            this._inputElement.setAttribute('aria-expanded', 'false');
            this._inputElement.removeAttribute('aria-activedescendant');
        }
    }
    setActiveDescendant(id) {
        this._inputElement.setAttribute('aria-activedescendant', id);
    }
    setInputValue(value, emitEvents) {
        if (this._inputElement.value === value) {
            return;
        }
        this._inputElement.value = value;
        if (this._inputMask) {
            this._inputMask.updateMask();
        }
        if (emitEvents) {
            this._inputElement.dispatchEvent(new Event('change'));
            this._inputElement.dispatchEvent(new Event('input'));
        }
    }
    isInputDisabled() {
        return this._inputElement.disabled;
    }
    isInputFocused() {
        return getActiveElement(this._component.ownerDocument) === this._inputElement;
    }
    getInputValue() {
        return this._inputMask ? this._inputMask.maskedValue : this._inputElement.value;
    }
    setDisabled(value) {
        this._inputElement.disabled = value;
        this._inputElement.setAttribute('aria-disabled', value.toString());
        if (this._toggleElement) {
            this._toggleElement.setAttribute('aria-disabled', value.toString());
            if (this._toggleElement.hasOwnProperty('disabled')) {
                this._toggleElement.disabled = value;
            }
        }
    }
    tryFocusInput() {
        this._inputElement.focus();
    }
    tryBlurInput() {
        this._inputElement.blur();
    }
    selectInputText() {
        this._inputElement.select();
    }
    emitInputEvent(type, data) {
        emitEvent(this._inputElement, type, data);
    }
}
