"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAutocompleteLoadingByOverridingFetch = addAutocompleteLoadingByOverridingFetch;
exports.resetDatePicker = resetDatePicker;
exports.setPickerDates = setPickerDates;
const tempo_1 = require("@formkit/tempo");
const lite_1 = require("dequal/lite");
const fieldType_enum_js_1 = require("../enums/fieldType.enum.js");
const dateUtils_js_1 = require("../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
 */
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);
        };
    }
}
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,
    });
}
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_enum_js_1.FieldType.dateUtc;
        const newDates = Array.isArray(newVal) ? newVal : [(newVal || '')];
        const pickerDates = [];
        const isoFormat = (0, dateUtils_js_1.mapTempoDateFormatWithFieldType)(fieldType_enum_js_1.FieldType.dateIso);
        const inputFormat = inputFieldType ? (0, dateUtils_js_1.mapTempoDateFormatWithFieldType)(inputFieldType) : undefined;
        for (const initialDate of newDates) {
            const date = initialDate instanceof Date ? initialDate : (0, dateUtils_js_1.tryParseDate)(initialDate, inputFormat);
            if (date) {
                pickerDates.push(date);
            }
        }
        const newSettingSelected = selectedSettings ?? {
            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,
        };
        if (!(0, lite_1.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) => (0, dateUtils_js_1.formatDateByFieldType)(p, undefined, outputFieldType)).join(' — ') : '';
    }
}
//# sourceMappingURL=commonEditorFilterUtils.js.map