'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var dayjs = require('dayjs');
var customParseFormat = require('../../../../_virtual/customParseFormat.js');
var utils = require('../start-end-date/utils.js');

dayjs.extend(customParseFormat);
const setDateValueWithinMinMax = (filter) => {
    const { defaultValue, min, max } = filter;
    let dateValue;
    if (defaultValue) {
        dateValue = defaultValue;
        if (min)
            dateValue = utils.default.compareMin(min, dateValue);
        if (max)
            dateValue = utils.default.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 && !req.query['preventDefault']) {
        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,
};

exports.default = DateInputUtils;
exports.getFilterFromDefinition = getFilterFromDefinition;
exports.setDateValueWithinMinMax = setDateValueWithinMinMax;
exports.setFilterValueFromDefault = setFilterValueFromDefault;
exports.setValueFromRequest = setValueFromRequest;
//# sourceMappingURL=utils.js.map
