2026-08-18 16:19:11 +03:30
import { _ as f , G as i , bF as P , I as O } from "./index-24cdd011.js" ; import { e as L } from "./convert-2ad76038.js" ; function B ( e ){ return { id : e . id , sender_type : e . sender_type , content : e . content || "" , message_type : e . message_type || "text" , file_url : e . file_url , file_name : e . file_name }} function $ ( e ){ return e . message_type === "image" ? "تصویر" : e . message_type === "audio" ? "پیام صوتی" : e . message_type === "file" ? e . file_name || "فایل" : ( e . content || "" ). trim (). slice ( 0 , 200 )} function j ( e ){ return e != null && e . id ? { id : e . id , sender_type : e . sender_type , content : e . content || "" , message_type : e . message_type || "text" , file_url : e . file_url , file_name : e . file_name } : null } function T ( e ){ return { id : e . id , conversation_id : e . conversation_id , sender_type : e . sender_type , sender_id : e . sender_id , content : e . content || "" , message_type : e . message_type || "text" , file_url : e . file_url , file_name : e . file_name , reply_to_id : e . reply_to_id , reply_to : j ( e . reply_to ), read_at : e . read_at , created_at : e . created_at }} function z ( e ){ return e === "user" ? "کاربر" : "پشتیبانی" } function G ( e ){ return e . message_type === "image" ? "تصویر" : e . message_type === "audio" ? "پیام صوتی" : e . message_type === "file" ? e . file_name || "فایل" : ( e . content || "" ). trim () || "پیام" } function K ( e , t ){ if ( e . reply_to ||! e . reply_to_id ) return e ; const r = t . find ( n => n . id === e . reply_to_id ); return r ? {... e , reply_to : { id : r . id , sender_type : r . sender_type , content : r . content , message_type : r . message_type , file_url : r . file_url , file_name : r . file_name }} : e } const v = `
2026-08-18 16:19:11 +03:30
` ). order ( "last_message_at" ,{ ascending :! 1 , nullsFirst :! 1 }). order ( "created_at" ,{ ascending :! 1 }); if ( t ) throw t ; return e || []} async function W ( e ){ const { data : t , error : r } = await i . from ( "support_messages" ). select ( v ). eq ( "conversation_id" , e ). order ( "created_at" ,{ ascending :! 0 }); if ( r ) throw r ; return ( t || []). map ( T )} async function X ( e ){ const t = new Date (). toISOString (), r = e . content . trim (), n = $ ({ content : r , message_type : "text" }),{ data : a , error : u } = await i . from ( "support_conversations" ). select ( "unread_by_user" ). eq ( "id" , e . conversationId ). single (); if ( u ) throw u ; const { data : o , error : s } = await i . from ( "support_messages" ). insert ([{ conversation_id : e . conversationId , sender_type : "admin" , sender_id : e . adminId , content : r , message_type : "text" , reply_to_id : e . replyToId || null }]). select ( v ). single (); if ( s ) throw s ; const { error : _ } = await i . from ( "support_conversations" ). update ({ last_message_at : t , last_message_preview : n , unread_by_user : (( a == null ? void 0 : a . unread_by_user ) ?? 0 ) + 1 , unread_by_admin : 0 , updated_at : t }). eq ( "id" , e . conversationId ); if ( _ ) throw _ ; return T ( o )} async function Y ( e ){ const t = new Date (). toISOString (); await i . from ( "support_messages" ). update ({ read_at : t }). eq ( "conversation_id" , e ). eq ( "sender_type" , "user" ). is ( "read_at" , null ), await i . from ( "support_conversations" ). update ({ unread_by_admin : 0 , updated_at : t }). eq ( "id" , e )} function Z ( e ){ var r ; if ( e . guest_id ) return (( r = e . guest_name ) == null ? void 0 : r . trim ()) || "مهمان" ; const t = e . users ; return t && ( t . name || [ t . first_name , t . last_name ]. filter ( Boolean ). join ( " " ) || t . phone ) || "کاربر" } function m ( e ){ return Array . isArray ( e ) ? e . map ( t => typeof t == "string" ? { url : t , name : null } : t && typeof t == "object" && "url" in t ? { url : String ( t . url || "" ), name : t . name || null } : null ). filter ( t => !! ( t != null && t . url )) : []} function M ( e ){ const t = Array . isArray ( e == null ? void 0 : e . messages ) ? e . messages . map ( r =>({... r , attachments : m ( r == null ? void 0 : r . attachments )})). sort (( r , n )=> new Date ( r . created_at ). getTime () - new Date ( n . created_at ). getTime ()) : void 0 ; return {... e , subject : ( e == null ? void 0 : e . subject ) || "other" , admin_queue : ( e == null ? void 0 : e . admin_queue ) || "new" , attachments : m ( e == null ? void 0 : e . attachments ), admin_attachments : m ( e == null ? void 0 : e . admin_attachments ), messages : t }} function ee ( e , t ){ return async r =>{ var k , q , w ; const { TableEngineRequest : n } = await f (()=> import ( "./buildSupabaseQuery-856791d4.js" ),[ "assets/buildSupabaseQuery-856791d4.js" , "assets/index-24cdd011.js" , "assets/index-a54d32f7.css" , "assets/supabaseTableQuery-8ab5665d.js" ]), a = [... r . filters || []], u = a . filter ( c =>( c == null ? void 0 : c . field ) && c . field !== "action" && c . field !== "row" ). map ( c => c . field === "ticket_no" ? {... c , field : "id" , hidden :! 1 } : {... c , hidden :! 1 }); u . push ({ field : "admin_queue" , op : "eq" , value : e , hidden :! 1 }), t !== "all" && u . push ({ field : "subject" , op : "eq" , value : t , hidden :! 1 }); const o = String ( r . search || "" ). trim (), s = {... r , filters : u , search : o , select : [ "*" , "users(id,name,first_name,last_name,phone)" ]}; if (((( k = s . sort ) == null ? void 0 : k . key ) === "ticket_no" || (( q = s . sort ) == null ? void 0 : q . key ) === "row_index" ) && ( s . sort = {... s . sort , key : "id" }), typeof s . order == "string" && ( s . order = s . order . replace ( /ticket_no/g , "id" ). replace ( /row_index/g , "id" )), ! s . sort &&! s . order && ( s . order = "-updated_at" ), /^\d+$/ . test ( o )){ const { supabase : c } = await f (()=> import ( "./index-24cdd011.js" ). then ( p => p . bS ),[ "assets/index-24cdd011.js" , "assets/index-a54d32f7.css" ]),{ applySupabaseFilter : E , toPostgrestIlikeTerm : I } = await f (()=> import ( "./supabaseTableQuery-8ab5665d.js" ),[]); let d = c . from ( "support_tickets" ). select ( "*,users(id,name,first_name,last_name,phone)" ,{ count : "exact" }); const y = I ( o ); d = d . or ( `id.eq. ${ Number ( o ) } ,problem.ilike. ${ y } ,account_name.ilike. ${ y } ,account_phone.ilike. ${ y } ` ), u . forEach ( p =>{ d = E ( d , p )}); const g = s . page || 1 , l = s . limit || 20 , h = ( g - 1 ) * l , S = (( w = s . sort ) == null ? void 0 : w . key ) || "updated_at" , D = S === "ticket_no" || S === "row_index" ? "id" : S , A = s . sort ? s . sort . order === "asc" :! 1 ; d = d . range ( h , h + l - 1 ). order ( D ,{ ascending : A }); const { data : R , error : b , count : x } = await d ; return b ? ( console . error ( "Supabase support tickets search error:" , b ),{ query : {... r , filters : a , search : o , total : 0 , page : g , limit : l }, rows : []}) : { query : {... r , filters : a , search : o , limit : l , page : g , total : x || 0 }, rows : ( R || []). map (( p , F )=>({... p , index : h + F }))}} const _ = await n ( s , "support_tickets" ); return {... _ , quer
`).eq("id",e).single();if(r)throw r;return M(t)}async function ne(e){var o,s;const t=P()||localStorage.getItem("tamin_token")||localStorage.getItem("token")||"";if(!t)throw new Error("توکن احراز هویت یافت نشد");const r=new FormData;r.append("file",e);const n=await fetch(` $ { O } / upload / machartaa - img `,{method:"POST",headers:{Authorization:` Bearer $ { t } `},body:r});if(!n.ok)throw new Error("خطا در آپلود تصویر");const a=await n.json(),u=a.full_src||a.url||a.src||a.path||((o=a.data)==null?void 0:o.full_src)||((s=a.data)==null?void 0:s.url);if(!u)throw new Error("مسیر فایل از سرور دریافت نشد");return L(u)}async function ae(e){const t=e.content.trim();if(!t&&!(e.attachments||[]).length)throw new Error("متن پاسخ یا تصویر الزامی است");const r=new Date().toISOString(),n=e.attachments||[],{data:a,error:u}=await i.from("support_ticket_messages").insert([{ticket_id:e.ticketId,sender_type:"admin",sender_id:e.adminId||null,content:t||" ",attachments:n}]).select("*").single();if(u)throw u;const{error:o}=await i.from("support_tickets").update({status:"answered",admin_queue:"answered",admin_note:t||null,admin_attachments:n,updated_at:r}).eq("id",e.ticketId);if(o)throw o;return{...a,attachments:m(a.attachments)}}async function se(e){const t=new Date().toISOString(),{data:r,error:n}=await i.from("support_tickets").update({admin_queue:"spam",updated_at:t}).eq("id",e).select();if(n)throw n;return r}async function ie(e){const t=new Date().toISOString(),r=e.status==="closed"?"closed":e.status==="answered"||e.status==="reviewed"?"answered":"new",{data:n,error:a}=await i.from("support_tickets").update({admin_queue:r,updated_at:t}).eq("id",e.id).select();if(a)throw a;return n}export{z as a,G as b,J as c,W as d,T as e,N as f,Z as g,U as h,H as i,ee as j,te as k,re as l,Y as m,ne as n,se as o,ie as p,ae as q,K as r,X as s,B as t,Q as u};