1import{j as e}from"./vendor-editor-BX0sR0z-.js";import{r as n}from"./vendor-react-BPk9u-w4.js";import{u as Y}from"./ConfirmProvider-DHQTVUCD.js";import{h as j,l as $,d as K,m as O}from"./index-FZUSOwo2.js";import{a as Z,u as ee}from"./usePortalProductFlavor-BNKE5Z5W.js";import{b as te}from"./navigation.admin-DU2BEEXu.js";import{toast as x}from"./vendor-sonner-DPZ7yCFm.js";import{d as ae,y as M,u as se,i as re,X as le,bo as W,L as B,P as oe,b as ne,aD as ie,l as ce,k as de}from"./vendor-icons-D7M_7Je-.js";import{A as xe}from"./index-P8wGLujW.js";import"./vendor-charts-Cf-ruFca.js";import"./vendor-ui-utils-Bbg69IUi.js";import"./useEscapeKey-BwRSuhVP.js";import"./vendor-graph-Df3mHyA7.js";import"./vendor-supabase-UBwEW4nm.js";import"./vendor-helmet-C74EPpLp.js";import"./portalProductTerms-BbgEIXua.js";import"./painelUrl-BpfzNXlx.js";const ue="https://api.cattive.com",me=i=>{const[N,w]=n.useState([]),[G,C]=n.useState(!0),c=n.useCallback(async()=>{try{C(!0);const{data:s,error:a}=await j.from("tts_audio_spots").select("*").eq("portal_id",i).order("weight",{ascending:!1});if(a)throw a;w(s||[])}catch(s){$.error("[TTS Spots] Error fetching:",s.message)}finally{C(!1)}},[i]),u=async(s,a)=>{try{const r=a!==void 0&&s.tts_text!==a,m={...s,portal_id:i,updated_at:new Date().toISOString(),start_date:s.start_date||null,end_date:s.end_date||null};r&&(m.audio_url=null);const{error:v}=await j.from("tts_audio_spots").upsert(m);if(v)throw v;return x.success(r?"Spot salvo! Cache de áudio invalidado.":"Spot salvo com sucesso!"),await c(),!0}catch(r){return $.error("[TTS Spots] Error saving:",r.message),x.error("Erro ao salvar spot."),!1}},E=async s=>{try{const{error:a}=await j.from("tts_audio_spots").delete().eq("id",s).eq("portal_id",i);if(a)throw a;return x.success("Spot excluÃdo!"),await c(),!0}catch{return x.error("Erro ao excluir spot."),!1}},P=async(s,a)=>{const r=a==="active"?"paused":"active";try{const{error:m}=await j.from("tts_audio_spots").update({status:r,updated_at:new Date().toISOString()}).eq("id",s).eq("portal_id",i);if(m)throw m;return x.success(r==="active"?"Spot ativado!":"Spot pausado!"),await c(),!0}catch{return x.error("Erro ao alterar status."),!1}},z=async(s,a)=>{try{const{error:r}=await j.from("tts_audio_spots").update({weight:a,updated_at:new Date().toISOString()}).eq("id",s).eq("portal_id",i);if(r)throw r;return await c(),!0}catch{return x.error("Erro ao atualizar peso."),!1}},A=async s=>{try{const{error:a}=await j.from("tts_audio_spots").update({audio_url:null,updated_at:new Date().toISOString()}).eq("id",s).eq("portal_id",i);if(a)throw a;return x.success("Cache de áudio limpo."),await c(),!0}catch{return x.error("Erro ao limpar cache."),!1}},k=async s=>{try{const{data:{session:a}}=await j.auth.getSession(),r=await fetch(`${ue}/functions/v1/tts-generate-stream`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${(a==null?void 0:a.access_token)||""}`},body:JSON.stringify({mode:"generate_spot",spot_id:s})});if(!r.ok){const v=await r.json().catch(()=>({error:"Unknown error"}));throw new Error(v.error||`HTTP ${r.status}`)}const m=await r.json();return x.success("Ãudio gerado e cacheado com sucesso!"),await c(),m.audio_url||null}catch(a){return $.error("[TTS Spots] Error generating audio:",a.message),x.error(`Erro ao gerar áudio: ${a.message}`),null}};return n.useEffect(()=>{i&&c()},[i,c]),{spots:N,loading:G,saveSpot:u,deleteSpot:E,toggleStatus:P,updateWeight:z,clearAudioCache:A,generateSpotAudio:k,refresh:c}},pe=i=>({portal_feature:{label:`Destaque do ${i}`,color:"bg-blue-50 text-blue-600"},product:{label:"Produto",color:"bg-purple-50 text-purple-600"},event:{label:"Evento",color:"bg-amber-50 text-amber-600"},seasonal:{label:"Sazonal",color:"bg-rose-50 text-rose-600"},sponsor:{label:"Patrocinador",color:"bg-emerald-50 text-emerald-600"}}),Ae=()=>{const i=Y(),{currentPortal:N}=K(),w=Z(),C=ee()==="blog"&&te("/admin/ads/tts-spots")||"Spots de Ãudio TTS",c=n.useMemo(()=>pe(w.siteNoun.charAt(0).toUpperCase()+w.siteNoun.slice(1)),[w.siteNoun]),{spots:u,loading:E,saveSpot:P,deleteSpot:z,toggleStatus:A,generateSpotAudio:k}=me(N.id),[s,a]=n.useState(""),[r,m]=n.useState("all"),[v,f]=n.useState(!1),[L,h]=n.useState(null),[H,T]=n.useState(null),g=n.useRef(null),[q,U]=n.useState(""),[o,b]=n.useState({title:"",tts_text:"",category:"portal_feature",weight:100,status:"active",tags:[]}),R=n.useMemo(()=>u.filter(t=>{const l=t.title.toLowerCase().includes(s.toLowerCase())||t.tts_text.toLowerCase().includes(s.toLowerCase()),d=r==="all"||t.status===r;return l&&d}),[u,s,r]),S=n.useMemo(()=>({total:u.length,active:u.filter(t=>t.status==="active").length,totalPlays:u.reduce((t,l)=>t+(l.total_plays||0),0),cached:u.filter(t=>!!t.audio_url).length}),[u]),I=async t=>{t.preventDefault(),await P({...o,portal_id:N.id},q)&&(f(!1),D())},D=()=>{b({title:"",tts_text:"",category:"portal_feature",weight:100,status:"active",tags:[]}),U("")},Q=async t=>{await i({title:"Excluir Spot",message:"Este spot será removido permanentemente.",confirmLabel:"Excluir",variant:"danger"})&&await z(t)},V=async t=>{var p;if(L===t.id){(p=g.current)==null||p.pause(),h(null);return}g.current&&(g.current.pause(),g.current.currentTime=0);let l=t.audio_url;if(!l&&(T(t.id),l=await k(t.id),T(null),!l))return;const d=new Audio(l);g.current=d,d.onended=()=>h(null),d.onerror=()=>{h(null)};try{await d.play(),h(t.id)}catch{h(null)}},J=async t=>{var d;L===t.id&&((d=g.current)==null||d.pause(),h(null)),T(t.id);const l=await k(t.id);if(T(null),l){const p=new Audio(l);g.current=p,p.onended=()=>h(null);try{await p.play(),h(t.id)}catch{}}},X=t=>{b({...t}),U(t.tts_text),f(!0)},_="w-full px-3 py-2 bg-white border border-slate-200 rounded-lg focus:outline-none focus:border-slate-400 text-[13px] text-slate-800 transition-colors placeholder:text-slate-400",F=t=>{const l=t.trim().split(/\s+/).length;return Math.round(l/2.5)};return e.jsxs("div",{className:"h-full flex flex-col bg-white",children:[e.jsxs("div",{className:"flex-shrink-0 border-b border-slate-200/80",children:[e.jsxs("div",{className:"px-8 py-5 flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-lg font-semibold text-slate-900 tracking-tight flex items-center gap-2",children:[e.jsx(ae,{size:20,className:"text-slate-400"}),C]}),e.jsx("p",{className:"text-[13px] text-slate-400 mt-0.5",children:"Propagandas promocionais inseridas entre a saudação e a leitura do conteúdo"})]}),e.jsxs("button",{onClick:()=>{D(),f(!0)},className:"bg-slate-900 text-white px-4 py-2 rounded-lg text-[13px] font-medium hover:bg-slate-800 transition-colors flex items-center gap-1.5",children:[e.jsx(M,{size:15,strokeWidth:2.5})," Novo Spot"]})]}),e.jsx("div",{className:"px-8 pb-4 flex items-center gap-8",children:[{label:"Total",value:S.total},{label:"Ativos",value:S.active}
1,{label:"Reproduções",value:S.totalPlays.toLocaleString("pt-BR")},{label:"Com Cache",value:`${S.cached}/${S.total}`}].map(t=>e.jsxs("div",{className:"flex items-baseline gap-2",children:[e.jsx("span",{className:"text-xs text-slate-400 font-medium",children:t.label}),e.jsx("span",{className:"text-sm font-semibold text-slate-700 tabular-nums",children:t.value})]},t.label))})]}),e.jsxs("div",{className:"flex-shrink-0 border-b border-slate-200/80 px-8 py-3 flex items-center gap-3",children:[e.jsxs("div",{className:"relative flex-1 max-w-sm",children:[e.jsx(se,{className:"absolute left-3 top-1/2 -translate-y-1/2 text-slate-300",size:15}),e.jsx("input",{type:"text",placeholder:"Buscar...",className:"w-full pl-9 pr-8 py-1.5 bg-transparent border border-slate-200 rounded-lg text-[13px] focus:outline-none focus:border-slate-400 placeholder:text-slate-300",value:s,onChange:t=>a(t.target.value)})]}),e.jsxs("div",{className:"relative",children:[e.jsxs("select",{className:"appearance-none bg-transparent border border-slate-200 rounded-lg pl-3 pr-7 py-1.5 text-[13px] font-medium text-slate-500 focus:outline-none cursor-pointer",value:r,onChange:t=>m(t.target.value),children:[e.jsx("option",{value:"all",children:"Status"}),e.jsx("option",{value:"active",children:"Ativo"}),e.jsx("option",{value:"paused",children:"Pausado"}),e.jsx("option",{value:"draft",children:"Rascunho"})]}),e.jsx(re,{size:12,className:"absolute right-2 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none"})]}),e.jsxs("div",{className:"ml-auto text-[12px] text-slate-400 tabular-nums",children:[R.length," de ",u.length]})]}),e.jsx(xe,{children:v&&e.jsx(O.div,{initial:{opacity:0}
1,animate:{opacity:1},exit:{opacity:0},className:"fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-900/40 backdrop-blur-sm",onClick:()=>f(!1),children:e.jsxs(O.form,{initial:{scale:.95,opacity:0},animate:{scale:1,opacity:1},exit:{scale:.95,opacity:0},onClick:t=>t.stopPropagation(),onSubmit:I,className:"bg-white rounded-2xl shadow-xl w-full max-w-lg p-6 space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-[15px] font-semibold text-slate-900",children:o.id?"Editar Spot":"Novo Spot de Ãudio"}),e.jsx("button",{type:"button",onClick:()=>f(!1),className:"text-slate-400 hover:text-slate-600",children:e.jsx(le,{size:18})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-[12px] font-medium text-slate-500 mb-1.5",children:"TÃtulo"}),e.jsx("input",{type:"text",required:!0,placeholder:"Ex: Canal WhatsApp",className:_,value:o.title||"",onChange:t=>b({...o,title:t.target.value})})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-[12px] font-medium text-slate-500 mb-1.5",children:["Texto para TTS ",e.jsx("span",{className:"text-slate-300",children:"(será convertido em áudio)"})]}),e.jsx("textarea",{required:!0,rows:3,placeholder:`Ex: Você sabia que o ${N.name}...`,className:`${_} resize-y min-h-[80px]`,value:o.tts_text||"",onChange:t=>b({...o,tts_text:t.target.value})}),e.jsxs("div",{className:"flex items-center justify-between mt-1",children:[e.jsxs("p",{className:"text-[11px] text-slate-400",children:["~",F(o.tts_text||""),"s de áudio ·"," ",(o.tts_text||"").split(/\s+/).filter(Boolean).length," palavras"]}),o.id&&q&&o.tts_text!==q&&e.jsxs("span",{className:"text-[11px] text-amber-500 font-medium flex items-center gap-1",children:[e.jsx(W,{size:10})," Texto alterado â cache será invalidado ao salvar"]})]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-[12px] font-medium text-slate-500 mb-1.5",children:"Categoria"}),e.jsx("select",{className:_,value:o.category,onChange:t=>b({...o,category:t.target.value}),children:Object.entries(c).map(([t,l])=>e.jsx("option",{value:t,children:l.label},t))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-[12px] font-medium text-slate-500 mb-1.5",children:"Peso"}),e.jsx("input",{type:"number",required:!0,min:1,max:500,className:_,value:o.weight||100,onChange:t=>b({...o,weight:parseInt(t.target.value)||100})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-[12px] font-medium text-slate-500 mb-1.5",children:"Status"}),e.jsxs("select",{className:_,value:o.status,onChange:t=>b({...o,status:t.target.value}),children:[e.jsx("option",{value:"active",children:"Ativo"}),e.jsx("option",{value:"paused",children:"Pausado"}),e.jsx("option",{value:"draft",children:"Rascunho"})]})]})]}),e.jsxs("div",{className:"flex justify-end pt-3 border-t border-slate-100 gap-2",children:[e.jsx("button",{type:"button",onClick:()=>f(!1),className:"px-3 py-1.5 rounded-lg text-[13px] font-medium text-slate-500 hover:bg-slate-100",children:"Cancelar"}),e.jsx("button",{type:"submit",className:"px-4 py-1.5 rounded-lg text-[13px] font-medium text-white bg-slate-900 hover:bg-slate-800",children:"Salvar"})]})]})})}),e.jsx("div",{className:"flex-1 overflow-y-auto",children:E?e.jsxs("div",{className:"h-full flex flex-col items-center justify-center gap-3",children:[e.jsx(B,{className:"w-5 h-5 text-slate-300 animate-spin"}),e.jsx("p",{className:"text-[13px] text-slate-400",children:"Carregando spots..."})]}):R.length>0?e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"sticky top-0 z-10",children:e.jsxs("tr",{className:"border-b border-slate-200/80 bg-slate-50/80 backdrop-blur-sm",children:[e.jsx("th",{className:"text-left px-8 py-2.5 w-[50px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"â¶"})}),e.jsx("th",{className:"text-left px-4 py-2.5",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"TÃtulo / Texto"})}),e.jsx("th",{className:"text-left px-4 py-2.5 w-[130px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"Categoria"})}),e.jsx("th",{className:"text-center px-4 py-2.5 w-[80px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"Status"})}),e.jsx("th",{className:"text-center px-4 py-2.5 w-[70px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"Peso"})}
1),e.jsx("th",{className:"text-center px-4 py-2.5 w-[60px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"~Duração"})}),e.jsx("th",{className:"text-right px-4 py-2.5 w-[100px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"Reproduções"})}),e.jsx("th",{className:"text-center px-4 py-2.5 w-[60px]",children:e.jsx("span",{className:"text-xs font-medium text-slate-400",children:"Cache"})}),e.jsx("th",{className:"text-right px-8 py-2.5 w-[130px]"})]})}),e.jsx("tbody",{children:R.map((t,l)=>{const d=c[t.category]||c.portal_feature,p=L===t.id,y=H===t.id;return e.jsxs(O.tr,{initial:{opacity:0},animate:{opacity:1},transition:{delay:l*.01},className:"border-b border-slate-100 hover:bg-slate-50/60 transition-colors group",children:[e.jsx("td",{className:"px-8 py-3",children:e.jsx("button",{onClick:()=>V(t),disabled:y,className:`w-8 h-8 rounded-full flex items-center justify-center transition-all ${y?"bg-blue-50 text-blue-500":p?"bg-slate-900 text-white scale-105 shadow-md":"bg-slate-100 text-slate-500 hover:bg-slate-200 hover:scale-105"}`,title:y?"Gerando áudio...":t.audio_url?"Reproduzir":"Gerar e reproduzir",children:y?e.jsx(B,{size:13,className:"animate-spin"}):p?e.jsx(oe,{size:13}):e.jsx(ne,{size:13,className:"ml-0.5"})})}),e.jsxs("td",{className:"px-4 py-3",children:[e.jsx("p",{className:"text-[13px] font-medium text-slate-800 truncate",children:t.title}),e.jsx("p",{className:"text-[12px] text-slate-400 truncate max-w-md mt-0.5",children:t.tts_text})]}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:`text-[11px] font-medium px-2 py-0.5 rounded-md ${d.color}`,children:d.label})}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("div",{className:"flex justify-center",children:e.jsx("button",{onClick:()=>A(t.id,t.status),className:`relative inline-flex items-center h-[18px] w-[32px] rounded-full transition-colors ${t.status==="active"?"bg-emerald-500":"bg-slate-200"}`,children:e.jsx("span",{className:`inline-block h-[14px] w-[14px] rounded-full bg-white shadow-sm transform transition-transform ${t.status==="active"?"translate-x-[16px]":"translate-x-[2px]"}`})})})}),e.jsx("td",{className:"px-4 py-3 text-center",children:e.jsx("span",{className:"text-[13px] text-slate-600 tabular-nums font-medium",children:t.weight})}),e.jsx("td",{className:"px-4 py-3 text-center",children:e.jsxs("span",{className:"text-[12px] text-slate-400 tabular-nums",children:["~",F(t.tts_text),"s"]})}),e.jsx("td",{className:"px-4 py-3 text-right",children:e.jsx("span",{className:"text-[13px] text-slate-600 tabular-nums font-medium",children:(t.total_plays||0).toLocaleString("pt-BR")})}),e.jsx("td",{className:"px-4 py-3 text-center",children:t.audio_url?e.jsx("span",{className:"w-2 h-2 rounded-full bg-emerald-400 inline-block",title:"Ãudio em cache"}):e.jsx("span",{className:"w-2 h-2 rounded-full bg-slate-200 inline-block",title:"Clique â¶ para gerar"})}),e.jsx("td",{className:"px-8 py-3",children:e.jsxs("div",{className:"flex items-center justify-end gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity",children:[e.jsx("button",{onClick:()=>X(t),className:"p-1.5 text-slate-400 hover:text-slate-700 rounded-md hover:bg-slate-100",title:"Editar",children:e.jsx(ie,{size:14})}),e.jsx("button",{onClick:()=>J(t),disabled:y,className:"p-1.5 text-slate-400 hover:text-blue-600 rounded-md hover:bg-blue-50",title:t.audio_url?"Regenerar áudio (exclui cache antigo)":"Gerar áudio",children:y?e.jsx(B,{size:14,className:"animate-spin"}):e.jsx(W,{size:14})}),e.jsx("button",{onClick:()=>Q(t.id),className:"p-1.5 text-slate-400 hover:text-red-600 rounded-md hover:bg-red-50",title:"Excluir",children:e.jsx(ce,{size:14})})]})})]},t.id)})})]}):e.jsxs("div",{className:"h-full flex flex-col items-center justify-center gap-2",children:[e.jsx("div",{className:"w-10 h-10 rounded-full bg-slate-100 flex items-center justify-center",children:e.jsx(de,{size:18,className:"text-slate-300"})}),e.jsx("p",{className:"text-[13px] text-slate-500 font-medium",children:"Nenhum spot encontrado"}),e.jsx("p",{className:"text-[12px] text-slate-400 max-w-xs text-center",children:"Spots são reproduzidos entre a saudação e a leitura do conteúdo."}),e.jsxs("button",{onClick:()=>{D(),f(!0)},className:"mt-1 text-[13px] bg-slate-900 text-white px-3.5 py-1.5 rounded-lg font-medium hover:bg-slate-800 flex items-center gap-1",children:[e.jsx(M,{size:14})," Criar spot"]})]})})]})};export{Ae as AdsTtsSpots};
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.