import dayjs from 'dayjs';
import customParse from '../../../../_virtual/customParseFormat.js';
import StartEndDateUtils from '../start-end-date/utils.js';

dayjs.extend(customParse);
const setDateValueWithinMinMax = (filter) => {
    const { defaultValue, min, max } = filter;
    let dateValue;
    if (defaultValue) {
        dateValue = defaultValue;
        if (min)
            dateValue = StartEndDateUtils.compareMin(min, dateValue);
        if (max)
            dateValue = StartEndDateUtils.compareMax(max, dateValue);
    }
    return dateValue;
};
const setValueFromRequest = (filter, req, prefix) => {
    const { min, max, mandatory } = filter;
    const dateValue = req.query[`${prefix}${filter.name}`];
    let value = dateValue;
    if (mandatory && !dateValue) {
        value = min || max || '1977-05-25';
    }
    return value;
};
const setFilterValueFromDefault = (defaultValue, filter) => {
    const value = defaultValue.value
        ? dayjs(defaultValue.value, 'D/M/YYYY')
            .format('YYYY-MM-DD')
            .toString()
        : '';
    return {
        ...filter,
        value,
    };
};
const getFilterFromDefinition = (filter, filterData) => {
    return {
        ...filterData,
        value: setDateValueWithinMinMax(filter) || '',
        min: filter.min,
        max: filter.max,
    };
};
var DateInputUtils = {
    setDateValueWithinMinMax,
    getFilterFromDefinition,
    setValueFromRequest,
    setFilterValueFromDefault,
};

export { DateInputUtils as default, getFilterFromDefinition, setDateValueWithinMinMax, setFilterValueFromDefault, setValueFromRequest };
//# sourceMappingURL=utils.js.map
