/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { emitEvent, listenOwnProperty, getActiveElement, toggleAttribute } from '@tylertech/forge-core';
import { CalendarDropdown } from '../calendar/calendar-dropdown';
import { DateInputMask } from '../core';
import { BaseDatePickerAdapter } from '../date-picker/base/base-date-picker-adapter';
import { createToggleElement } from '../date-picker/base/base-date-picker-utils';
import { FIELD_CONSTANTS } from '../field';
import { DATE_RANGE_PICKER_CONSTANTS } from './date-range-picker-constants';
export class DateRangePickerAdapter extends BaseDatePickerAdapter {
    constructor(component) {
        super(component);
        this._dropdownIdentifier = `forge-date-range-picker-${this._identifier}`;
    }
    _initializeInput() {
        this._fromInputElement = this._component.querySelectorAll(DATE_RANGE_PICKER_CONSTANTS.selectors.INPUT)[0];
        this._toInputElement = this._component.querySelectorAll(DATE_RANGE_PICKER_CONSTANTS.selectors.INPUT)[1];
        if (!this._fromInputElement || !this._fromInputElement) {
            throw new Error(`The ${DATE_RANGE_PICKER_CONSTANTS.elementName} requires two inputs`);
        }
        const separator = this._createInputSeparator();
        this._fromInputElement.insertAdjacentElement('afterend', separator);
    }
    _initializeCalendarDropdown() {
        const targetElement = this._getDefaultTargetElement();
        this._calendarDropdown = new CalendarDropdown(targetElement, this._dropdownIdentifier);
    }
    initializeMask(fromOptions) {
        var _a;
        (_a = this._fromInputMask) === null || _a === void 0 ? void 0 : _a.destroy();
        this._fromInputMask = new DateInputMask(this._fromInputElement, fromOptions);
    }
    destroyMask() {
        var _a;
        (_a = this._fromInputMask) === null || _a === void 0 ? void 0 : _a.destroy();
        this._fromInputMask = undefined;
    }
    initializeToMask(toOptions) {
        var _a;
        (_a = this._toInputMask) === null || _a === void 0 ? void 0 : _a.destroy();
        this._toInputMask = new DateInputMask(this._toInputElement, toOptions);
    }
    destroyToMask() {
        var _a;
        (_a = this._toInputMask) === null || _a === void 0 ? void 0 : _a.destroy();
        this._toInputMask = undefined;
    }
    destroy() {
        super.destroy();
        this._destroyToValueChangeListener();
    }
    _destroyToValueChangeListener() {
        if (typeof this._toValueChangeListener === 'function') {
            this._toValueChangeListener();
        }
    }
    initializeAccessibility() {
        this._applyToInputs(input => input.setAttribute('autocomplete', 'off'));
        this._applyToInputs(input => input.setAttribute('autocorrect', 'off'));
        this._applyToInputs(input => input.setAttribute('autocapitalize', 'off'));
        this._applyToInputs(input => input.setAttribute('spellcheck', 'false'));
        this._applyToInputs(input => input.setAttribute('role', 'combobox'));
        this._applyToInputs(input => input.setAttribute('aria-live', 'assertive'));
        this._applyToInputs(input => input.setAttribute('aria-atomic', 'true'));
        this._applyToInputs(input => input.setAttribute('aria-haspopup', 'true'));
        this._applyToInputs(input => input.setAttribute('aria-expanded', 'false'));
        this._applyToInputs(input => input.setAttribute('aria-owns', this._dropdownIdentifier));
    }
    addInputListener(type, listener, capture) {
        var _a;
        (_a = this._fromInputElement) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener, { capture });
    }
    addToInputListener(type, listener, capture) {
        var _a;
        (_a = this._toInputElement) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener, { capture });
    }
    removeInputListener(type, listener) {
        var _a;
        (_a = this._fromInputElement) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener);
    }
    removeToInputListener(type, listener) {
        var _a;
        (_a = this._toInputElement) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener);
    }
    setInputValueChangedListener(context, listener) {
        if (this._valueChangeListeners.length) {
            this.destroyValueChangeListener();
        }
        const destroyListenerCb = listenOwnProperty(context, this._fromInputElement, 'value', listener);
        this._valueChangeListeners.push(destroyListenerCb);
    }
    setToInputValueChangedListener(context, listener) {
        this._destroyToValueChangeListener();
        this._toValueChangeListener = listenOwnProperty(context, this._toInputElement, 'value', listener);
    }
    hasInputElement() {
        return !!this._fromInputElement && !!this._toInputElement;
    }
    attachCalendar(calendarConfig, dropdownConfig) {
        super.attachCalendar(calendarConfig, dropdownConfig);
        this._fromInputElement.setAttribute('aria-expanded', 'true');
    }
    detachCalendar() {
        super.detachCalendar();
        if (this._fromInputElement) {
            this._fromInputElement.setAttribute('aria-expanded', 'false');
            this._fromInputElement.removeAttribute('aria-activedescendant');
        }
    }
    setActiveDescendant(id) {
        this._fromInputElement.setAttribute('aria-activedescendant', id);
    }
    setInputValue(value, emitEvents) {
        if (this._fromInputElement.value === value) {
            return;
        }
        this._fromInputElement.value = value;
        if (this._fromInputMask) {
            this._fromInputMask.updateMask();
        }
        if (emitEvents) {
            this._fromInputElement.dispatchEvent(new Event('change'));
            this._fromInputElement.dispatchEvent(new Event('input'));
            ;
        }
    }
    setToInputValue(value, emitEvents) {
        if (this._toInputElement.value === value) {
            return;
        }
        this._toInputElement.value = value;
        if (this._toInputMask) {
            this._toInputMask.updateMask();
        }
        if (emitEvents) {
            this._toInputElement.dispatchEvent(new Event('change'));
            this._toInputElement.dispatchEvent(new Event('input'));
        }
    }
    isInputDisabled() {
        return this._fromInputElement.disabled;
    }
    isInputFocused(target) {
        if (target && this._toInputElement === target || this._fromInputElement === target) {
            return true;
        }
        const activeEl = getActiveElement(this._component.ownerDocument);
        return this._toInputElement === activeEl || this._fromInputElement === activeEl;
    }
    getInputValue() {
        return this._fromInputElement.value;
    }
    getToInputValue() {
        return this._toInputElement.value;
    }
    setDisabled(isDisabled) {
        this._fromInputElement.disabled = isDisabled;
        this._toInputElement.disabled = isDisabled;
        this._toInputElement.setAttribute('aria-disabled', isDisabled.toString());
        this._fromInputElement.setAttribute('aria-disabled', isDisabled.toString());
        if (this._toggleElement) {
            this._toggleElement.setAttribute('aria-disabled', isDisabled.toString());
            if (this._toggleElement.hasOwnProperty('disabled')) {
                this._toggleElement.disabled = isDisabled;
            }
        }
    }
    emitInputEvent(type, data) {
        emitEvent(this._fromInputElement, type, data);
    }
    emitToInputEvent(type, data) {
        emitEvent(this._toInputElement, type, data);
    }
    _createToggleElement() {
        return createToggleElement('date_range');
    }
    tryFocusInput() {
        this._fromInputElement.focus();
    }
    tryBlurInput() {
        this._fromInputElement.blur();
    }
    selectInputText() {
        this._fromInputElement.select();
    }
    selectToInputText() {
        this._toInputElement.select();
    }
    _applyToInputs(action) {
        [this._fromInputElement, this._toInputElement].forEach(action);
    }
    _createInputSeparator() {
        const separator = document.createElement('span');
        toggleAttribute(separator, true, FIELD_CONSTANTS.attributes.MULTI_INPUT_SEPARATOR);
        separator.setAttribute('aria-hidden', 'true');
        separator.textContent = '-';
        return separator;
    }
}
