jQuery(function($) {
//---------------------------------------------------------------------------------
window.TerminKalender = window.TerminKalender || {};
window.terminkalender = window.terminkalender || {};  // legacy before 1.2.4
//---------------------------------------------------------------------------------
window.TerminKalender.check_date =  function(){
    const INPUT_FORMAT = 'YYYY-MM-DDTHH:mm';
    const factors = { 'WEEKLY': 'weeks', 'MONTHLY': 'months', 'YEARLY': 'years' };
    const allDay = $('#event-allday').prop('checked');
    const recurring = $('#event-recurring').val();
    const date_length = factors[recurring] || 'years';
    // Parse dates
    const startDate = moment.utc($.trim($('#event-start').val()), INPUT_FORMAT);
    let endDate = moment.utc($.trim($('#event-end').val()), INPUT_FORMAT);
    let dtstartDate = moment.utc($.trim($('#event-dtstart').val()), INPUT_FORMAT);
    let untilDate = moment.utc($.trim($('#event-until').val()), INPUT_FORMAT);
    // Fallback to event extended props
    if (!dtstartDate.isValid() && event?.extendedProps?.dtstart) {
        dtstartDate = moment.utc(event.extendedProps.dtstart);
    }
    if (!untilDate.isValid() && event?.extendedProps?.until) {
        untilDate = moment.utc(event.extendedProps.until);
    }
    if (!recurring) {
    // Non-recurring: ensure valid end date
        if (!endDate.isValid()) endDate = untilDate.isValid() ? untilDate : startDate.clone();
        // Prevent end date before start date
        if (endDate.isBefore(startDate)) {
            endDate = startDate.clone();
        }
        // For non-allDay events, ensure minimum duration
        if (!allDay && endDate.isSameOrBefore(startDate)) {
            endDate = startDate.clone().add(12, 'hours');
            //if (sessionStorage.getItem( 'original_end')){$('#event-end').val(sessionStorage.getItem( 'original_end'));};
        }
        $('#event-end').val(endDate.format(INPUT_FORMAT));
    } else {
        // Recurring: set dtstart and ensure until >= dtstart + 2 intervals
        if (!dtstartDate.isValid()) {
            dtstartDate = startDate.clone();
            $('#event-dtstart').val(dtstartDate.format(INPUT_FORMAT));
        }
        if (!untilDate.isValid()) untilDate = startDate.clone().add(3, date_length);
        const minUntilDate = dtstartDate.clone().add(1, date_length);
        if (untilDate.isSameOrBefore(minUntilDate)) {
            untilDate = moment.utc(
                dtstartDate.clone().add(1, date_length).format('YYYY-MM-DD') + 'T' + untilDate.format('HH:mm')
            );
        }
        $('#event-until').val(untilDate.format(INPUT_FORMAT));
    }
}// end
//---------------------------------------------------------------------------------
window.terminkalender.check_date =  function(){
window.TerminKalender.check_date();
    }  // legacy before 1.2.4
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------

//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
});
//---------------------------------------------------------------------------------