import { format } from '@formkit/tempo';
import { dequal } from 'dequal/lite';
import { FieldType } from '../enums/fieldType.enum.js';
import { formatDateByFieldType, mapTempoDateFormatWithFieldType, tryParseDate } from '../services/dateUtils.js';
/**
 * add loading class ".slick-autocomplete-loading" to the Kraaden Autocomplete input element
 * by overriding the original user's fetch method.
 * We will add the loading class when the fetch starts and later remove it when the update callback is being called.
 * @param inputElm - autocomplete input element
 * @param autocompleterOptions - autocomplete settings
 */
export function addAutocompleteLoadingByOverridingFetch(inputElm, autocompleterOptions) {
    const previousFetch = autocompleterOptions.fetch;
    if (previousFetch) {
        autocompleterOptions.fetch = (searchTerm, updateCallback, trigger, cursorPos) => {
            // add loading class
            inputElm.classList.add('slick-autocomplete-loading');
            const previousCallback = updateCallback;
            const newUpdateCallback = (items) => {
                previousCallback(items);
                // we're done, time to remove loading class
                inputElm.classList.remove('slick-autocomplete-loading');
            };
            // call original fetch implementation
            previousFetch(searchTerm, newUpdateCallback, trigger, cursorPos);
        };
    }
}
export function resetDatePicker(pickerInstance) {
    const today = new Date();
    pickerInstance.settings.selected = {
        dates: [],
        month: today.getMonth(),
        year: today.getFullYear(),
    };
    const dateInputElm = pickerInstance.HTMLInputElement;
    if (dateInputElm) {
        dateInputElm.value = '';
    }
    pickerInstance.update({
        dates: true,
        month: true,
        year: true,
        time: true,
    });
}
export function setPickerDates(colEditorOrFilter, dateInputElm, pickerInstance, options) {
    const { oldVal, newVal, columnDef, selectedSettings, updatePickerUI } = options;
    if (oldVal !== newVal) {
        const inputFieldType = colEditorOrFilter.type || columnDef.type;
        const outputFieldType = columnDef.outputType || colEditorOrFilter.type || columnDef.type || FieldType.dateUtc;
        const newDates = Array.isArray(newVal) ? newVal : [(newVal || '')];
        const pickerDates = [];
        const isoFormat = mapTempoDateFormatWithFieldType(FieldType.dateIso);
        const inputFormat = inputFieldType ? mapTempoDateFormatWithFieldType(inputFieldType) : undefined;
        for (const initialDate of newDates) {
            const date = initialDate instanceof Date ? initialDate : tryParseDate(initialDate, inputFormat);
            if (date) {
                pickerDates.push(date);
            }
        }
        const newSettingSelected = selectedSettings ?? {
            dates: [pickerDates.map((p) => format(p, isoFormat)).join(':')],
            month: pickerDates[0]?.getMonth(),
            year: pickerDates[0]?.getFullYear(),
            time: inputFormat === 'ISO8601' || (inputFormat || '').toLowerCase().includes('h') ? format(pickerDates[0], 'HH:mm') : undefined,
        };
        if (!dequal(pickerInstance.settings.selected, newSettingSelected)) {
            pickerInstance.settings.selected = newSettingSelected;
            if (updatePickerUI && pickerInstance?.update) {
                pickerInstance.update({
                    dates: true,
                    month: true,
                    year: true,
                    time: true,
                });
            }
        }
        dateInputElm.value = newDates.length ? pickerDates.map((p) => formatDateByFieldType(p, undefined, outputFieldType)).join(' — ') : '';
    }
}
//# sourceMappingURL=commonEditorFilterUtils.js.map