(()=>{"use strict";var e={n:s=>{var t=s&&s.__esModule?()=>s.default:()=>s;return e.d(t,{a:t}),t},d:(s,t)=>{for(var n in t)e.o(t,n)&&!e.o(s,n)&&Object.defineProperty(s,n,{enumerable:!0,get:t[n]})},o:(e,s)=>Object.prototype.hasOwnProperty.call(e,s)};const s=window.wp.element,t=window.wp.apiFetch;var n=e.n(t);const o=window.wp.i18n,i=window.ReactJSXRuntime;function l(e){return e>=80?"#00a32a":e>=60?"#0a84ff":e>=40?"#dba617":"#d63638"}function d({score:e}){const s=l(e);return(0,i.jsx)("div",{style:{width:56,height:56,borderRadius:"50%",border:`3px solid ${s}`,display:"flex",alignItems:"center",justifyContent:"center",flexShrink:0},children:(0,i.jsx)("span",{style:{fontSize:20,fontWeight:700,color:s},children:e})})}function r({data:e}){const{avg_score:s,trend_delta:t,analyzed_count:n}=e,l=t>0?"#00a32a":"#d63638",r=t>0?"↑":"↓";return(0,i.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:16,padding:"12px 0",borderBottom:"1px solid #f0f0f1"},children:[(0,i.jsx)(d,{score:s}),(0,i.jsxs)("div",{style:{flex:1},children:[(0,i.jsx)("div",{style:{fontSize:14,fontWeight:600,color:"#1d2327"},children:(0,o.__)("Average SEO Score","seopulse")}),(0,i.jsx)("div",{style:{fontSize:12,color:"#646970",marginTop:2},children:(0,o.sprintf)(/* translators: %d: number of analyzed posts */ /* translators: %d: number of analyzed posts */
(0,o._n)("Based on %d analyzed post","Based on %d analyzed posts",n,"seopulse"),n)}),0!==t&&(0,i.jsx)("div",{style:{fontSize:12,color:l,marginTop:4,fontWeight:500},children:`${r} ${t>0?"+":""}${t} ${(0,o.__)("pts vs prev. 30 days","seopulse")}`})]})]})}function a({issues:e}){return 0===e.length?null:(0,i.jsxs)("div",{style:{padding:"12px 0",borderBottom:"1px solid #f0f0f1"},children:[(0,i.jsx)("div",{style:{fontSize:13,fontWeight:600,color:"#1d2327",marginBottom:8},children:(0,o.__)("Top Recurring Issues","seopulse")}),(0,i.jsx)("ul",{style:{margin:0,padding:0,listStyle:"none"},children:e.map(e=>(0,i.jsxs)("li",{style:{padding:"6px 0",fontSize:12},children:[(0,i.jsx)("span",{style:{color:"#d63638",fontWeight:600},children:"⚠"})," ",(0,i.jsx)("strong",{children:e.label})," ",(0,i.jsxs)("span",{style:{color:"#646970"},children:["(",(0,o.sprintf)(/* translators: %d: number of posts with this issue */ /* translators: %d: number of posts with this issue */
(0,o._n)("%d post","%d posts",e.count,"seopulse"),e.count),")"]}),(0,i.jsxs)("div",{style:{color:"#646970",marginTop:2,paddingLeft:18},children:["💡 ",e.quick_win]})]},e.module))})]})}function c({posts:e}){return 0===e.length?null:(0,i.jsxs)("div",{style:{padding:"12px 0"},children:[(0,i.jsx)("div",{style:{fontSize:13,fontWeight:600,color:"#1d2327",marginBottom:8},children:(0,o.__)("Recently Analyzed","seopulse")}),(0,i.jsx)("ul",{style:{margin:0,padding:0,listStyle:"none"},children:e.map(e=>(0,i.jsxs)("li",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",padding:"4px 0",fontSize:12},children:[(0,i.jsx)("a",{href:e.edit_url,style:{color:"#2271b1",textDecoration:"none",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",maxWidth:"75%"},children:e.title}),(0,i.jsxs)("span",{style:{color:l(e.score),fontWeight:600,flexShrink:0},children:[e.score,"/100"]})]},e.id))})]})}function p({dashboardUrl:e}){return(0,i.jsxs)("div",{style:{textAlign:"center",padding:"20px 0"},children:[(0,i.jsx)("span",{className:"dashicons dashicons-chart-bar",style:{fontSize:48,color:"#c3c4c7",width:48,height:48}}),(0,i.jsx)("p",{style:{marginTop:12},children:(0,o.__)("No analyzed posts yet. Run your first SEO analysis to see your scores here.","seopulse")}),(0,i.jsx)("a",{href:e,className:"button button-primary",style:{marginTop:8},children:(0,o.__)("Get started","seopulse")})]})}function u(){return(0,i.jsx)("div",{style:{padding:"12px 0"},children:[40,60,50,70,45].map((e,s)=>(0,i.jsx)("div",{style:{height:12,background:"#f0f0f1",borderRadius:4,marginBottom:10,width:`${e}%`,animation:"pulse 1.5s ease-in-out infinite"}},s))})}function h(){const e=window.seopulseWpDashboardWidget,[t,l]=(0,s.useState)(e.initialData??null),[d,h]=(0,s.useState)(!e.initialData),[f,x]=(0,s.useState)(null);(0,s.useEffect)(()=>{n().use(n().createNonceMiddleware(e.nonce))},[]),(0,s.useEffect)(()=>{e.initialData||y()},[]);const y=(0,s.useCallback)(async()=>{h(!0),x(null);try{const s=await n()({url:e.restUrl+"seopulse/v1/dashboard/widget-overview",method:"GET"});l(s.data)}catch(e){x((0,o.__)("Failed to load data.","seopulse"))}finally{h(!1)}},[]),g=(0,s.useCallback)(async()=>{h(!0),x(null);try{const s=await n()({url:e.restUrl+"seopulse/v1/dashboard/widget-overview/refresh",method:"POST"});l(s.data)}catch(e){x((0,o.__)("Refresh failed.","seopulse"))}finally{h(!1)}},[]);return(0,i.jsxs)("div",{className:"seopulse-wp-dashboard-widget",children:[(0,i.jsx)("div",{style:{display:"flex",justifyContent:"flex-end",marginBottom:4},children:(0,i.jsxs)("button",{type:"button",className:"button-link",onClick:g,disabled:d,title:(0,o.__)("Refresh","seopulse"),style:{display:"flex",alignItems:"center",gap:4,fontSize:12,color:d?"#c3c4c7":"#646970",cursor:d?"not-allowed":"pointer",padding:0,border:0,background:"none"},children:[(0,i.jsx)("span",{className:"dashicons dashicons-update"+(d?" seopulse-spin":""),style:{fontSize:16,width:16,height:16}}),(0,o.__)("Refresh","seopulse")]})}),f&&(0,i.jsx)("div",{style:{padding:"6px 10px",background:"#fcf0f1",border:"1px solid #d63638",borderRadius:3,fontSize:12,color:"#d63638",marginBottom:8},children:f}),d&&(0,i.jsx)(u,{}),!d&&null!==t&&(0,i.jsx)(i.Fragment,{children:0===t.analyzed_count?(0,i.jsx)(p,{dashboardUrl:e.dashboardUrl}):(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(r,{data:t}),(0,i.jsx)(a,{issues:t.top_issues}),(0,i.jsx)(c,{posts:t.recent_posts}),(0,i.jsx)("p",{style:{textAlign:"right",marginTop:8},children:(0,i.jsxs)("a",{href:e.dashboardUrl,children:[(0,o.__)("Go to SEOPulse dashboard","seopulse")," →"]})})]})})]})}const f=()=>{const e=document.getElementById("seopulse-wp-dashboard-widget-root");e&&void 0!==window.seopulseWpDashboardWidget&&(0,s.createRoot)(e).render((0,i.jsx)(h,{}))};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",f):f()})();