{{-- Normal Calendar Data --}}
<script>
    $(function() {
      //* Build Calendar Data
        // Build calendar
        const $normal_calendar = $('#normal-calendar');
        window.normal_calendar = new FullCalendar.Calendar($normal_calendar[0], {
          // Plugins - Localization - Lang
            plugins: ['interaction', 'resourceTimeline'],
            locale: window.page_lang,
            lang: window.page_lang, // Idioma
            navLinks: false,
            timeZone: "local",

          // Default view and design
            defaultView: 'resourceTimelineMonth',
            aspectRatio: 1.5,
            scrollTime: '00:00',
            aspectRatio: 2,
            resourceAreaWidth: '30',
            height: 600,

          // Header and Buttons
            header: {
                left: 'today prev,next',
                center: 'title',
                right: 'resourceTimelineMonth resourceTimelineTrimestre'
                // right: 'resourceTimelineMonth resourceTimelineTrimestre resourceTimelineWeek resourceTimelineFifteenDay resourceTimelineOneDay'
            },

          // Custom button's texts
            buttonText: {
                today: "{{ __('neutral_words')['today'] }}",
                resourceTimelineMonth: "{{ __('neutral_words')['month'] }}",
                resourceTimelineWeek: "{{ __('neutral_words')['week'] }}",
            },

          // Custom views and design
            views: {
                resourceTimelineTrimestre: {
                    type: 'resourceTimeline',
                    duration: { months: 3 },
                    buttonText: "{{ __('neutral_words')['quarter'] }}"
                },
                resourceTimelineOneDay: {
                    type: 'resourceTimeline',
                    duration: { days: 1 },
                    buttonText: "{{ __('neutral_words')['day'] }}"
                },
                resourceTimelineFifteenDay: {
                    type: 'resourceTimeline',
                    duration: { weeks: 2 },
                    buttonText: "{{ __('neutral_words')['fortnight'] }}"
                },
            },

          // Data
            resourceColumns: @json($render_data->resource_columns),
            resourceLabelText:  "{{ $render_data->resource_main_label }}",
            resourceOrder: '{{ $render_data->resource_order }}',
            resources(fetchInfo, successData, failureMessage) {
                $.get("{{ route('api.public-calendar.rooms') }}")
                    .done(successData).fail(failureMessage);
            },

            events: function(fetchInfo, successCallback, failureCallback) {
              // Conditionals
                if(!window.is_normal_calendar) {
                    successCallback([]);
                    return;
                }

              // Get Stored Data
                let eventsEncrypted = localStorage.getItem("NormalEventData") || null;
                let eventsStored = decryptData(eventsEncrypted);

              // Request for Changes
                // Request Data
                const start = moment(fetchInfo.startStr).format('YYYY-MM-DD');
                const end = moment(fetchInfo.endStr).format('YYYY-MM-DD');

                $.get("{{ route('api.public-calendar.events') }}", { start, end},
                    function(response) {
                        let data = JSON.parse(response);

                        if (JSON.stringify(eventsStored) !== JSON.stringify(data)) {
                            successCallback(data);

                            const encrypted = encryptData(data);
                            localStorage.setItem("NormalEventData", encrypted);

                            launchSwalToast(
                                `{{ __('neutral_words')['information'] }} <b style="color:#12a15e">{{ __('common_actions')['updated'] }}</b>`,
                                'info',
                            );
                        }
                    }
                );
            },
        });

        // Draw Calendar
        window.normal_calendar.render();

      //* Calendar data cycle
        window.updateCalendarEvents = () => window.normal_calendar.refetchEvents();
        window.updateCalendarEventsDebounced = (window.updateCalendarEventsDebounced || debounce(updateCalendarEvents, 800));

        setInterval(() => {
            if(window.is_normal_calendar && !window.is_normal_dragging_event)
                updateCalendarEventsDebounced();
        }, 6000);
    });
</script>
