/*!
FullCalendar v5.2.0
Docs & License: https://fullcalendar.io/
(c) 2020 Adam Shaw
*/
import { __assign } from 'tslib';
import { rrulestr, RRule } from 'rrule';
import { identity, createDuration, createPlugin } from '@fullcalendar/common';

var RRULE_EVENT_REFINERS = {
    rrule: identity,
    duration: createDuration
};

var recurring = {
    parse: function (refined, dateEnv) {
        if (refined.rrule != null) {
            var parsed = parseRRule(refined.rrule, dateEnv);
            if (parsed) {
                return {
                    typeData: parsed.rrule,
                    allDayGuess: parsed.allDayGuess,
                    duration: refined.duration
                };
            }
        }
        return null;
    },
    expand: function (rrule, framingRange) {
        // we WANT an inclusive start and in exclusive end, but the js rrule lib will only do either BOTH
        // inclusive or BOTH exclusive, which is stupid: https://github.com/jakubroztocil/rrule/issues/84
        // Workaround: make inclusive, which will generate extra occurences, and then trim.
        return rrule.between(framingRange.start, framingRange.end, true)
            .filter(function (date) { return date.valueOf() < framingRange.end.valueOf(); });
    }
};
var main = createPlugin({
    recurringTypes: [recurring],
    eventRefiners: RRULE_EVENT_REFINERS
});
function parseRRule(input, dateEnv) {
    var allDayGuess = null;
    var rrule;
    if (typeof input === 'string') {
        var preparseData = preparseRRuleStr(input, dateEnv);
        rrule = rrulestr(preparseData.outStr);
        allDayGuess = preparseData.isTimeUnspecified;
    }
    else if (typeof input === 'object' && input) { // non-null object
        var refined = __assign({}, input); // copy
        if (typeof refined.dtstart === 'string') {
            var dtstartMeta = dateEnv.createMarkerMeta(refined.dtstart);
            if (dtstartMeta) {
                refined.dtstart = dtstartMeta.marker;
                allDayGuess = dtstartMeta.isTimeUnspecified;
            }
            else {
                delete refined.dtstart;
            }
        }
        if (typeof refined.until === 'string') {
            refined.until = dateEnv.createMarker(refined.until);
        }
        if (refined.freq != null) {
            refined.freq = convertConstant(refined.freq);
        }
        if (refined.wkst != null) {
            refined.wkst = convertConstant(refined.wkst);
        }
        else {
            refined.wkst = (dateEnv.weekDow - 1 + 7) % 7; // convert Sunday-first to Monday-first
        }
        if (refined.byweekday != null) {
            refined.byweekday = convertConstants(refined.byweekday); // the plural version
        }
        rrule = new RRule(refined);
    }
    if (rrule) {
        return { rrule: rrule, allDayGuess: allDayGuess };
    }
    return null;
}
function preparseRRuleStr(str, dateEnv) {
    var isTimeUnspecified = null;
    function processAndReplace(whole, introPart, datePart) {
        var res = dateEnv.parse(datePart);
        if (res) {
            if (res.isTimeUnspecified) {
                isTimeUnspecified = true;
            }
            return introPart + formatRRuleDate(res.marker);
        }
        else {
            return whole;
        }
    }
    str = str.replace(/\b(DTSTART:)([^\n]*)/, processAndReplace);
    str = str.replace(/\b(UNTIL=)([^;]*)/, processAndReplace);
    return { outStr: str, isTimeUnspecified: isTimeUnspecified };
}
function formatRRuleDate(date) {
    return date.toISOString().replace(/[-:]/g, '').replace('.000', '');
}
function convertConstants(input) {
    if (Array.isArray(input)) {
        return input.map(convertConstant);
    }
    return convertConstant(input);
}
function convertConstant(input) {
    if (typeof input === 'string') {
        return RRule[input.toUpperCase()];
    }
    return input;
}

export default main;
//# sourceMappingURL=main.js.map
