import{FEEDBACK_API_URL as l}from"../../constants/common.js";import{mapObject as f}from"../../../shared/jsUtils.js";import{getClientIp as u}from"../utils/get-client-ip.js";function h(e){if(!e)return e;const a=["userAgent","firstName","lastName","auth_time","platform","id","email","ipAddress"];return f(e,(n,t)=>a.includes(t)?n:"*****")}function b({component:e,path:a,location:n,lang:t,score:r,max:s,reasons:o,comment:i,metadata:m,email:c}){return{feedbackComponent:e.toUpperCase(),path:a,location:n,lang:t,score:r,maxScore:s,reasons:o,comment:i,email:c,metadata:h(m)}}async function g(e,a){return(await fetch(l,{method:"POST",body:JSON.stringify(b(e)),headers:a})).json()}async function w(e){const a=await e.req.json(),n=e.req.header("user-agent"),t=u(e.req.raw),r=e.req.header("Sec-Ch-Ua-Platform"),s={...a.metadata,userAgent:n,ipAddress:t,platform:r?r.replace(/"/g,""):"unknown"},o=[];(!a.path||a.path==="")&&o.push("`path` is required");const i=["sentiment","rating","comment","problem","mood","scale"];if(i.includes(a.component)||o.push(`\`component\` field should be one of ${i.join(", ")}.`),o.length)return e.json({errors:o},400);const{claims:m}=e.get("auth"),c={"Content-Type":"application/json"},p=m?.email||a?.email||s?.email;try{const d=await g({...a,email:p,metadata:{email:p,...s}},c);return e.json({message:"Thanks for your feedback",...d},200,{})}catch(d){return e.json({errors:["Failed to send feedback",d.message]},500)}}export{w as feedbackHandler,h as normalizeFeedbackMetadata};
