(()=>{"use strict";const e=window.wp.i18n;jQuery(document).ready((function(t){function s(e){return e>=80?"green":e>=50?"yellow":e>=30?"orange":"red"}function o(o,r){const i=t(o),n=i.find(".quality-results");i.find(".quality-empty").hide(),n.show();const a=r.data||r,c=a.overall_score||0,d=a.scores||{},l=a.feedback||{},u=l.strengths||[],f=l.improvements||[],p=l.suggestions||[];let y="";y+='
',y+="

"+(0,e.__)("Docs quality analysis results","betterdocs")+"

",y+='",y+="
";const g=s(c),h=2*Math.PI*54,v=h-c/100*h;y+='
',y+='
',y+='

'+(0,e.__)("Overall Score","betterdocs")+"

",y+='
',y+='',y+='',y+='',y+="",y+='
',y+=''+c+"",y+=''+(0,e.__)("out of 100","betterdocs")+"",y+="
",y+="
",y+='
'+function(t){return t>=80?(0,e.__)("Excellent","betterdocs"):t>=60?(0,e.__)("Good","betterdocs"):t>=40?(0,e.__)("Needs Improvement","betterdocs"):(0,e.__)("Poor","betterdocs")}(c)+"
",y+='

'+(0,e.__)("This score is a weighted average of clarity, relevance, structure, and other quality factors.","betterdocs")+"

",y+="
",y+='
',Object.keys(d).length>0&&(y+='
',y+='

'+(0,e.__)("Score Breakdown","betterdocs")+"

",y+='
',Object.keys(d).forEach((function(e){const t=d[e];y+='
',y+='
',y+=''+e.charAt(0).toUpperCase()+e.slice(1)+"",y+=''+t+"%",y+="
",y+='
',y+='
',y+='
',y+="
",y+="
",y+="
"})),y+="
",y+="
"),y+="
",y+="
";const b=[];u.length>0&&b.push(...u),f.length>0&&b.push(...f),p.length>0&&b.push(...p),b.length>0&&(y+='
',y+='

'+(0,e.__)("Actionable Insights","betterdocs")+"

",y+='
',b.forEach((function(e){y+='
',y+='
',y+='',y+='',y+='',y+="",y+="
",y+=''+e+"",y+="
"})),y+="
",y+="
"),n.html(y)}function r(e){const s=t(e);s.find(".quality-empty").hide(),s.find(".quality-results").hide(),s.find(".quality-error").hide(),s.find(".quality-loading").show()}function i(e){t(e).find(".quality-loading").hide()}function n(s,o){const r=t(s);i(s);let n="",a=!1;n="string"==typeof o?o:o&&"object"==typeof o?o.message?o.message:o.data&&o.data.message?o.data.message:(0,e.__)("An unexpected error occurred. Please try again.","betterdocs"):(0,e.__)("Analysis failed. Please try again.","betterdocs"),n.toLowerCase().includes("api key")||n.toLowerCase().includes("not configured")?(!0,n=(0,e.__)("OpenAI API key is not configured. Please configure your API key in the settings to use this feature.","betterdocs"),r.find(".betterdocs-analyze-btn").hide(),r.find(".quality-empty").show(),r.find(".quality-error").find(".error-icon").show(),r.find(".quality-error").find("strong").show(),r.find(".quality-error").find(".error-action-button").show(),r.find(".quality-error").removeClass("error-content-validation").addClass("error-config")):(r.find(".quality-error").find(".error-icon").hide(),r.find(".quality-error").find("strong").hide(),r.find(".quality-error").find(".error-action-button").hide(),r.find(".betterdocs-analyze-btn").show(),r.find(".quality-empty").show(),r.find(".quality-error").removeClass("error-config").addClass("error-content-validation")),r.find(".quality-error").find(".error-message").text(n),r.find(".quality-error").show()}t(document).on("click","#betterdocs-analyze-quality",(function(s){s.preventDefault();const a=t(this),c=a.closest(".betterdocs-quality-analysis-content"),d=c.data("post-id");if(!d)return void alert((0,e.__)("Please save the post first.","betterdocs"));let l="",u="";if("undefined"!=typeof wp&&wp.data&&wp.data.select("core/editor")){const e=wp.data.select("core/editor");l=e.getEditedPostContent(),u=e.getEditedPostAttribute("title")}else"undefined"!=typeof tinymce&&tinymce.get("content")?(l=tinymce.get("content").getContent(),u=t("#title").val()||""):t("#content").length&&(l=t("#content").val(),u=t("#title").val()||"");a.prop("disabled",!0),r(c);const f={action:"betterdocs_analyze_quality",post_id:d,nonce:betterdocsQualityScore.nonce};l&&(f.current_content=l,f.current_title=u),t.ajax({url:betterdocsQualityScore.ajax_url,type:"POST",data:f,success:function(e){e.success?(i(c),o(c,e.data)):n(c,e.data||e)},error:function(){n(c,(0,e.__)("Network error. Please try again.","betterdocs"))},complete:function(){a.prop("disabled",!1)}})})),t(document).on("click","#betterdocs-regenerate-quality",(function(s){s.preventDefault();const a=t(this),c=a.closest(".betterdocs-quality-analysis-content"),d=c.data("post-id");if(!d)return void alert((0,e.__)("Please save the post first.","betterdocs"));let l="",u="";if("undefined"!=typeof wp&&wp.data&&wp.data.select("core/editor")){const e=wp.data.select("core/editor");l=e.getEditedPostContent(),u=e.getEditedPostAttribute("title")}else"undefined"!=typeof tinymce&&tinymce.get("content")?(l=tinymce.get("content").getContent(),u=t("#title").val()||""):t("#content").length&&(l=t("#content").val(),u=t("#title").val()||"");a.prop("disabled",!0),r(c);const f={action:"betterdocs_analyze_quality",post_id:d,nonce:betterdocsQualityScore.nonce};l&&(f.current_content=l,f.current_title=u),t.ajax({url:betterdocsQualityScore.ajax_url,type:"POST",data:f,success:function(e){e.success?(i(c),o(c,e.data)):n(c,e.data||e)},error:function(){n(c,(0,e.__)("Network error. Please try again.","betterdocs"))},complete:function(){a.prop("disabled",!1)}})}))}))})();