import{o as e}from"./chunk-mlkTr37E.js";import{t}from"./query-string-RcCSSKcJ.js";import{b as n}from"./event-B-4OIAbe.js";import{x as r}from"./cache-BCH2kRNz.js";import{n as i}from"./joi-YsA4RdjR.js";import{r as a,t as o}from"./init-vTxIv4PY.js";import{r as s}from"./get-ztzP-Ywo.js";import{r as c}from"./seller-CQq_p16h.js";var l=i.object({toUserId:i.number().integer().required(),toShopId:i.number().integer().required()}),u=e(r()),d={userId:0,ctoken:``,authorization:``,token:``},f=({userId:e,ctoken:n,body:r,timestamp:i,authorization:a})=>{let o=`7.3.4`,s={_uid:`0-${e}`,_v:o},c={fromID:e,plat:o,ver:`v1.2`,reqID:r.request_id,toID:r.to_id,timestamp:i,type:r.type,strinput:r.type===`text`?r.content.text:``},l=t.stringify({key:`396c15ad-6d3e-4018-98be-cef58cb45cd0`,plat:c.plat,ver:c.ver,fromID:c.fromID,reqID:c.reqID,toID:c.toID,timestamp:c.timestamp,type:c.type,strinput:c.strinput},{sort:!1,encode:!1});return{query:{...s,_api_source:`sc`,csrf_token:n},headers:{"x-v":1,"x-s":u.default.SHA256(l).toString(),ts:i,Authorization:`Bearer ${a}`}}},p=async()=>{if(!d.authorization){let e=await c();d.authorization=e.token,d.token=e.p_token,d.userId=(await s()).id,d.ctoken=(await a()).find(e=>e.name===`CTOKEN`).value}return d},m=`8.8.9`,h=`sc`,g=`VN`,_=`WEBCHAT_AUTH_ERROR`,v=(e,t,n)=>{if(!(e&&t&&n))throw _},y=(e,t,n)=>({_uid:`1-${e}`,_v:m,csrf_token:t,SPC_CDS_CHAT:n,"x-shop-region":g,_api_source:h}),b=e=>({authorization:`Bearer ${e}`,"content-type":`text/plain;charset=UTF-8`,"x-shop-region":g}),x=(e,t,r)=>{let i=y(e,t,n());return{headers:b(r),params:i}},S=async(e,t,r,i=`text`,a={},s=``)=>{if(await l.validateAsync({toUserId:e,toShopId:t}),i===`text`&&!r)throw Error(`Text is required`);if(i===`sticker`&&!a.stickerId)throw Error(`StickerId is required`);if(i===`image`&&!a.imageId)throw Error(`ImageId is required`);if(i===`product`&&!a.productId)throw Error(`ProductId is required`);if(i===`voucher`&&!a.voucherId)throw Error(`VoucherId is required`);let c={to_id:e,shop_id:t,type:i,content:{text:r,uid:n()},request_id:n(),chat_send_option:{force_send_cancel_order_warning:!1,comply_cancel_order_warning:!1},entry_point:`direct_chat_entry_point`,choice_info:{real_shop_id:null},biz_id:0,source:`minichat`};i===`sticker`?(c.content.sticker_id=a.stickerId,c.content.sticker_package_id=a.stickerPackage,delete c.content.text):i===`image`?(c.content.image_id=a.imageId,c.content.url=`https://down-sg.img.susercontent.com/${a.imageId}`,delete c.content.text):i===`product`?(c.content.product_id=a.productId,c.content.shop_id=a.shopId,c.content.product_name=a.productName,delete c.content.text):i===`voucher`&&(c.content.voucher_id=a.voucherId,c.content.voucher_code=a.voucherCode,c.content.source=`webchat`,delete c.content.text),s&&(c.conversation_id=s);let u=Date.now(),{userId:d,ctoken:m,authorization:h}=await p();v(d,m,h);let{query:g,headers:_}=f({userId:d,ctoken:m,body:c,timestamp:u,authorization:h}),y={headers:_,params:g};return await o.post(`/webchat/api/v1.2/mini/messages`,c,y)},C=async e=>{let t=new FormData;t.append(`file`,e);let{userId:n,ctoken:r,authorization:i}=await p();v(n,r,i);let a=x(n,r,i);return(await o.post(`/webchat/api/coreapi/v1.2/images`,t,{...a,headers:{...a.headers,"Content-Type":`multipart/form-data`}})).url_hash},w=async(e,t,n,r=2)=>{let i=`/webchat/api/v1.2/mini/conversations/${e}/status`,a={shop_id:t,status:n,biz_id:r},{userId:s,ctoken:c,authorization:l}=await p();v(s,c,l);let u=x(s,c,l);return await o.put(i,a,u)},T=async(e,t)=>w(e,t,`activated`),E=async(e,t)=>w(e,t,`closed`),D=async(e,t,n=0,r=!0,i=`seller_contact_buyer`)=>{let a={user_id:Number(e),shop_id:Number(t),biz_id:Number(n),on_message_received:r,entry_point:i},{userId:s,ctoken:c,authorization:l}=await p();v(s,c,l);let u=x(s,c,l);return await o.post(`/webchat/api/v1.2/mini/conversation/redirection`,a,u)};export{C as a,E as i,S as n,T as r,D as t};