/**
 * @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() {
        this._calendarDropdown?.destroy();
        this.destroyValueChangeListener();
    }
    destroyValueChangeListener() {
        this._valueChangeListeners.forEach(cb => cb());
    }
    addToggleListener(type, listener) {
        this._toggleElement?.addEventListener(type, listener);
    }
    removeToggleListener(type, listener) {
        this._toggleElement?.removeEventListener(type, listener);
    }
    attachCalendar(calendarConfig, dropdownConfig) {
        this._calendarDropdown?.destroy();
        this._initializeCalendarDropdown();
        if (!this._calendarDropdown) {
            throw new Error('CalendarDropdown was not initialized.');
        }
        if (dropdownConfig) {
            Object.assign(this._calendarDropdown, dropdownConfig);
        }
        this._calendarDropdown.open(calendarConfig);
        this._calendarDropdown.dropdownElement?.style.setProperty('--forge-calendar-width', '320px');
        this._calendarDropdown.calendar?.style.setProperty('margin', '8px');
    }
    detachCalendar() {
        if (this._calendarDropdown && this._calendarDropdown.isOpen) {
            this._calendarDropdown.close();
        }
    }
    goToCalendarDate(date) {
        this._calendarDropdown?.calendar?.goToDate(date, true);
    }
    addCalendarListener(type, listener) {
        this._calendarDropdown?.calendar?.addEventListener(type, listener);
    }
    removeCalendarListener(type, listener) {
        this._calendarDropdown?.calendar?.removeEventListener(type, listener);
    }
    setCalendarValue(value) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.calendar.value = value;
        }
    }
    setCalendarMinDate(value) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.calendar.min = value;
        }
    }
    setCalendarMaxDate(value) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.calendar.max = value;
        }
    }
    setCalendarDisabledDates(value) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.calendar.disabledDates = value;
        }
    }
    setCalendarDisabledDaysOfWeek(value) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.calendar.disabledDaysOfWeek = value;
        }
    }
    setCalendarDisableDayCallback(disableDayCallback) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.calendar.disabledDateBuilder = disableDayCallback;
        }
    }
    setCalendarActiveDate(date) {
        this._calendarDropdown?.calendar?.setActiveDate(date);
    }
    getCalendarActiveDate() {
        return this._calendarDropdown?.calendar?.activeDate;
    }
    setCalendarYearRange(value) {
        if (this._calendarDropdown?.calendar?.yearRange) {
            this._calendarDropdown.calendar.yearRange = value;
        }
    }
    setCalendarLocale(locale) {
        if (this._calendarDropdown?.calendar) {
            this._calendarDropdown.locale = locale;
        }
    }
    propagateCalendarKey(evt) {
        this._calendarDropdown?.calendar?.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?.popoverTargetElement) {
            return textField.popoverTargetElement;
        }
        return this._component.querySelector('input') || this._component;
    }
}
