(function(e){let l=1,u=1,c=!1;e(document).ready(function(){f(),h()});function f(t=1,a=!1){if(c)return;c=!0;const s=e("#commentsList"),n=e("#commentsLoading"),i=e("#loadMoreWrapper");a||s.html(n.show()),e.ajax({url:COMMENTS_FRONTEND_CONFIG.routes.index,method:"GET",data:{page:t},success:function(r){r.success&&(l=r.pagination.current_page,u=r.pagination.last_page,v(r.stats),a||s.empty(),n.hide(),r.comments.length>0?r.comments.forEach(function(o){s.append(m(o))}):a||s.html(N()),l<u?i.show():i.hide())},error:function(){n.hide(),s.html('<div class="text-danger text-center py-4">'+COMMENTS_FRONTEND_CONFIG.translations.error+"</div>")},complete:function(){c=!1}})}function v(t){if(e("#commentStats").html(t.total+" "+COMMENTS_FRONTEND_CONFIG.translations.reviews),COMMENTS_FRONTEND_CONFIG.showRating&&t.average_rating){e("#ratingSummary").show(),e("#avgRating").text(parseFloat(t.average_rating).toFixed(1)),e("#totalRatings").text(t.total+" "+COMMENTS_FRONTEND_CONFIG.translations.reviews);let a="";const s=Math.round(t.average_rating);for(let o=1;o<=5;o++)a+='<i class="fas fa-star '+(o<=s?"":"empty")+'"></i>';e("#avgRatingStars").html(a);let n="";const i=t.rating_distribution,r=Math.max(...Object.values(i),1);for(let o=5;o>=1;o--){const g=i[o]||0,O=g/r*100;n+=`
                    <div class="rating-bar-row">
                        <span class="rating-bar-label">${o} stars</span>
                        <div class="rating-bar-track">
                            <div class="rating-bar-fill" style="width: ${O}%"></div>
                        </div>
                        <span class="rating-bar-count">${g}</span>
                    </div>
                `}e("#ratingBars").html(n)}}function m(t,a=!1){let s="";if(t.rating&&!a)for(let i=1;i<=5;i++)s+='<i class="fas fa-star '+(i<=t.rating?"":"empty")+'"></i>';let n="";return t.replies&&t.replies.length>0&&(n='<div class="comment-replies">',t.replies.forEach(function(i){n+=m(i,!0)}),n+="</div>"),`
            <div class="comment" data-id="${t.id}">
                <div class="comment-avatar">
                    <span class="avatar-initials">${t.author_initials}</span>
                </div>
                <div class="comment-body">
                    <div class="comment-header">
                        <span class="comment-author">${p(t.author_name)}</span>
                        <span class="comment-date">${t.created_at_human}</span>
                    </div>
                    ${s?'<div class="comment-rating">'+s+"</div>":""}
                    <div class="comment-content">${t.content}</div>
                    <div class="comment-actions">
                        <button type="button" class="btn-reply" onclick="replyTo(${t.id}, '${p(t.author_name)}')">
                            <i class="fas fa-reply"></i> ${COMMENTS_FRONTEND_CONFIG.translations.reply}
                        </button>
                    </div>
                    ${n}
                </div>
            </div>
        `}function N(){return`
            <div class="no-comments">
                <i class="far fa-comments"></i>
                <p>${COMMENTS_FRONTEND_CONFIG.translations.no_comments}</p>
            </div>
        `}function h(){e("#commentForm").on("submit",function(t){t.preventDefault(),_()}),e("#loadMoreBtn").on("click",function(){f(l+1,!0)})}function _(){const t=e("#commentForm"),a=t.find(".btn-submit"),s=a.html();a.prop("disabled",!0).html('<i class="fas fa-spinner fa-spin"></i> ...'),e.ajax({url:COMMENTS_FRONTEND_CONFIG.routes.store,method:"POST",data:t.serialize(),success:function(n){if(n.success){if(t[0].reset(),cancelReply(),d("success",n.message),n.is_approved&&n.comment){const i=e(m(n.comment));if(n.comment.parent_id){const r=e(`.comment[data-id="${n.comment.parent_id}"]`);let o=r.find(".comment-replies").first();o.length===0&&(o=e('<div class="comment-replies"></div>'),r.find(".comment-body").first().append(o)),o.append(i)}else{const r=e("#commentsList .no-comments");r.length&&r.remove(),e("#commentsList").prepend(i)}}}else d("danger",n.message)},error:function(n){let i=COMMENTS_FRONTEND_CONFIG.translations.error;n.responseJSON&&n.responseJSON.message&&(i=n.responseJSON.message),d("danger",i)},complete:function(){a.prop("disabled",!1).html(s)}})}window.replyTo=function(t,a){e("#parentId").val(t),e("#replyToName").text(a),e("#replyIndicator").show(),e("#ratingInputGroup").hide(),e("html, body").animate({scrollTop:e("#commentFormWrapper").offset().top-100},300),e("#commentContent").focus()},window.cancelReply=function(){e("#parentId").val(""),e("#replyIndicator").hide(),e("#ratingInputGroup").show()};function d(t,a){const s=`
            <div class="alert alert-${t} alert-dismissible fade show" role="alert">
                ${a}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        `,n=e(s);e("#commentFormWrapper").prepend(n),setTimeout(function(){n.alert("close")},5e3)}function p(t){const a=document.createElement("div");return a.textContent=t,a.innerHTML}})(jQuery);
