'use strict';

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

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

/* eslint-disable no-param-reassign */
dayjs.extend(customParseFormat);
const dateIsInBounds = (startDate, endDate, min, max) => {
    dayjs.extend(isBetween);
    const minDate = dayjs(min);
    const maxDate = dayjs(max);
    const startDateIsBetweenMinAndMax = startDate
        ? startDate.isBetween(minDate, maxDate, 'day', '[]')
        : true;
    const endDateIsBetweenMinAndMax = endDate ? endDate.isBetween(minDate, maxDate, 'day', '[]') : true;
    return startDateIsBetweenMinAndMax && endDateIsBetweenMinAndMax;
};
const calcDates = (durationValue) => {
    let endDate = dayjs();
    let startDate = dayjs();
    switch (durationValue) {
        case 'none':
            endDate = '';
            startDate = '';
            break;
        case 'yesterday':
            endDate = dayjs();
            startDate = endDate.subtract(1, 'day');
            break;
        case 'tomorrow':
            startDate = dayjs();
            endDate = startDate.add(1, 'day');
            break;
        case 'last-seven-days':
            endDate = dayjs();
            startDate = endDate.subtract(1, 'week');
            break;
        case 'next-seven-days':
            startDate = dayjs();
            endDate = startDate.add(1, 'week');
            break;
        case 'last-month':
            endDate = dayjs();
            startDate = endDate.subtract(1, 'month');
            break;
        case 'next-month':
            startDate = dayjs();
            endDate = startDate.add(1, 'month');
            break;
    }
    return {
        endDate,
        startDate,
    };
};
const setValueFromRequest = (filter, req, prefix) => {
    const { relativeOptions } = filter;
    const start = req.query[`${prefix}${filter.name}.start`];
    const end = req.query[`${prefix}${filter.name}.end`];
    const relative = req.query[`${prefix}${filter.name}.relative-duration`];
    let relativeDisabled = false;
    if (relative && relativeOptions) {
        const option = relativeOptions.find((opt) => opt.value === relative);
        relativeDisabled = option && option.disabled ? option.disabled : false;
    }
    const value = {
        start: start || filter.min,
        end: end || filter.max,
        ...(relative && !relativeDisabled && { relative }),
    };
    return value;
};
const setDefaultValue = (req, name) => {
    const dateRangeName = name.split('.')[0];
    const dateRangeDefaults = Object.keys(req.body)
        .filter((key) => key.includes(dateRangeName))
        .map((key) => {
        return { name: key.split('.')[2], value: req.body[key] };
    });
    const dateRangeValue = { start: '', end: '' };
    dateRangeDefaults.forEach((dateRangeDefault) => {
        if (dateRangeDefault.name.includes('start')) {
            dateRangeValue.start = dateRangeDefault.value;
        }
        if (dateRangeDefault.name.includes('end')) {
            dateRangeValue.end = dateRangeDefault.value;
        }
        if (dateRangeDefault.name.includes('relative-duration')) {
            dateRangeValue.relative = dateRangeDefault.value;
        }
    });
    return { value: dateRangeValue, name: dateRangeName };
};
const setFilterValueFromDefault = (defaultValue, filter) => {
    const value = { start: '', end: '', relative: '' };
    const { start, end, relative } = defaultValue.value;
    if (relative) {
        value.relative = relative;
    }
    if (start) {
        value.start = dayjs(start, 'D/M/YYYY').format('YYYY-MM-DD').toString();
    }
    if (end) {
        value.end = dayjs(end, 'D/M/YYYY').format('YYYY-MM-DD').toString();
    }
    return {
        ...filter,
        value,
    };
};
const getRelativeDateOptions = (min, max) => {
    if (!min)
        min = '1977-05-25';
    if (!max)
        max = '9999-01-01';
    let options = getRelativeValues();
    options.forEach((option) => {
        if (option.value) {
            const { endDate, startDate } = calcDates(option.value);
            if (!dateIsInBounds(startDate, endDate, min, max)) {
                option.disabled = true;
            }
        }
    });
    if (options
        .filter((opt) => opt.value !== 'none')
        .every((opt) => {
        return opt.value === null || opt.disabled;
    })) {
        options = [];
    }
    return options;
};
const mapRelativeValue = (value) => {
    const values = getRelativeValues();
    const opt = values.find((v) => v.value === value);
    return opt ? opt.text : '';
};
const getRelativeValues = () => {
    return [
        { value: 'none', text: 'None' },
        { value: 'yesterday', text: 'Yesterday' },
        { value: 'tomorrow', text: 'Tomorrow' },
        { value: 'last-seven-days', text: 'Last week' },
        { value: 'next-seven-days', text: 'Next week' },
        { value: 'last-month', text: 'Last month' },
        { value: 'next-month', text: 'Next month' },
    ];
};
const getFilterFromDefinition = (filter, filterData) => {
    const value = utils.default.getStartAndEndValueFromDefinition(filter);
    const relativeOptions = getRelativeDateOptions(filter.min, filter.max);
    return {
        ...filterData,
        min: filter.min,
        max: filter.max,
        relativeOptions,
        value,
    };
};
const getQueryFromDefinition = (filter, name, filterPrefix) => {
    const dates = filter.defaultValue ? filter.defaultValue.split(' - ') : [];
    let param = '';
    if (dates.length >= 1) {
        param = `${filterPrefix}${name}.start=${dates[0]}`;
        if (dates.length >= 2) {
            param = `${param}&${filterPrefix}${name}.end=${dates[1]}`;
        }
    }
    return param;
};
var DateRangeInputUtils = {
    calcDates,
    getRelativeDateOptions,
    getFilterFromDefinition,
    setValueFromRequest,
    getQueryFromDefinition,
    mapRelativeValue,
    setDefaultValue,
    setFilterValueFromDefault,
};

exports.calcDates = calcDates;
exports.dateIsInBounds = dateIsInBounds;
exports.default = DateRangeInputUtils;
exports.getFilterFromDefinition = getFilterFromDefinition;
exports.getQueryFromDefinition = getQueryFromDefinition;
exports.getRelativeDateOptions = getRelativeDateOptions;
exports.getRelativeValues = getRelativeValues;
exports.mapRelativeValue = mapRelativeValue;
exports.setDefaultValue = setDefaultValue;
exports.setFilterValueFromDefault = setFilterValueFromDefault;
exports.setValueFromRequest = setValueFromRequest;
//# sourceMappingURL=utils.js.map
