"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setPickerDates = exports.addAutocompleteLoadingByOverridingFetch = void 0;
const tempo_1 = require("@formkit/tempo");
const enums_1 = require("../enums");
const dateUtils_1 = require("../services/dateUtils");
/**
 * 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
 */
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);
        };
    }
}
exports.addAutocompleteLoadingByOverridingFetch = addAutocompleteLoadingByOverridingFetch;
function setPickerDates(dateInputElm, pickerOptions, dateValues, columnDef, colEditorOrFilter) {
    const currentDateOrDates = dateValues;
    const outputFieldType = columnDef.outputType || colEditorOrFilter.type || columnDef.type || enums_1.FieldType.dateUtc;
    const inputFieldType = colEditorOrFilter.type || columnDef.type;
    const isoFormat = (0, dateUtils_1.mapTempoDateFormatWithFieldType)(enums_1.FieldType.dateIso);
    const inputFormat = inputFieldType ? (0, dateUtils_1.mapTempoDateFormatWithFieldType)(inputFieldType) : undefined;
    const initialDates = Array.isArray(currentDateOrDates) ? currentDateOrDates : [(currentDateOrDates || '')];
    if (initialDates.length && initialDates[0]) {
        const pickerDates = [];
        for (const initialDate of initialDates) {
            const date = initialDate instanceof Date ? initialDate : (0, dateUtils_1.tryParseDate)(initialDate, inputFormat);
            if (date) {
                pickerDates.push(date);
            }
        }
        if (pickerDates.length) {
            pickerOptions.settings.selected = {
                dates: [pickerDates.map(p => (0, tempo_1.format)(p, isoFormat)).join(':')],
                month: pickerDates[0].getMonth(),
                year: pickerDates[0].getFullYear(),
                time: inputFormat === 'ISO8601' || (inputFormat || '').toLowerCase().includes('h') ? (0, tempo_1.format)(pickerDates[0], 'HH:mm') : undefined,
            };
        }
        dateInputElm.value = initialDates.length ? pickerDates.map(p => (0, dateUtils_1.formatDateByFieldType)(p, undefined, outputFieldType)).join(' — ') : '';
    }
}
exports.setPickerDates = setPickerDates;
//# sourceMappingURL=commonEditorFilterUtils.js.map