1import{b as U,u as R,c as T,j as e}from"./vendor-query-Cwv8E7Zv.js";import{c as $,u as H,b as K,B as b,F as W,s as f,I as G,a as A}from"./index-C0fP_BvB.js";import{C as B,a as M,b as J,c as X}from"./card-DYR0t1yt.js";import{L as q}from"./label-DGLoJ0Mu.js";import{T as Y}from"./textarea-Dip0ZPzO.js";import{S as Z}from"./switch-DREX_cgA.js";import{D as V,f as ee,a as z,b as F,c as I}from"./dialog-B1lQToto.js";import{S as se,a as ae,b as te,c as re,d as ie}from"./select-B9QlOF_y.js";import{r as n}from"./vendor-react-Dl4O_m7m.js";import{B as ne}from"./badge-CWXJFI6L.js";import{P as ce}from"./plus-Qa7KXAZ8.js";import{P as oe}from"./pencil-Di6YiyVf.js";import{T as le}from"./trash-2-BI7sokss.js";import{E as D}from"./eye-vVEDMsFu.js";import"./vendor-ui-CVsjces5.js";import"./vendor-charts-BWQ58F1S.js";import"./vendor-supabase-DfwRNDmx.js";import"./chevron-down-I8OUiNnH.js";import"./check-BfBwRiOs.js";/** 2 * @license lucide-react v0.462.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 de=$("Info",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"M12 16v-4",key:"1dtifu"}],["path",{d:"M12 8h.01",key:"e9boi3"}]]),Q=[{key:"name",desc:"Nome do produto"},{key:"price_current",desc:"Preço atual (formatado)"},{key:"price_avg",desc:"Preço médio 30d"},{key:"price_lowest",desc:"Menor preço 30d"},{key:"discount",desc:"Desconto em %"},{key:"score",desc:"Score público"},{key:"link",desc:"Link de rastreamento"},{key:"marketplace",desc:"Marketplace (UPPER)"},{key:"image",desc:"URL da imagem"},{key:"veredito",desc:"Veredito (ex: Ãtima oferta!)"}];function me(d,t,p){if(!t)return"(selecione um produto para preview)";const r=l=>l!=null?l.toLocaleString("pt-BR",{style:"currency",currency:"BRL"}):"â",m=p||{},h=m.score_public??m.score_final_public??0,j=t.score_boost??0,c=Math.max(0,Math.min(100,Number(h)+j)),u=l=>l<60?"Não é o melhor momento":l<75?"Preço normal":l<85?"Bom preço":"Ãtima oferta!",g={name:t.name??"",price_current:r(m.price_current),price_avg:r(m.price_average_30d),price_lowest:r(m.price_lowest_30d),discount:String(Math.round(m.discount_percent??0)),score:String(Math.round(c)),link:`${window.location.origin}/go/abc123`,marketplace:(t.marketplace??"").toUpperCase(),image:t.image_url??"",veredito:u(c)};return d.replace(/\{\{(\w+)\}\}/g,(l,v)=>g[v]??l)}function pe(d){const t=new Set(Q.map(r=>r.key));return[...d.matchAll(/\{\{(\w+)\}\}/g)].map(r=>r[1]).filter(r=>!t.has(r))}function O({name:d,body:t,onNameChange:p,onBodyChange:r,onSave:m,saving:h,saveLabel:j,orgId:c}){const[u,g]=n.useState(""),[l,v]=n.useState(null),[k,S]=n.useState(!1),{toast:y}=K(),{data:i}=R({queryKey:["preview-products",c],enabled:!!c,queryFn:async()=>{const{data:a}=await f.from("products").select("id, name, marketplace, image_url, score_public, score_boost, verdict_override, price, product_metrics(price_current, price_average_30d, price_lowest_30d, discount_percent, score_public, score_final_public)").eq("organization_id",c).is("deleted_at",null).eq("archived",!1).order("created_at",{ascending:!1}).limit(20);return a??[]}});n.useEffect(()=>{i!=null&&i.length&&!u&&g(i[0].id)},[i,u]);const o=(i==null?void 0:i.find(a=>a.id===u))??null,N=o?Array.isArray(o.product_metrics)?o.product_metrics[0]:o.product_metrics:null,x=n.useMemo(()=>me(t,o,N),[t,o,N]),w=n.useMemo(()=>pe(t),[t]),C=n.useCallback(async()=>{if(!(!o||!c)){S(!0);try{const{data:a}=await f.from("message_templates").select("id").eq("organization_id",c).limit(1);v(x),y({title:"â Preview validado",description:"Template renderizado com sucesso"})}catch(a){y({title:"Erro na validação",description:a.message,variant:"destructive"})}finally{S(!1)}}},[o,c,x,y]),E=d.trim().length>0&&t.trim().length>0&&w.length===0;return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(q,{children:"Nome *"}),e.jsx(G,{placeholder:"Ex: Oferta Relâmpago",value:d,onChange:a=>p(a.target.value)})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(q,{children:"Corpo da mensagem *"}),e.jsx(Y,{placeholder:`ð¥ {{name}} 7 8ð° De: ~~{{price_avg}}~~ 9ð Por: {{price_current}} 10ð {{discount}}% OFF 11â {{veredito}} 12 13ð {{link}}`,className:"min-h-[200px] font-mono text-sm",value:t,onChange:a=>r(a.target.value)}),e.jsxs("div",{className:"rounded-md border p-3 bg-muted/30 space-y-2",children:[e.jsxs("p",{className:"text-xs font-medium flex items-center gap-1",children:[e.jsx(de,{className:"h-3 w-3"})," Variáveis disponÃveis"]}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:Q.map(a=>e.jsx("button",{type:"button",onClick:()=>r(t+`{{${a.key}}}`),className:"text-[10px] px-2 py-1 rounded-md bg-background border hover:bg-accent transition-colors",title:a.desc,children:`{{${a.key}}}`},a.key))})]}),w.length>0&&e.jsx("div",{className:"rounded-md border border-destructive/30 bg-destructive/5 p-2",children:e.jsxs("p",{className:"text-xs text-destructive font-medium",children:["â ï¸ Variáveis desconhecidas: ",w.map(a=>`{{${a}}}
13`).join(", ")]})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(q,{className:"flex items-center gap-1",children:[e.jsx(D,{className:"h-3.5 w-3.5"})," Preview"]}),e.jsxs(se,{value:u,onValueChange:g,children:[e.jsx(ae,{className:"w-[200px] h-8 text-xs",children:e.jsx(te,{placeholder:"Produto exemplo..."})}),e.jsx(re,{children:i==null?void 0:i.map(a=>{var P;return e.jsx(ie,{value:a.id,className:"text-xs",children:(P=a.name)==null?void 0:P.substring(0,40)},a.id)})})]})]}),e.jsx("div",{className:"rounded-md border bg-muted/20 p-4 min-h-[200px] whitespace-pre-wrap text-sm font-mono leading-relaxed",children:x}),e.jsxs(b,{variant:"outline",size:"sm",onClick:C,disabled:k||!o,className:"w-full",children:[k?e.jsx(A,{className:"h-3.5 w-3.5 mr-1.5 animate-spin"}):e.jsx(D,{className:"h-3.5 w-3.5 mr-1.5"}),"Validar no servidor"]})]})]}),e.jsxs(b,{onClick:m,disabled:!E||h,className:"w-full",children:[h&&e.jsx(A,{className:"h-4 w-4 mr-1.5 animate-spin"}),j]})]})}function Le(){const{profile:d}=H(),t=d==null?void 0:d.organization_id,p=U(),{toast:r}=K(),[m,h]=n.useState(!1),[j,c]=n.useState(""),[u,g]=n.useState(""),[l,v]=n.useState(!1),[k,S]=n.useState(""),[y,i]=n.useState(""),[o,N]=n.useState(""),{data:x}=R({queryKey:["templates",t],enabled:!!t,queryFn:async()=>{const{data:s}=await f.from("message_templates").select("*").eq("organization_id",t).order("created_at",{ascending:!1});return s??[]}}),w=T({mutationFn:async()=>{const{error:s}=await f.from("message_templates").insert({organization_id:t,name:j,body_template:u});if(s)throw s},onSuccess:()=>{p.invalidateQueries({queryKey:["templates"]}),h(!1),c(""),g(""),r({title:"Template criado"})}}),C=T({mutationFn:async()=>{const{error:s}=await f.from("message_templates").update({name:y,body_template:o}).eq("id",k);if(s)throw s},onSuccess:()=>{p.invalidateQueries({queryKey:["templates"]}),v(!1),r({title:"Template atualizado"})}}),E=T({mutationFn:async({id:s,active:_})=>{const{error:L}=await f.from("message_templates").update({active:_}).eq("id",s);if(L)throw L},onSuccess:()=>p.invalidateQueries({queryKey:["templates"]})}),a=T({mutationFn:async s=>{const{error:_}=await f.from("message_templates").delete().eq("id",s);if(_)throw _},onSuccess:()=>{p.invalidateQueries({queryKey:["templates"]}),r({title:"Template removido"})}}),P=s=>{S(s.id),i(s.name),N(s.body_template),v(!0)};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold tracking-tight",children:"Templates"}),e.jsx("p",{className:"text-muted-foreground",children:"Modelos de mensagem para WhatsApp com preview ao vivo"})]}),e.jsxs(V,{open:m,onOpenChange:h,children:[e.jsx(ee,{asChild:!0,children:e.jsxs(b,{children:[e.jsx(ce,{className:"h-4 w-4 mr-1"})," Novo Template"]})}),e.jsxs(z,{className:"sm:max-w-4xl",children:[e.jsx(F,{children:e.jsx(I,{children:"Novo Template"})}),e.jsx(O,{name:j,body:u,onNameChange:c,onBodyChange:g,onSave:()=>w.mutate(),saving:w.isPending,saveLabel:"Criar Template",orgId:t})]})]})]}),e.jsx(V,{open:l,onOpenChange:v,children:e.jsxs(z,{className:"sm:max-w-4xl",children:[e.jsx(F,{children:e.jsx(I,{children:"Editar Template"})}),e.jsx(O,{name:y,body:o,onNameChange:i,onBodyChange:N,onSave:()=>C.mutate(),saving:C.isPending,saveLabel:"Salvar Alterações",orgId:t})]})}),x!=null&&x.length?e.jsx("div",{className:"grid gap-4 sm:grid-cols-2",children:x.map(s=>e.jsxs(B,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(X,{className:"text-sm",children:s.name}),e.jsx(ne,{variant:s.active?"default":"secondary",children:s.active?"Ativo":"Inativo"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(b,{variant:"ghost",size:"icon",className:"h-8 w-8",onClick:()=>P(s),children:e.jsx(oe,{className:"h-4 w-4"})}),e.jsx(Z,{checked:s.active,onCheckedChange:_=>E.mutate({id:s.id,active:_})}),e.jsx(b,{variant:"ghost",size:"icon",className:"h-8 w-8",onClick:()=>a.mutate(s.id),children:e.jsx(le,{className:"h-4 w-4 text-destructive"})})]})]}),e.jsx(M,{children:e.jsx("pre",{className:"text-xs bg-muted p-3 rounded-md whitespace-pre-wrap font-mono",children:s.body_template})})]},s.id))}):e.jsx(B,{children:e.jsxs(M,{className:"p-8 text-center text-muted-foreground",children:[e.jsx(W,{className:"h-8 w-8 mx-auto mb-2 opacity-50"}),"Nenhum template criado."]})})]})}export{Le 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.