(function () {
    'use strict';

    function onDomReady(callback) {
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', callback);
            return;
        }
        callback();
    }

    function createCommentController() {
        var state = {
            comments: [],
            observer: null
        };

        function markHidden(comment, index) {
            comment.style.animation = 'none';
            comment.classList.add('comment-hidden');
            comment.dataset.animationDelay = index * 0.05;
        }

        function reveal(comment) {
            var delay = parseFloat(comment.dataset.animationDelay) || 0;
            setTimeout(function () {
                comment.classList.remove('comment-hidden');
                comment.classList.add('comment-visible');
            }, delay * 1000);
        }

        function handleIntersection(entries) {
            entries.forEach(function (entry) {
                if (!entry.isIntersecting) {
                    return;
                }

                reveal(entry.target);
                if (state.observer) {
                    state.observer.unobserve(entry.target);
                }
            });
        }

        function createObserver() {
            state.observer = new IntersectionObserver(handleIntersection, {
                root: null,
                rootMargin: '30px',
                threshold: 0.1
            });
        }

        function observeAll() {
            if (!state.observer) {
                return;
            }

            state.comments.forEach(function (comment) {
                state.observer.observe(comment);
            });
        }

        function setup() {
            state.comments = Array.prototype.slice.call(document.querySelectorAll('.comment'));
            if (!state.comments.length) {
                return;
            }

            createObserver();
            state.comments.forEach(markHidden);
            observeAll();
        }

        function refresh() {
            if (state.observer) {
                state.observer.disconnect();
            }
            setup();
        }

        return {
            setup: setup,
            refresh: refresh,
            animateAllComments: function () {
                state.comments.forEach(function (comment, index) {
                    setTimeout(function () {
                        comment.classList.remove('comment-hidden');
                        comment.classList.add('comment-visible');
                    }, index * 50);
                });
            }
        };
    }

    var controller = createCommentController();
    onDomReady(controller.setup);

    window.nxtEventPlannerCommentAnimationController = controller;

    document.addEventListener('comment-added', function () {
        if (window.nxtEventPlannerCommentAnimationController) {
            window.nxtEventPlannerCommentAnimationController.refresh();
        }
    });
})();
