1import{c as ke,x as t,j as e,a0 as xe,y as Ae,z as V,s as Ce,T as Ee,X as Te,n as Pe,K as Me,a3 as Le,l as $e}from"./index-DyWV83gM.js";import{u as qe}from"./user-context-DAVaQNKi.js";import{s as R}from"./supabase-D_kz0RN6.js";import{a as Ie,b as De,c as ze,g as Ge,T as Fe,F as Oe,d as Re,P as Ue,S as Be}from"./SearchTermAnalysis-Cq21aU6k.js";import{M as Ye}from"./MetaPlacementAnalysis-DlsJ3ej4.js";import{g as Ke,s as re}from"./performanceSyncService-B-Tavdcc.js";import{g as Xe,j as He,k as We,l as Ze,n as ne,S as Je,m as Qe,a as oe}from"./relativeTime-D8VrmDyX.js";import{c as ae}from"./apiClient-BukVzz1Q.js";import{u as Ve}from"./usePlanLimits-BxKZq8FC.js";import{u as ea}from"./upgrade-modal-context-5TsgmwJX.js";import{u as aa,S as ta}from"./featureUnlockTier-CDj1WDWB.js";import{Z as K}from"./zap-owrTc1sK.js";import{L as O}from"./loader-2-DvMZDIAe.js";import{P as ue}from"./plus-xXwTGSwI.js";import{A as W}from"./alert-circle-CvUZ9AVL.js";import{C as pe}from"./clock-zAlj5_jF.js";import{P as sa}from"./pause-DZvN-_W4.js";import{P as ra}from"./play-BrUBuhaS.js";import{T as ge}from"./trash-2-D8QM9kED.js";import{R as he}from"./refresh-cw-CQs5nCLz.js";import{M as na}from"./minus-circle-j44EdeWB.js";import{U as te}from"./users-D74pqx9Q.js";import{A as ie}from"./arrow-left-DjDMreQS.js";import{B as le}from"./bar-chart-3-CKnVAQYO.js";import{L as oa}from"./layers-HuClQ0pv.js";import"./debugging-DmfSWYto.js";import"./auth-B8zwNqtX.js";import"./SeverityBadge-CnBpsDFr.js";import"./GroupHeader-Byoa9Qfs.js";import"./Drawer-CAjDL-8C.js";import"./x-circle-CPKtany_.js";import"./info-D2xtCQ_7.js";import"./trending-up-Cguyj62k.js";import"./chevron-up-CQqrVq5n.js";import"./calendar-BTY2s210.js";import"./dollar-sign-9okeSkcQ.js";import"./wrench-D77FRxGN.js";import"./shield-80f2ryHI.js";import"./alert-octagon-CgXnXtAq.js";import"./useQuery-BR-_KG4W.js";import"./timer-DMoI6WUp.js";import"./featureFlags-C0lJ78AB.js";import"./useTrackingDiagnostic-D0dT8gx0.js";import"./trackingDiagnostic-DhJ199Mc.js";import"./map-pin-DIUJmttQ.js";import"./AutoScanBadge-iA8T1SAJ.js";import"./download-D69O2ceD.js";import"./history-C6qc4Ef-.js";import"./lock-M6qq0cAd.js";import"./supabaseJson-BKaEOcBg.js";import"./PlanManagementModal-dBzV5HMk.js";import"./TokenRefreshService-CsqjRhKF.js";import"./stripeService-GF_N4nxv.js";import"./minimize-2-Dm_b9pdI.js";import"./check-circle-BIYsDei0.js";import"./external-link-BPE-fhI3.js";import"./credit-card-CPYBdRjL.js";/** 2 * @license lucide-react v0.344.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const ia=ke("CircleDashed",[["path",{d:"M10.1 2.182a10 10 0 0 1 3.8 0",key:"5ilxe3"}],["path",{d:"M13.9 21.818a10 10 0 0 1-3.8 0",key:"11zvb9"}],["path",{d:"M17.609 3.721a10 10 0 0 1 2.69 2.7",key:"1iw5b2"}],["path",{d:"M2.182 13.9a10 10 0 0 1 0-3.8",key:"c0bmvh"}],["path",{d:"M20.279 17.609a10 10 0 0 1-2.7 2.69",key:"1ruxm7"}],["path",{d:"M21.818 10.1a10 10 0 0 1 0 3.8",key:"qkgqxc"}],["path",{d:"M3.721 6.391a10 10 0 0 1 2.7-2.69",key:"1mcia2"}],["path",{d:"M6.391 20.279a10 10 0 0 1-2.69-2.7",key:"1fvljs"}]]);async function la(i){const{data:r,error:a}=await R.from("perf_automation_rules").select("*").eq("organization_id",i).order("created_at",{ascending:!1});if(a)throw new Error(`Failed to fetch rules: ${a.message}`);return r||[]}async function ca(i,r,a){const{data:p,error:f}=await R.from("perf_automation_rules").insert({organization_id:i,created_by:r,name:a.name,description:a.description||null,platform:a.platform,ad_account_id:a.ad_account_id||null,entity_type:a.entity_type,conditions:a.conditions,action_type:a.action_type,action_config:a.action_config||{},auto_execute:a.auto_execute||!1,safety_category:a.safety_category||"review",max_executions_per_day:a.max_executions_per_day||10,cooldown_hours:a.cooldown_hours||24}).select("*").single();if(f)throw new Error(`Failed to create rule: ${f.message}`);return p}async function ye(i,r){const{error:a}=await R.from("perf_automation_rules").update({...r,updated_at:new Date().toISOString()}).eq("id",i);if(a)throw new Error(`Failed to update rule: ${a.message}`)}async function da(i){const{error:r}=await R.from("perf_automation_rules").delete().eq("id",i);if(r)throw new Error(`Failed to delete rule: ${r.message}`)}async function ma(i,r){await ye(i,{is_active:r})}async function xa(i){return(await ae("campaign-automation",{action:"get_pending",organization_id:i})).executions||[]}async function ua(i,r){const a=await ae("campaign-automation",{action:"approve_execution",execution_id:i,organization_id:r});if(!a.success)throw new Error(a.error||"Approval failed")}async function pa(i){const r=await ae("campaign-automation",{action:"reject_execution",execution_id:i});if(!r.success)throw new Error(r.error||"Rejection failed")}const ga={pause_keyword:"Pause keyword",pause_ad:"Pause ad",pause_ad_set:"Pause ad set",adjust_budget:"Adjust budget",add_negative_keyword:"Add negative keyword",flag_creative:"Flag creative"},ha={keyword:"Keywords",ad:"Ads",ad_set:"Ad Sets",ad_group:"Ad Groups",campaign:"Campaigns"};function ya({organizationId:i,onCreateRule:r,onEditRule:a}){const[p,f]=t.useState([]),[g,l]=t.useState(!0),[o,b]=t.useState(null),{canUseAutomation:_}=Ve(),{openUpgradeModal:T}=ea();t.useEffect(()=>{y()},[i]);async function y(){l(!0);try{const s=await la(i);f(s)}catch(s){b(s instanceof Error?s.message:"Failed to load rules")}
6l(!1)}async function u(s){try{await ma(s.id,!s.is_active),f(x=>x.map(v=>v.id===s.id?{...v,is_active:!v.is_active}:v))}catch(x){b(x instanceof Error?x.message:"Toggle failed")}}async function n(s){try{await da(s),f(x=>x.filter(v=>v.id!==s))}catch(x){b(x instanceof Error?x.message:"Delete failed")}}return _?g?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(O,{className:"h-5 w-5 animate-spin text-gray-400"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-3",children:[e.jsx("div",{className:"p-2 bg-purple-50 rounded-lg",children:e.jsx(K,{className:"h-5 w-5 text-purple-600"})}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-gray-900",children:"Automation Rules"}),e.jsx("p",{className:"text-xs text-gray-500",children:p.length===0?"Create rules to automate optimizations":`${p.length} rule${p.length!==1?"s":""}`})]})]}),e.jsxs("button",{onClick:r,className:"inline-flex items-center space-x-1.5 px-3 py-1.5 text-xs font-medium text-white bg-[#69a08a] hover:bg-[#5a8f7a] rounded-md transition-colors",children:[e.jsx(ue,{className:"h-3.5 w-3.5"}),e.jsx("span",{children:"New Rule"})]})]}),o&&e.jsxs("div",{className:"flex items-center space-x-2 p-3 rounded-lg bg-red-50 text-red-700 text-sm",children:[e.jsx(W,{className:"h-4 w-4 flex-shrink-0"}),e.jsx("span",{children:o})]}),p.length===0?e.jsxs("div",{className:"text-center py-8 text-gray-400",children:[e.jsx(K,{className:"h-10 w-10 mx-auto mb-2 opacity-30"}),e.jsx("p",{className:"text-sm",children:"No automation rules yet."}),e.jsx("p",{className:"text-xs mt-1",children:"Create a rule to automatically optimize your campaigns."})]}):e.jsx("div",{className:"space-y-2",children:p.map(s=>e.jsxs("div",{className:`flex items-center justify-between p-4 rounded-lg border transition-colors cursor-pointer hover:bg-gray-50 ${s.is_active?"bg-white border-gray-200":"bg-gray-50 border-gray-100 opacity-60"}`,onClick:()=>a(s),children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-900 truncate",children:s.name}),e.jsx("span",{className:`inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-medium ${s.auto_execute?"bg-green-100 text-green-700":"bg-yellow-100 text-yellow-700"}`,children:s.auto_execute?"Auto":"Approval"}),e.jsx("span",{className:"text-[10px] text-gray-400 font-medium",children:s.platform==="google_ads"?"Google":"Meta"})]}),e.jsxs("p",{className:"text-xs text-gray-500 mt-0.5",children:[ha[s.entity_type]||s.entity_type," â ",ga[s.action_type]||s.action_type,s.conditions.length>0&&` â ${s.conditions.length} condition${s.conditions.length!==1?"s":""}`]}),s.last_evaluated_at&&e.jsxs("p",{className:"text-[10px] text-gray-400 mt-1 flex items-center space-x-1",children:[e.jsx(pe,{className:"h-3 w-3"}),e.jsxs("span",{children:["Last evaluated: ",new Date(s.last_evaluated_at).toLocaleString()]})]})]}),e.jsxs("div",{className:"flex items-center space-x-2 ml-4",onClick:x=>x.stopPropagation(),children:[e.jsx("button",{onClick:()=>u(s),className:`p-1.5 rounded-md transition-colors ${s.is_active?"text-green-600 hover:bg-green-50":"text-gray-400 hover:bg-gray-100"}`,title:s.is_active?"Pause rule":"Activate rule",children:s.is_active?e.jsx(sa,{className:"h-4 w-4"}):e.jsx(ra,{className:"h-4 w-4"})}),e.jsx("button",{onClick:()=>n(s.id),className:"p-1.5 rounded-md text-gray-400 hover:text-red-600 hover:bg-red-50 transition-colors",title:"Delete rule",children:e.jsx(ge,{className:"h-4 w-4"})})]})]},s.id))})]}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center space-x-3",children:[e.jsx("div",{className:"p-2 bg-purple-50 rounded-lg",children:e.jsx(K,{className:"h-5 w-5 text-purple-600"})}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-gray-900",children:"Automation Rules"}),e.jsx("p",{className:"text-xs text-gray-500",children:"Automate optimizations based on performance rules"})]})]}),e.jsxs("div",{className:"text-center py-8 bg-gray-50 rounded-lg border border-gray-100",children:[e.jsx(K,{className:"h-8 w-8 mx-auto mb-2 text-gray-300"}),e.jsx("p",{className:"text-sm font-medium text-gray-600",children:"Automation is available on Professional plans"}),e.jsx("p",{className:"text-xs text-gray-400 mt-1",children:"Set rules to auto-pause weak keywords, adju
6st budgets, and more."}),e.jsxs("button",{type:"button",onClick:()=>T(aa("can_use_automation","Automation rules")),className:"mt-4 inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium rounded-lg bg-[#69a08a] text-white hover:bg-[#5a8f7a] transition-colors",children:[e.jsx(K,{className:"h-4 w-4"}),"Upgrade"]})]})]})}const fa=[{value:"cost_micros",label:"Spend (micros)"},{value:"conversions",label:"Conversions"},{value:"clicks",label:"Clicks"},{value:"impressions",label:"Impressions"},{value:"cpa_micros",label:"CPA (micros)"},{value:"cpc_micros",label:"CPC (micros)"},{value:"ctr",label:"CTR"},{value:"roas",label:"ROAS"}],ba=[{value:">",label:">"},{value:"<",label:"<"},{value:">=",label:">="},{value:"<=",label:"<="},{value:"=",label:"="},{value:"!=",label:"!="}],ja=[{value:"keyword",label:"Keywords"},{value:"ad",label:"Ads"},{value:"ad_group",label:"Ad Groups"},{value:"ad_set",label:"Ad Sets"},{value:"campaign",label:"Campaigns"}],va=[{value:"pause_keyword",label:"Pause keyword"},{value:"pause_ad",label:"Pause ad"},{value:"pause_ad_set",label:"Pause ad set"},{value:"adjust_budget",label:"Adjust budget"},{value:"add_negative_keyword",label:"Add negative keyword"},{value:"flag_creative",label:"Flag creative"}],ce={metric:"cost_micros",operator:">",value:0,period_days:30};function Na({organizationId:i,userId:r,existingRule:a,onClose:p,onSaved:f}){const g=!!a,[l,o]=t.useState((a==null?void 0:a.name)||""),[b]=t.useState((a==null?void 0:a.description)||""),[_,T]=t.useState((a==null?void 0:a.platform)||"google_ads"),[y,u]=t.useState((a==null?void 0:a.entity_type)||"keyword"),[n,s]=t.useState((a==null?void 0:a.conditions)||[{...ce}]),[x,v]=t.useState((a==null?void 0:a.action_type)||"pause_keyword"),[C,S]=t.useState((a==null?void 0:a.auto_execute)||!1),[M,d]=t.useState((a==null?void 0:a.max_executions_per_day)||10),[P,L]=t.useState((a==null?void 0:a.cooldown_hours)||24),[k,D]=t.useState(!1),[$,E]=t.useState(null);function U(){s(c=>[...c,{...ce}])}function I(c){s(A=>A.filter((j,N)=>N!==c))}function z(c,A,j){s(N=>N.map((m,q)=>q===c?{...m,[A]:j}:m))}async function G(){if(!l.trim()){E("Rule name is required");return}if(n.length===0){E("At least one condition is required");return}D(!0),E(null);try{const c={name:l.trim(),description:b.trim()||void 0,platform:_,entity_type:y,conditions:n,action_type:x,auto_execute:C,safety_category:C?"safe":"review",max_executions_per_day:M,cooldown_hours:P};g?await ye(a.id,c):await ca(i,r,c),f()}catch(c){E(c instanceof Error?c.message:"Save failed")}D(!1)}return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/50",children:e.jsxs("div",{className:"bg-white rounded-xl shadow-2xl w-full max-w-lg max-h-[90vh] overflow-y-auto mx-4",children:[e.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-gray-200",children:[e.jsx("h2",{className:"text-lg font-semibold text-gray-900",children:g?"Edit Rule":"New Automation Rule"}),e.jsx("button",{onClick:p,className:"p-1 text-gray-400 hover:text-gray-600",children:e.jsx(xe,{className:"h-5 w-5"})})]}),e.jsxs("div",{className:"px-6 py-4 space-y-5",children:[$&&e.jsx("div",{className:"p-3 rounded-lg bg-red-50 text-red-700 text-sm",children:$}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Rule Name"}),e.jsx("input",{type:"text",value:l,onChange:c=>o(c.target.value),placeholder:"e.g., Pause zero-conversion keywords",className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-[#69a08a] focus:border-[#69a08a]"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Platform"}),e.jsxs("select",{value:_,onChange:c=>T(c.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-[#69a08a] focus:border-[#69a08a]",children:[e.jsx("option",{value:"google_ads",children:"Google Ads"}),e.jsx("option",{value:"meta_ads",children:"Meta Ads"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Entity Type"}),e.jsx("select",{value:y,onChange:c=>u(c.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-[#69a08a] focus:border-[#69a08a]",children:ja.map(c=>e.jsx("option",{value:c.value,children:c.label},c.value))})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:["Conditions ",e.jsx("span",{className:"text-gray-400 font-normal",children:"(all must match)"})]}),e.jsx("div",{className:"space-y-2",children:n.map((c,A)=>e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("select",{value:c.metric,onChange:j=>
6z(A,"metric",j.target.value),className:"flex-1 px-2 py-1.5 border border-gray-300 rounded text-xs focus:ring-[#69a08a] focus:border-[#69a08a]",children:fa.map(j=>e.jsx("option",{value:j.value,children:j.label},j.value))}),e.jsx("select",{value:c.operator,onChange:j=>z(A,"operator",j.target.value),className:"w-14 px-2 py-1.5 border border-gray-300 rounded text-xs text-center focus:ring-[#69a08a] focus:border-[#69a08a]",children:ba.map(j=>e.jsx("option",{value:j.value,children:j.label},j.value))}),e.jsx("input",{type:"number",value:c.value,onChange:j=>z(A,"value",parseFloat(j.target.value)||0),className:"w-28 px-2 py-1.5 border border-gray-300 rounded text-xs focus:ring-[#69a08a] focus:border-[#69a08a]"}),e.jsx("span",{className:"text-xs text-gray-400 whitespace-nowrap",children:"in"}),e.jsx("input",{type:"number",value:c.period_days,onChange:j=>z(A,"period_days",parseInt(j.target.value)||7),className:"w-14 px-2 py-1.5 border border-gray-300 rounded text-xs focus:ring-[#69a08a] focus:border-[#69a08a]"}),e.jsx("span",{className:"text-xs text-gray-400",children:"days"}),n.length>1&&e.jsx("button",{onClick:()=>I(A),className:"p-1 text-gray-400 hover:text-red-500",children:e.jsx(ge,{className:"h-3.5 w-3.5"})})]},A))}),e.jsxs("button",{onClick:U,className:"mt-2 inline-flex items-center space-x-1 text-xs text-[#69a08a] hover:text-[#5a8f7a]",children:[e.jsx(ue,{className:"h-3.5 w-3.5"}),e.jsx("span",{children:"Add condition"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Action"}),e.jsx("select",{value:x,onChange:c=>v(c.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-[#69a08a] focus:border-[#69a08a]",children:va.map(c=>e.jsx("option",{value:c.value,children:c.label},c.value))})]}),e.jsxs("div",{className:"flex items-center justify-between p-3 rounded-lg bg-gray-50 border border-gray-100",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-gray-700",children:"Auto-execute"}),e.jsx("p",{className:"text-xs text-gray-500",children:"Apply automatically without approval (safe actions only)"})]}),e.jsx("button",{onClick:()=>S(!C),className:`relative inline-flex h-5 w-9 items-center rounded-full transition-colors ${C?"bg-[#69a08a]":"bg-gray-300"}`,children:e.jsx("span",{className:`inline-block h-3.5 w-3.5 transform rounded-full bg-white transition-transform ${C?"translate-x-4.5":"translate-x-0.5"}`,style:{transform:C?"translateX(18px)":"translateX(2px)"}})})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-gray-600 mb-1",children:"Max per day"}),e.jsx("input",{type:"number",value:M,onChange:c=>d(parseInt(c.target.value)||1),min:1,max:100,className:"w-full px-3 py-1.5 border border-gray-300 rounded text-sm focus:ring-[#69a08a] focus:border-[#69a08a]"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-gray-600 mb-1",children:"Cooldown (hours)"}),e.jsx("input",{type:"number",value:P,onChange:c=>L(parseInt(c.target.value)||1),min:1,max:720,className:"w-full px-3 py-1.5 border border-gray-300 rounded text-sm focus:ring-[#69a08a] focus:border-[#69a08a]"})]})]})]}),e.jsxs("div",{className:"flex items-center justify-end space-x-3 px-6 py-4 border-t border-gray-200",children:[e.jsx("button",{onClick:p,className:"px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-100 rounded-lg transition-colors",children:"Cancel"}),e.jsxs("button",{onClick:G,disabled:k,className:"inline-flex items-center space-x-1.5 px-4 py-2 text-sm font-medium text-white bg-[#69a08a] hover:bg-[#5a8f7a] rounded-lg disabled:opacity-50 transition-colors",children:[k&&e.jsx(O,{className:"h-3.5 w-3.5 animate-spin"}),e.jsx("span",{children:g?"Save Changes":"Create Rule"})]})]})]})})}function wa({organizationId:i}){const[r,a]=t.useState([]),[p,f]=t.useState(!0),[g,l]=t.useState(null),[o,b]=t.useState(null);t.useEffect(()=>{_()},[i]);async function _(){f(!0);try{const u=await xa(i);a(u)}catch{}f(!1)}async function T(u){l(u),b(null);try{await ua(u,i),a(n=>n.filter(s=>s.id!==u))}catch(n){b(n instanceof Error?n.message:"Approval failed")}l(null)}async function y(u){l(u);try{await pa(u),a(n=>n.filter(s=>s.id!==u))}catch(n){b(n instanceof Error?n.message:"Rejection failed")}l(null)}return p?e.jsx("div",{className:"flex items-center justify-center py-4",children:e.jsx(O,{className:"h-4 w-4 animate-spin text-gray-400"})}):r.length===0?null:e.jsxs("div",{className:"bg-yellow-50 border border-yellow-200 rounded-xl p-4 space-y-3",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(pe,{className:"h-4 w-4 text-yellow-600"}),e.jsxs("h3",{className:"text-sm font-semibold text-yellow-800",children:[r.length," pending approval",r.length!==1?"s":""]})]}),o&&e.jsxs("div",{className:"flex items-center space-x-2 text-sm text-red-600",children:[e.jsx(W,{className:"h-3.5 w-3.5"}),e.jsx("span",{children:o})]}),e.jsx("div",{className:"space-y-2",children:r.map(u=>{var n,s,x;return e.jsxs("div",{className:"flex items-center justify-between bg-white rounded-lg p-3 border border-yellow-100",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 truncate",children:((n=u.perf_automation_rules)==null?void 0:n.name)||"Rule"}),e.jsxs("p",{className:"text-xs text-gray-500",children:[u.matched_entity_type," ",u.matched_entity_external_id," â ",(x=(s=u.perf_automation_rules)==null?void 0:s.action_type)==null?void 0:x.replace(/_/g," ")
6]})]}),e.jsxs("div",{className:"flex items-center space-x-1.5 ml-3",children:[e.jsx("button",{onClick:()=>T(u.id),disabled:g===u.id,className:"p-1.5 rounded-md bg-green-100 text-green-700 hover:bg-green-200 disabled:opacity-50 transition-colors",title:"Approve",children:g===u.id?e.jsx(O,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(Ae,{className:"h-3.5 w-3.5"})}),e.jsx("button",{onClick:()=>y(u.id),disabled:g===u.id,className:"p-1.5 rounded-md bg-red-100 text-red-700 hover:bg-red-200 disabled:opacity-50 transition-colors",title:"Reject",children:e.jsx(xe,{className:"h-3.5 w-3.5"})})]})]},u.id)})})]})}const _a={interests:"Audience segments",audience_mode:"Observation or targeting",competitor_segment:"Competitor segment",demographics:"Age and gender"};function Sa({verdict:i}){const r={matches:{label:"In place",className:"bg-green-100 text-green-800 border-green-200",Icon:Ce},partial:{label:"Partly",className:"bg-amber-100 text-amber-800 border-amber-200",Icon:V},missing:{label:"Not set up",className:"bg-amber-100 text-amber-800 border-amber-200",Icon:V},drifted:{label:"Set up differently",className:"bg-blue-100 text-blue-800 border-blue-200",Icon:V},not_applicable:{label:"Not available here",className:"bg-gray-100 text-gray-600 border-gray-200",Icon:na},unknown:{label:"Not judged",className:"bg-gray-100 text-gray-600 border-gray-200",Icon:ia}},{label:a,className:p,Icon:f}=r[i];return e.jsxs("span",{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[11px] font-medium border shrink-0 ${p}`,children:[e.jsx(f,{className:"h-3 w-3"}),a]})}function ka({organizationId:i,platform:r,adAccountId:a,campaignExternalId:p,initial:f,onChecked:g}){const[l,o]=t.useState(f),[b,_]=t.useState(!1),[T,y]=t.useState(null),u=async()=>{_(!0),y(null);try{const n=await Xe({organizationId:i,platform:r,adAccountId:a,campaignExternalId:p});n.status==="ok"?(o({...n.coverage,checkedAt:n.checkedAt}),g==null||g()):n.status==="failed"?y(n.reason==="no_google_auth"?"Mateq could not reach the Google Ads account for this campaign.":"Mateq could not read this campaign's audience settings from Google Ads. Nothing was changed."):y("Checking what is live is only available for Google campaigns.")}catch(n){y(n instanceof Error?n.message:"The check failed.")}finally{_(!1)}};return e.jsxs("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 mb-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-gray-900",children:"What this campaign has in Google Ads"}),e.jsx("p",{className:"text-xs text-gray-500 mt-0.5",children:l!=null&&l.checkedAt?`Read from Google Ads on ${new Date(l.checkedAt).toLocaleString()}.`:"Mateq has not read this campaign's audience settings yet."})]}),e.jsxs("button",{type:"button",onClick:u,disabled:b,className:"flex items-center gap-2 px-3 py-2 text-sm font-medium text-[#69a08a] border border-[#69a08a] rounded-lg hover:bg-green-50 disabled:opacity-50 shrink-0",children:[b?e.jsx(O,{className:"h-4 w-4 animate-spin"}):e.jsx(he,{className:"h-4 w-4"}),b?"Readingâ¦":l?"Check again":"Check what is live"]})]}),T&&e.jsx("p",{className:"text-sm text-gray-600 bg-gray-50 border border-gray-200 rounded-lg p-3 mb-4",children:T}),l?e.jsx("div",{className:"space-y-3",children:l.dimensions.map(n=>e.jsxs("div",{className:"border border-gray-100 rounded-lg p-3",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 mb-2",children:[e.jsx("span",{className:"text-sm font-medium text-gray-900",children:_a[n.key]}),e.jsx(Sa,{verdict:n.verdict})]}),e.jsxs("dl",{className:"grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-1 text-xs",children:[e.jsxs("div",{children:[e.jsx("dt",{className:"text-gray-400 uppercase tracking-wide text-[10px]",children:"Live now"}),e.jsx("dd",{className:"text-gray-900",children:n.live})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-gray-400 uppercase tracking-wide text-[10px]",children:"From your landing page"}),e.jsx("dd",{className:"text-gray-900",children:n.suggested??"â"})]})]}),n.missing&&n.missing.length>0&&e.jsxs("p",{className:"text-xs text-amber-700 mt-2",children:["Missing: ",n.missing.join(", ")
6]}),n.extra&&n.extra.length>0&&e.jsxs("p",{className:"text-xs text-gray-500 mt-1",children:["Also on the campaign: ",n.extra.join(", ")]}),n.note&&e.jsx("p",{className:"text-[11px] text-gray-400 mt-1",children:n.note})]},n.key))}):e.jsx("p",{className:"text-sm text-gray-500",children:"Reading the live settings costs a few Google Ads requests, so Mateq only does it when you ask."})]})}const de={none:"No audience set up",partial:"Partly set up",matches:"Matches the landing page",drifted:"Set up differently",unknown:"Not judged yet"},me={none:"bg-amber-100 text-amber-800 border-amber-200",partial:"bg-amber-100 text-amber-800 border-amber-200",matches:"bg-green-100 text-green-800 border-green-200",drifted:"bg-blue-100 text-blue-800 border-blue-200",unknown:"bg-gray-100 text-gray-600 border-gray-200"},ee=["none","partial","drifted","unknown","matches"];function Aa({organizationId:i,adAccountId:r,accountName:a,onOpenCampaign:p}){const[f,g]=t.useState([]),[l,o]=t.useState({}),[b,_]=t.useState(0),[T,y]=t.useState(0),[u,n]=t.useState(0),[s,x]=t.useState(!1),[v,C]=t.useState(null),S=async()=>{x(!0),C(null);try{const d=await He({organizationId:i,platform:"google_ads",adAccountId:r});if(d.status!=="ok"){C(d.status==="failed"?"Mateq could not reach this Google Ads account.":"This check is available for Google Ads accounts only.");return}g(P=>{const L=new Map(P.map(k=>[k.externalId,k]));for(const k of d.campaigns)L.set(k.externalId,k);return[...L.values()]}),o(P=>{const L={...P};for(const[k,D]of Object.entries(d.summary))L[k]=(L[k]??0)+(D??0);return L}),_(P=>P+d.checked),y(d.total),n(d.remaining)}catch(d){C(d instanceof Error?d.message:"The check failed.")}finally{x(!1)}},M=[...f].sort((d,P)=>ee.indexOf(d.state)-ee.indexOf(P.state));return e.jsxs("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 mb-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 bg-[#69a08a] bg-opacity-10 rounded-lg flex items-center justify-center shrink-0",children:e.jsx(te,{className:"h-5 w-5 text-[#69a08a]"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-gray-900",children:"Audience coverage"}),e.jsxs("p",{className:"text-xs text-gray-500",children:["Which campaigns in ",a||`account ${r}`," have an audience set up, read from Google Ads."]})]})]}),e.jsxs("button",{type:"button",onClick:S,disabled:s,className:"flex items-center gap-2 px-3 py-2 text-sm font-medium text-[#69a08a] border border-[#69a08a] rounded-lg hover:bg-green-50 disabled:opacity-50 shrink-0",children:[s&&e.jsx(O,{className:"h-4 w-4 animate-spin"}),s?"Checkingâ¦":u>0?`Check ${u} more`:b>0?"Check again":"Check this account"]})]}),v&&e.jsx("p",{className:"text-sm text-gray-600 bg-gray-50 border border-gray-200 rounded-lg p-3 mb-4",children:v}),b===0&&!v&&e.jsx("p",{className:"text-sm text-gray-500",children:"Each campaign costs a few Google Ads requests, so this runs when you ask for it. No campaign is changed."}),b>0&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex flex-wrap gap-2 mb-4",children:ee.filter(d=>(l[d]??0)>0).map(d=>e.jsxs("span",{className:`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium border ${me[d]}`,children:[e.jsx("span",{className:"font-semibold",children:l[d]}),de[d]]},d))}),e.jsxs("p",{className:"text-xs text-gray-500 mb-3",children:[b," of ",T," campaigns checked",u>0?`, ${u} to go`:"","."]}),e.jsx("div",{className:"divide-y divide-gray-100",children:M.map(d=>e.jsxs("button",{type:"button",onClick:()=>p==null?void 0:p(d.externalId),className:"w-full flex items-center justify-between gap-3 py-2 text-left hover:bg-gray-50 rounded px-2 -mx-2",children:[e.jsx("span",{className:"text-sm text-gray-900 truncate",children:d.name||d.externalId}),e.jsxs("span",{className:"flex items-center gap-2 shrink-0",children:[d.actionable&&e.jsx("span",{className:"text-[11px] text-[#69a08a] font-medium",children:"can be set up"}),e.jsx("span",{className:`inline-flex items-center px-2 py-0.5 rounded-full text-[11px] font-medium border ${me[d.state]}`,children:de[d.state]})]})]},d.externalId))})]})]})}function Ca(i){switch(i){case"synced_ad":return"Taken from the page this campaign's ads point at. Change it to read a different page.";case"asset_group":return"Taken from this campaign's asset groups. Change it to read a different page.";case"user":return"You chose this page. Clear it to fall back to the campaign's own page.";default:return"Mateq will use the page this campaign's ads point at. Type one to override it."}}function Ea({organizationId:i,platform:r,adAccountId:a,campaignExternalId:p,campaignName:f}){const[g,l]=t.useState(null),[o,b]=t.useState(null),[_,T]=t.useState(null),[y,u]=t.useState(null),[n,s]=t.useState(!0),[x,v]=t.useState(!1),[C,S]=t.useState(null),[M,d]=t.useState(null),[P,L]=t.useState(null),[k,D]=t.useState(null),$=Ie(i),E=t.useCallback(async()=>{const{data:m}=await R.from("perf_recommendations").select("id, title, description, action_type, action_payload, apply_mode, status").eq("organization_id",i).eq("platform",r).eq("ad_account_id",a).eq("campaign_external_id",p).eq("category","audie
6nce").eq("status","active").order("created_at",{ascending:!1}).limit(1).maybeSingle();u(m??null)},[i,r,a,p]),U=t.useCallback(async()=>{s(!0);try{const m=await We({organizationId:i,platform:r,adAccountId:a,campaignExternalId:p});m.status==="ok"&&(l(m.audienceTargeting),b(m.landingPageUrl),L(m.landingPageSource),D(m.coverage),T(m.campaign.channelType)),await E()}catch(m){S(m instanceof Error?m.message:"Could not load the audience for this campaign.")}finally{s(!1)}},[i,r,a,p,E]);t.useEffect(()=>{U()},[U]);const I=async()=>{var m,q;v(!0),S(null);try{const w=await Ze({organizationId:i,platform:r,adAccountId:a,campaignExternalId:p,url:(M??"").trim()||void 0,force:!0});w.status==="ok"?(l(w.audienceTargeting),(m=w.page)!=null&&m.url&&b(w.page.url),(q=w.page)!=null&&q.landingPageSource&&L(w.page.landingPageSource),w.warnings.length>0&&S(w.warnings.join(" ")),await E()):w.status==="disabled"?S("Audience targeting is not enabled for this project yet."):w.reason==="no_landing_page"?S("Mateq could not find a landing page for this campaign. Paste the page you advertise and derive again."):S("The landing page could not be read. Try a different URL.")}catch(w){S(w instanceof Error?w.message:"Deriving the audience failed.")}finally{v(!1)}},z=async()=>{if(!(!(y!=null&&y.action_type)||!y.action_payload)){S(null);try{await $.mutateAsync({id:y.id,platform:r,ad_account_id:a,action_type:y.action_type,action_payload:y.action_payload,entity_type:"campaign",entity_external_id:p,campaign_external_id:p}),S("Applied. You can undo it from the action history below."),await E()}catch(m){S(m instanceof Error?m.message:"Applying the audience failed.")}}},G=r==="google_ads",c=(g==null?void 0:g.interests)??[],A=c.filter(m=>{var q;return(q=m.google)==null?void 0:q.userInterestId}),j=g!=null&&g.competitorSegment.enabled?g.competitorSegment.domains:[],N=g==null?void 0:g.demographics;return n?e.jsxs("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6 flex items-center gap-2 text-gray-500",children:[e.jsx(O,{className:"h-4 w-4 animate-spin"}),e.jsx("span",{className:"text-sm",children:"Loadingâ¦"})]}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 mb-5",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 bg-[#69a08a] bg-opacity-10 rounded-lg flex items-center justify-center",children:e.jsx(te,{className:"h-5 w-5 text-[#69a08a]"})}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-gray-900",children:"Audience"}),e.jsxs("p",{className:"text-xs text-gray-500",children:["Derived from the page ",f?`â${f}â `:"this campaign ","advertises. Mateq did not build this campaign, so nothing here changes until you apply it."]})]})]}),e.jsxs("button",{type:"button",onClick:I,disabled:x||!G,className:"flex items-center gap-2 px-3 py-2 text-sm font-medium text-[#69a08a] border border-[#69a08a] rounded-lg hover:bg-green-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors shrink-0",children:[e.jsx(he,{className:`h-4 w-4 ${x?"animate-spin":""}`}),x?"Derivingâ¦":g?"Derive again":"Derive from landing page"]})]}),!G&&e.jsx("p",{className:"text-sm text-gray-500 mb-4",children:"Meta campaigns cannot take audience targeting from Mateq: Meta ads carry no landing page in the sync, and editing a live ad set's targeting restarts its learning phase."}),G&&e.jsxs("div",{className:"mb-4",children:[e.jsx("label",{htmlFor:"external-audience-url",className:"text-xs font-semibold text-gray-500 uppercase tracking-wide block mb-1",children:"Page to read"}),e.jsx("input",{id:"external-audience-url",type:"url",value:M??o??"",onChange:m=>d(m.target.value),placeholder:"https://example.com/offer",className:"w-full max-w-lg px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-[#69a08a]"}),e.jsx("p",{className:"text-[11px] text-gray-400 mt-1",children:M!==null&&M!==o?"Deriving from this page instead of the one Mateq found.":Ca(P)})]}),g&&e.jsxs("div",{className:"space-y-5 text-sm",children:[e.jsxs("div",{className:"pb-4 border-b border-gray-100",children:[e.jsx("label",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wide block mb-2",children:"Interests"}),c.length===0?e.jsx("p",{className:"text-gray-400",children:"No interests yet."}):e.jsx("div",{className:"flex flex-wrap gap-2",children:c.map(m=>{var q,w;return e.jsxs("span",{title:m.rationale,className:`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium border ${(q=m.google)!=null&&q.userInterestId?"bg-[#69a08a]/10 text-[#4d7a67] border-[#69a08a]/30":"bg-gray-100 text-gray-500 border-gray-200"}`,children:[m.label,e.jsx("span",{className:"text-[10px] text-gray-500",children:m.kind==="in_market"?"in-market":"affinity"}),((w=m.google)==null?void 0:w.userInterestId)&&e.jsx("span",{className:"text-[10px] text-blue-700",children:"Google â"})]},m.label)})}),c.length>A.length&&e.jsx("p",{className:"text-[11px] text-gray-400 mt-2",children:"Greyed-out labels have no matching Google audience segment, so they are not part of the apply."})]}),e.jsxs("div",{className:"pb-4 border-b border-gray-100",children:[e.jsx("label",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wide block mb-2",children:"Competitor segment"}),j.length===0?e.jsx("p",{className:"text-gray-400",children:"No competitor sites found."}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex flex-wrap gap-2",children:j.map(m=>e.jsx("span",{className:"inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-700 border border-gray-200",children:m.domain},m.domain))}),e.jsx("p",{className:"text-[11px] text-gray-400 mt-2",children:_==="PERFORMANCE_MAX"?"These become a custom segment of people who browse sites like these, attached as a signal.":"Google offers custom segments on Performance Max, not on Search, so these are not part of the apply here."})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wide block mb-2",children:"Demographics"}),e.jsx("p",{className:"text-gray-900",children:N!=null&&N.ageMin||N!=null&&N.ageMax?`${N.ageMin??18}â${N.ageMax??65}`:"All ages"}),(N==null?void 0:N.rationale)&&e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:N.rationale}),e.jsx("p",{className:"text-[11px] text-gray-400 mt-1",children:"Age and gender are shown as insight only. They restrict who a live campaign reaches, so Mateq does not write them to a campaign it did not build."})]})]}),C&&e.jsx("p",{className:"text-sm text-gray-600 mt-4 bg-gray-50 border border-gray-200 rounded-lg p-3",children:C})]}),G&&e.jsx(ka,{organizationId:i,platform:r,adAccountId:a,campaignExternalId:p,initial:k,onChecked:()=>void E()}),G&&e.jsx(Aa,{organizationId:i,adAccountId:a,onOpenCampaign:m=>{m!==p&&window.location.assign(window.location.pathname.replace(/[^/]+$/,m))}}),y&&e.jsx("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Ee,{className:"h-5 w-5 text-[#69a08a] shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h3",{className:"font-semibold text-gray-900",children:y.title}),e.jsx("p",{className:"text-sm text-gray-600 mt-1",children:y.description}),y.action_type?e.jsxs("button",{type:"button",onClick:z,disabled:$.isPending,className:"mt-4 flex items-center gap-2 px-4 py-2 bg-[#69a08a] text-white text-sm font-medium rounded-lg hover:bg-[#5a8d78] disabled:opacity-50",children:[$.isPending&&e.jsx(O,{className:"h-4 w-4 animate-spin"}),"Review and apply"]}):e.jsx("p",{className:"text-xs text-gray-400 mt-3",children:"This one cannot be applied from Mateq right now â the reason is in the text above."})]})]})})]})}function Lt(){const{platform:i,externalId:r}=Te(),a=Pe(),{currentOrganization:p,user:f}=qe(),{lang:g}=Me(),l=p||"",[o,b]=t.useState(null),[_,T]=t.useState([]),[y]=Le(),u=["performance","search_terms","audie
6nce","placements","structure","automation"],n=y.get("tab"),s=u.includes(n??"")?n:"performance",[x,v]=t.useState(s),[C,S]=t.useState(!0),[M,d]=t.useState(null),[P,L]=t.useState([]),[k,D]=t.useState(!1),[$,E]=t.useState(!1),[U,I]=t.useState(""),[z,G]=t.useState("30d"),[c,A]=t.useState(!1),[j,N]=t.useState(void 0),[m,q]=t.useState(0),w=De(l||null,(o==null?void 0:o.platform)??null,(o==null?void 0:o.ad_account_id)??null,(o==null?void 0:o.external_id)??null,{scope:"campaign"});t.useEffect(()=>{i&&r&&l&&X()},[i,r,l]);async function X(){S(!0);try{const{data:h,error:Y}=await R.from("perf_external_entities").select("*").eq("organization_id",l).eq("platform",i).eq("entity_type","campaign").eq("external_id",r).maybeSingle();if(Y||!h){d("Campaign not found"),S(!1);return}b(h);const{data:Q}=await R.from("perf_external_entities").select("entity_type, external_id, entity_name, entity_status").eq("organization_id",l).eq("platform",i).eq("ad_account_id",h.ad_account_id).eq("parent_external_id",r).order("entity_type");T(Q||[]);const Se=await Ke(l);L(Se)}catch{d("Failed to load campaign")}S(!1)}async function fe(){if(!(!o||k)){D(!0);try{await re(l,o.ad_account_id,o.platform,{syncType:"incremental"}),await X()}catch(h){d(h instanceof Error?h.message:"Sync failed")}D(!1)}}async function be(){if(!(!o||$)){E(!0),d(null);try{I("Syncing data..."),await re(l,o.ad_account_id,o.platform,{syncType:"incremental"}),I("Analyzing campaigns..."),await oe(l,o.ad_account_id,o.platform),I("Loading results..."),await X()}catch(h){d(h instanceof Error?h.message:"Scan failed")}E(!1),I("")}}async function je(){if(!(!o||$)){E(!0),d(null);try{I("Analyzing campaigns..."),await oe(l,o.ad_account_id,o.platform),await X()}catch(h){d(h instanceof Error?h.message:"Analysis failed")}E(!1),I("")}}if(C)return e.jsx("div",{className:"flex items-center justify-center min-h-[400px]",children:e.jsx(O,{className:"h-6 w-6 animate-spin text-gray-400"})});if(!o)return e.jsxs("div",{className:"p-6",children:[e.jsxs("button",{onClick:()=>a(`/${g}/campaigns`),className:"flex items-center text-sm text-gray-500 hover:text-gray-700 mb-4",children:[e.jsx(ie,{className:"h-4 w-4 mr-1"})," Back to Campaigns"]}),e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(W,{className:"h-12 w-12 mx-auto mb-3 text-gray-300"}),e.jsx("p",{children:"Campaign not found"})]})]});const F=o.platform,H=ne(o.entity_status,F),ve=H.charAt(0).toUpperCase()+H.slice(1),Z=o.metadata||{},se=Z.channel_sub_type||Z.channel_type||Z.objective||null,Ne=w.data&&w.data.length>0?[...w.data].map(h=>h.evaluated_at).sort().reverse()[0]:null,B=P.find(h=>h.platform===o.platform&&h.ad_account_id===o.ad_account_id&&h.sync_scope==="metrics")??null,J=$&&U!=="Syncing data...",we=ze({cursorDate:(B==null?void 0:B.last_synced_date)??null,cursorUpdatedAt:(B==null?void 0:B.updated_at)??null,analyzedAt:Ne,recommendationsAt:null,rangeStart:Ge(z),isSyncing:k||$&&!J,isAnalyzing:J}),_e=J?"analyzing":k||$?"syncing":M?"failed":"idle";return e.jsxs("div",{className:"space-y-4 p-6",children:[e.jsxs("div",{children:[e.jsxs("button",{onClick:()=>a(`/${g}/campaigns`),className:"flex items-center text-sm text-gray-500 hover:text-gray-700 mb-4 transition-colors",children:[e.jsx(ie,{className:"h-4 w-4 mr-1"})," Back to Campaigns"]}),e.jsxs("div",{className:"flex items-center justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"flex items-center space-x-3 min-w-0",children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900 truncate",children:o.entity_name||`Campaign ${r}`}),e.jsx("span",{className:`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${H==="active"?"bg-green-100 text-green-800":H==="paused"?"bg-yellow-100 text-yellow-800":"bg-gray-100 text-gray-800"}`,children:ve}),e.jsx("span",{className:`inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium border ${Qe[F]}`,children:Je[F]}),se&&e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-600",children:String(se).replace(/_/g," ")})]}),x!=="performance"&&e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(Fe,{value:z,onChange:G}),e.jsx(Oe,{model:we,variant:"chip"}),e.jsx(Re,{phase:_e,onSyncAndAnalyze:be,onSyncOnly:fe,onAnalyzeOnly:je})]})]})]}),M&&e.jsxs("div",{className:"flex items-center space-x-2 p-3 rounded-lg bg-red-50 text-red-700",children:[e.jsx(W,{className:"h-4 w-4 flex-shrink-0"}),e.jsx("p",{className:"text-sm",children:M}),e.jsx("button",{onClick:()=>d(null),className:"ml-auto text-red-400 hover:text-red-600",children:"Ã"})]}),e.jsx("div",{className:"border-b border-gray-200",children:e.jsxs("nav",{className:"flex space-x-8",children:[e.jsxs("button",{onClick:()=>v("performance"),className:`flex items-center space-x-2 pb-3 px-1 border-b-2 text-sm font-medium transition-colors ${x==="performance"?"border-[#69a08a] text-[#69a08a]":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:[e.jsx(le,{className:"h-4 w-4"}),e.jsx("span",{children:"Performance"})]}),F==="google_ads"&&e.jsxs("button",{onClick:()=>v("search_terms"),className:`flex items-center space-x-2 pb-3 px-1 border-b-2 text-sm font-medium transition-colors ${x==="search_terms"?"border-[#69a08a] text-[#69a08a]":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:[e.jsx($e,{className:"h-4 w-4"}),e.jsx("span",{children:"Search Terms"})]}),F==="google_ads"&&e.jsxs("button",{onClick:()=>v("audience"),className:`flex items-center space-x-2 pb-3 px-1 border-b-2 text-sm font-medium transition-colors ${x==="audience"?"border-[#69a08a] text-[#69a08a]":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:[e.jsx(te,{className:"h-4 w-4"}),e.jsx("span",{children:"Audience"})]}),F==="meta_ads"&&e.jsxs("button",{onClick:()=>v("placements"),className:`flex items-center space-x-2 pb-3 px-1 border-b-2 text-sm font-medium transition-colors ${x==="placements"?"border-[#69a08a] text-[#69a08a]":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:[e.jsx(le,{className:"h-4 w-4"}),e.jsx("span",{children:"Placements"})]}),e.jsxs("button",{onClick:()=>v("structure"),className:`flex items-center space-x-2 pb-3 px-1 border-b-2 text-sm font-medium transition-colors ${x==="structure"?"border-[#69a08a] text-[#69a08a]":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:[e.jsx(oa,{className:"h-4 w-4"}),e.jsx("span",{children:"Structure"}),_.length>0&&e.jsxs("span",{className:"text-xs text-gray-400",children:["(",_.length,")"]})]}),e.jsxs("button",{onClick:()=>v("automation"),className:`flex items-center space-x-2 pb-3 px-1 border-b-2 text-sm font-medium transition-colors ${x==="automation"?"border-[#69a08a] text-[#69a08a]":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:[e.jsx(ta,{className:"h-4 w-4"}),e.jsx("span",{children:"Automation"})]})]})}),x==="performance"&&e.jsx(Ue,{organizationId:l,platform:F,adAccountId:o.ad_account_id,campaignExternalId:r,campaignName:o.entity_name||void 0}),x==="search_terms"&&e.jsx("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:e.jsx(Be,{organizationId:l,adAccountId:o.ad_account_id,campaignExternalId:r})}),x==="placements"&&e.jsx("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:e.jsx(Ye,{organizationId:l,adAccountId:o.ad_account_id,campaignExternalId:r})}),x==="audience"&&e.jsx(Ea,{organizationId:l,platform:F,adAccountId:o.ad_account_id,campaignExternalId:r,campaignName:o.entity_name}),x==="structure"&&e.jsxs("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:[e.jsx("h2",{className:"text-lg font-semibold text-gray-900 mb-4",children:"Campaign Structure"}),_.length===0?e.jsx("p",{className:"text-sm text-gray-500",children:"No child entities discovered yet. Run a sync to discover ad groups, ad sets, and ads."}):e.jsx("div",{className:"space-y-2",children:_.map(h=>{const Y=ne(h.entity_status,F),Q=h.entity_type==="ad_group"?"Ad Group":h.entity_type==="ad_set"?"Ad Set":h.entity_type==="ad"?"Ad":h.entity_type;return e.jsxs("div",{className:"flex items-center justify-between py-2 px-3 rounded-lg hover:bg-gray-50",children:[e.jsxs("div",{className:"flex items-center space-x-3",children:[e.jsx("span",{className:"text-xs font-medium text-gray-400 uppercase w-16",children:Q}),e.jsx("span",{className:"text-sm text-gray-900",children:h.entity_name||h.external_id})]}),e.jsx("span",{className:`inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium ${Y==="active"?"bg-green-100 text-green-700":Y==="paused"?"bg-yellow-100 text-yellow-700":"bg-gray-100 text-gray-600"}`,children:Y})]},h.external_id)})})]}),x==="automation"&&e.jsxs("div",{className:"space-y-6",children:[e.jsx(wa,{organizationId:l}),e.jsx("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-6",children:e.jsx(ya,{organizationId:l,onCreateRule:()=>{N(void 0),A(!0)},onEditRule:h=>{N(h),A(!0)}},m)})]}),c&&e.jsx(Na,{organizationId:l,userId:(f==null?void 0:f.id)||"",existingRule:j,onClose:()=>{A(!1),N(void 0)},onSaved:()=>{A(!1),N(void 0),q(h=>h+1)}})]})}export{Lt as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.