2026-08-15 13:12:23 +03:30
import { _ as f , G as o , bC as F , I as O } from "./index-885a0812.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 v ( 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 ( s => s . 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 E = `
2026-08-15 13:12:23 +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 o . from ( "support_messages" ). select ( E ). eq ( "conversation_id" , e ). order ( "created_at" ,{ ascending :! 0 }); if ( r ) throw r ; return ( t || []). map ( v )} async function X ( e ){ const t = new Date (). toISOString (), r = e . content . trim (), s = $ ({ content : r , message_type : "text" }),{ data : a , error : u } = await o . from ( "support_conversations" ). select ( "unread_by_user" ). eq ( "id" , e . conversationId ). single (); if ( u ) throw u ; const { data : i , error : n } = await o . 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 ( E ). single (); if ( n ) throw n ; const { error : _ } = await o . from ( "support_conversations" ). update ({ last_message_at : t , last_message_preview : s , 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 v ( i )} async function Y ( e ){ const t = new Date (). toISOString (); await o . from ( "support_messages" ). update ({ read_at : t }). eq ( "conversation_id" , e ). eq ( "sender_type" , "user" ). is ( "read_at" , null ), await o . 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 , s )=> new Date ( r . created_at ). getTime () - new Date ( s . 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 S , q , b ; const { TableEngineRequest : s } = await f (()=> import ( "./buildSupabaseQuery-bf7d4f96.js" ),[ "assets/buildSupabaseQuery-bf7d4f96.js" , "assets/index-885a0812.js" , "assets/index-d4e075d8.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 i = String ( r . search || "" ). trim (), n = {... r , filters : u , search : i , select : [ "*" , "users(id,name,first_name,last_name,phone)" ]}; if (((( S = n . sort ) == null ? void 0 : S . key ) === "ticket_no" || (( q = n . sort ) == null ? void 0 : q . key ) === "row_index" ) && ( n . sort = {... n . sort , key : "id" }), typeof n . order == "string" && ( n . order = n . order . replace ( /ticket_no/g , "id" ). replace ( /row_index/g , "id" )), ! n . sort &&! n . order && ( n . order = "-updated_at" ), /^\d+$/ . test ( i )){ const { supabase : c } = await f (()=> import ( "./index-885a0812.js" ). then ( l => l . bS ),[ "assets/index-885a0812.js" , "assets/index-d4e075d8.css" ]),{ applySupabaseFilter : T , 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 ( i ); d = d . or ( `id.eq. ${ Number ( i ) } ,problem.ilike. ${ y } ,account_name.ilike. ${ y } ,account_phone.ilike. ${ y } ` ), u . forEach ( l =>{ d = T ( d , l )}); const g = n . page || 1 , p = n . limit || 20 , h = ( g - 1 ) * p , k = (( b = n . sort ) == null ? void 0 : b . key ) || "updated_at" , A = k === "ticket_no" || k === "row_index" ? "id" : k , D = n . sort ? n . sort . order === "asc" :! 1 ; d = d . range ( h , h + p - 1 ). order ( A ,{ ascending : D }); const { data : R , error : w , count : x } = await d ; return w ? ( console . error ( "Supabase support tickets search error:" , w ),{ query : {... r , filters : a , search : i , total : 0 , page : g , limit : p }, rows : []}) : { query : {... r , filters : a , search : i , limit : p , page : g , total : x || 0 }, rows : ( R || []). map (( l , P )=>({... l , index : h + P }))}} const _ = await s ( n , "support_tickets" ); return {... _ , quer
`).eq("id",e).single();if(r)throw r;return M(t)}async function ne(e){var i,n;const t=F()||localStorage.getItem("tamin_token")||localStorage.getItem("token")||"";if(!t)throw new Error("توکن احراز هویت یافت نشد");const r=new FormData;r.append("file",e);const s=await fetch(` $ { O } / upload / machartaa - img `,{method:"POST",headers:{Authorization:` Bearer $ { t } `},body:r});if(!s.ok)throw new Error("خطا در آپلود تصویر");const a=await s.json(),u=a.full_src||a.url||a.src||a.path||((i=a.data)==null?void 0:i.full_src)||((n=a.data)==null?void 0:n.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(),s=e.attachments||[],{data:a,error:u}=await o.from("support_ticket_messages").insert([{ticket_id:e.ticketId,sender_type:"admin",sender_id:e.adminId||null,content:t||" ",attachments:s}]).select("*").single();if(u)throw u;const{error:i}=await o.from("support_tickets").update({status:"answered",admin_queue:"answered",admin_note:t||null,admin_attachments:s,updated_at:r}).eq("id",e.ticketId);if(i)throw i;return{...a,attachments:m(a.attachments)}}export{z as a,G as b,J as c,W as d,v 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,ae as o,K as r,X as s,B as t,Q as u};