/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { randomChars } from '@tylertech/forge-core';
import { BaseAdapter } from '../../core';
import { ICON_BUTTON_CONSTANTS } from '../../icon-button';
import { TEXT_FIELD_CONSTANTS } from '../../text-field';
import { BASE_DATE_PICKER_CONSTANTS } from './base-date-picker-constants';
import { createToggleElement } from './base-date-picker-utils';
export class BaseDatePickerAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._valueChangeListeners = [];
        this._identifier = randomChars();
    }
    initialize() {
        this._initializeInput();
    }
    destroy() {
        var _a;
        (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.destroy();
        this.destroyValueChangeListener();
    }
    destroyValueChangeListener() {
        this._valueChangeListeners.forEach(cb => cb());
    }
    addToggleListener(type, listener) {
        var _a;
        (_a = this._toggleElement) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener);
    }
    removeToggleListener(type, listener) {
        var _a;
        (_a = this._toggleElement) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener);
    }
    attachCalendar(calendarConfig, dropdownConfig) {
        var _a, _b, _c;
        (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.destroy();
        this._initializeCalendarDropdown();
        if (!this._calendarDropdown) {
            throw new Error('CalendarDropdown was not initialized.');
        }
        if (dropdownConfig) {
            Object.assign(this._calendarDropdown, dropdownConfig);
        }
        this._calendarDropdown.open(calendarConfig);
        (_b = this._calendarDropdown.dropdownElement) === null || _b === void 0 ? void 0 : _b.style.setProperty('--forge-calendar-width', '320px');
        (_c = this._calendarDropdown.calendar) === null || _c === void 0 ? void 0 : _c.style.setProperty('margin', '8px');
    }
    detachCalendar() {
        if (this._calendarDropdown && this._calendarDropdown.isOpen) {
            this._calendarDropdown.close();
        }
    }
    goToCalendarDate(date) {
        var _a, _b;
        (_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.goToDate(date, true);
    }
    addCalendarListener(type, listener) {
        var _a, _b;
        (_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.addEventListener(type, listener);
    }
    removeCalendarListener(type, listener) {
        var _a, _b;
        (_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.removeEventListener(type, listener);
    }
    setCalendarValue(value) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.calendar.value = value;
        }
    }
    setCalendarMinDate(value) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.calendar.min = value;
        }
    }
    setCalendarMaxDate(value) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.calendar.max = value;
        }
    }
    setCalendarDisabledDates(value) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.calendar.disabledDates = value;
        }
    }
    setCalendarDisabledDaysOfWeek(value) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.calendar.disabledDaysOfWeek = value;
        }
    }
    setCalendarDisableDayCallback(disableDayCallback) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.calendar.disabledDateBuilder = disableDayCallback;
        }
    }
    setCalendarActiveDate(date) {
        var _a, _b;
        (_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.setActiveDate(date);
    }
    getCalendarActiveDate() {
        var _a, _b;
        return (_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.activeDate;
    }
    setCalendarYearRange(value) {
        var _a, _b;
        if ((_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.yearRange) {
            this._calendarDropdown.calendar.yearRange = value;
        }
    }
    setCalendarLocale(locale) {
        var _a;
        if ((_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) {
            this._calendarDropdown.locale = locale;
        }
    }
    propagateCalendarKey(evt) {
        var _a, _b;
        (_b = (_a = this._calendarDropdown) === null || _a === void 0 ? void 0 : _a.calendar) === null || _b === void 0 ? void 0 : _b.handleKey(evt);
    }
    tryCreateToggle() {
        const textField = this._component.querySelector('forge-text-field');
        const toggleElement = this._component.querySelector(BASE_DATE_PICKER_CONSTANTS.selectors.TOGGLE);
        if (textField) {
            const existingIconButton = textField.querySelector(`${ICON_BUTTON_CONSTANTS.elementName}[slot=end]`);
            if (existingIconButton || toggleElement) {
                this._toggleElement = (existingIconButton || toggleElement);
                return;
            }
            const iconButtonElement = this._createToggleElement();
            textField.appendChild(iconButtonElement);
            this._toggleElement = iconButtonElement;
        }
        else if (toggleElement) {
            this._toggleElement = toggleElement;
        }
    }
    _createToggleElement() {
        return createToggleElement('insert_invitation');
    }
    _getDefaultTargetElement() {
        // This component is often used with the Forge text-field, if so, let's target our popup around
        // one if its internal elements for best alignment
        const textField = this._component.querySelector(TEXT_FIELD_CONSTANTS.elementName);
        if (textField === null || textField === void 0 ? void 0 : textField.popoverTargetElement) {
            return textField.popoverTargetElement;
        }
        return this._component.querySelector('input') || this._component;
    }
}
