vendor: 5,719 bytes, line 1
1import{t as wa,w as fr,r as a,v as yr,x as St,y as us,z as at,B as Ca,I as Ta,u as Re,j as e,C as Pa,A as jr,g as Lt,e as $t,f as Ut,h as Bt,m as Ea}from"./i18next-CqvhuwpF.js";import{b as ke,e as $e,h as ms,c as ze,d as qe,P as Aa,U as Da,C as Fa,g as Yt,u as Nr,z as Ia,m as be,n as fe,o as ye,p as je,q as J,r as ct,f as ks,w as Vt,H as Oa,I as Ra,J as Ma,A as La}from"./AppLayout-BC65n6PF.js";import{a as We,J as vr,C as Ue,e as me,A as he,B as P,f as $a,t as Wt,u as Ss,d as ws,P as js,K as Ua,y as Ns,M as _r,c as Ye,S as Ba,X as kr,g as Ha,N as dt,T as ut,w as mt,x as ht,r as Va,v as za,O as tr,b as qa,i as Ya}from"./main-BOZtKaat.js";import{I as re}from"./input-AoMgvwfd.js";import{T as Cs,a as Ts,b as He,c as Ve}from"./tabs-D57N7-Ac.js";import{b as xe,e as W,T as xt,a as pt,c as Q,d as gt}from"./table-CioQXBhx.js";import{P as Se}from"./phoneNumbers.service-BinwXsdw.js";import{A as Ct}from"./agents.service-tVMofRVn.js";import{A as kt}from"./account_balance.service-CQx_Ampj.js";import{f as Ze,n as vs}from"./phone-CAUIplRW.js";import{T as sr,a as rr,b as De,c as Fe,d as jt,e as rt,f as Ie,g as Wa,P as Ga}from"./storageKeys-BCkiLQ8C.js";import{A as Gt,a as Kt,b as Zt,c as Jt,d as Qt,e as Xt,f as es,g as ts}from"./alert-dialog-UA2nFcsp.js";import{c as Ps,L as Be,B as Ne,d as Ka,P as Za}from"./label-8iG0W7Uo.js";import{T as zt}from"./textarea-BdiuRWmI.js";import{a as we,f as Sr,b as Ce,c as Te,d as Pe,e as Oe,D as Ee}from"./dialog-7HwtdUOo.js";import{S as Ja}from"./scheduler.service-DdvYIBvV.js";import{M as Qa}from"./message-square-BiUjPdin.js";import{T as ss}from"./trash-2-ByqD5vp6.js";import{b as Es,c as Xa,d as qt,g as vt,V as _s,a as wr,I as Me,i as ar}from"./VerifiedCallerIdBadges-CieCzbX2.js";import{i as Cr,a as en,t as tn,g as nr,b as ir,I as sn}from"./verifiedCallerId-DqMU680s.js";import{a as rn}from"./addMonths-CkLX-sh9.js";import{U as an,R as Tr,a as As,A as nn,b as ln,c as Pr,L as Er}from"./unlink-CzBAYtfN.js";import{P as Ar,a as Dr,b as Fr}from"./popover-B2gf5OT9.js";import{L as ae}from"./loader-circle-BtBAOIIe.js";import{C as wt}from"./circle-x-DBZWKv6F.js";import{C as Ds}from"./circle-check-BeENQkwg.js";import{C as Ir}from"./circle-alert-DLtgPsY5.js";import{D as on}from"./download-B83gKAa5.js";import{C as cn}from"./chevron-down-CO4tUd1h.js";import{I as Or}from"./info-B8elj0A9.js";import{C as dn}from"./index-DL9Qt027.js";import{C as un}from"./copy-CnYUiJI_.js";import{P as Rr}from"./pencil-BL6wqd6o.js";import{O as Le}from"./outboundSipTrunk.service-CsXwgLE-.js";import{P as mn}from"./phone-forwarded-BjSuKheJ.js";import{f as hn}from"./index-C3iDPX0J.js";import{S as lr}from"./search-Dc_9KVnO.js";import"./BrandingLogo-D4ghZRC3.js";import"./circle-check-big-BjEEymhV.js";import"./userInvitation.service-CpLpy6E7.js";import"./accountInvitationStorage-BDhY2gyG.js";var xn={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,link:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0};const pn=wa(xn);var gn=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function or(s){var t={type:"tag",name:"",voidElement:!1,attrs:{},children:[]},i=s.match(/<\/?([^\s]+?)[/\s>]/);if(i&&(t.name=i[1],(pn[i[1]]||s.charAt(s.length-2)==="/")&&(t.voidElement=!0),t.name.startsWith("!--"))){var l=s.indexOf("-->");return{type:"comment",comment:l!==-1?s.slice(4,l):""}}for(var d=new RegExp(gn),r=null;(r=d.exec(s))!==null;)if(r[0].trim())if(r[1]){var _=r[1].trim(),f=[_,""];_.indexOf("=")>-1&&(f=_.split("=")),t.attrs[f[0]]=f[1],d.lastIndex--}else r[2]&&(t.attrs[r[2]]=r[3].trim().substring(1,r[3].length-1));return t}var bn=/<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,fn=/^\s*$/,yn=Object.create(null);function Mr(s,t){switch(t.type){case"text":return s+t.content;case"tag":return s+="<"+t.name+(t.attrs?function(i){var l=[];for(var d in i)l.push(d+'="'+i[d]+'"');return l.length?" "+l.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?s:s+t.children.reduce(Mr,"")+"</"+t.name+">";case"comment":return s+"<!--"+t.comment+"-->"}}var jn={parse:function(s,t){t||(t={}),t.components||(t.components=yn);var i,l=[],d=[],r=-1,_=!1;if(s.indexOf("<")!==0){var f=s.indexOf("<");l.push({type:"text",content:f===-1?s:s.substring(0,f)})}return s.replace(bn,function(C,y){if(_){if(C!=="</"+i.name+">")return;_=!1}var u,h=C.charAt(1)!=="/",v=C.startsWith("<!--"),I=y+C.length,x=s.charAt(I);if(v){var D=or(C);return r<0?(l.push(D),l):((u=d[r]).children.push(D),l)}if(h&&(r++,(i=or(C)).type==="tag"&&t.components[i.name]&&(i.type="component",_=!0),i.voidElement||_||!x||x==="<"||i.children.push({type:"text",content:s.slice(I,s.indexOf("<",I))}),r===0&&l.push(i),(u=d[r-1])&&u.children.push(i),d[r]=i),(!h||i.voidElement)&&(r>-1&&(i.voidElement||i.name===C.slice(2,-1))&&(r--,i=r===-1?l:d[r]),!_&&x!=="<"&&x)){u=r===-1?l:d[r].children;var E=s.indexOf("<",I),w=s.slice(I,E===-1?void 0:E);fn.test(w)&&(w=" "),(E>-1&&r+u.length>=0||w!==" ")&&u.push({type:"text",content:w})}}),l},stringify:function(s){return s.reduce(function(t,i){return t+Mr("",i)},"")}};const hs=(s,t)=>{if(!s)return!1;const i=s.props?.children??s.children;return t?i.length>0:!!i},xs=s=>{if(!s)return[];const t=s.props?.children??s.children;return s.props?.i18nIsDynamicList?ot(t):t},Nn=s=>Array.isArray(s)&&s.every(a.isValidElement),ot=s=>Array.isArray(s)?s:[s],vn=(s,t)=>{const i={...t};return i.props=Object.assign(s.props,t.props),i},Lr=(s,t,i,l)=>{if(!s)return"";let d="";const r=ot(s),_=t?.transSupportBasicHtmlNodes?t.transKeepBasicHtmlNodesFor??[]:[];return r.forEach((f,C)=>{if(St(f)){d+=`${f}`;return}if(a.isValidElement(f)){const{props:y,type:u}=f,h=Object.keys(y).length,v=_.indexOf(u)>-1,I=y.children;if(!I&&v&&!h){d+=`<${u}/>`;return}if(!I&&(!v||h)||y.i18nIsDynamicList){d+=`<${C}></${C}>`;return}
1if(v&&h===1&&St(I)){d+=`<${u}>${I}</${u}>`;return}const x=Lr(I,t,i,l);d+=`<${C}>${x}</${C}>`;return}if(f===null){us(i,"TRANS_NULL_VALUE","Passed in a null value as child",{i18nKey:l});return}if(at(f)){const{format:y,...u}=f,h=Object.keys(u);if(h.length===1){const v=y?`${h[0]}, ${y}`:h[0];d+=`{{${v}}}`;return}us(i,"TRANS_INVALID_OBJ","Invalid child - Object should only have keys {{ value, format }} (format is optional).",{i18nKey:l,child:f});return}us(i,"TRANS_INVALID_VAR","Passed in a variable like {number} - pass variables for interpolation as full objects like {{number}}.",{i18nKey:l,child:f})}),d},_n=(s,t,i,l,d,r,_)=>{if(i==="")return[];const f=d.transKeepBasicHtmlNodesFor||[],C=i&&new RegExp(f.map(w=>`<${w}`).join("|")).test(i);if(!s&&!t&&!C&&!_)return[i];const y=t??{},u=w=>{ot(w).forEach(F=>{St(F)||(hs(F)?u(xs(F)):at(F)&&!a.isValidElement(F)&&Object.assign(y,F))})};u(s);const h=jn.parse(`<0>${i}</0>`),v={...y,...r},I=(w,q,F)=>{const V=xs(w),z=D(V,q.children,F);return Nn(V)&&z.length===0||w.props?.i18nIsDynamicList?V:z},x=(w,q,F,V,z)=>{w.dummy?(w.children=q,F.push(a.cloneElement(w,{key:V},z?void 0:q))):F.push(...a.Children.map([w],m=>{const j={...m.props};return delete j.i18nIsDynamicList,a.createElement(m.type,{...j,key:V,ref:m.props.ref??m.ref},z?null:q)}))},D=(w,q,F)=>{const V=ot(w);return ot(q).reduce((m,j,g)=>{const O=j.children?.[0]?.content&&l.services.interpolator.interpolate(j.children[0].content,v,l.language);if(j.type==="tag"){let B=V[parseInt(j.name,10)];!B&&t&&(B=t[j.name]),F.length===1&&!B&&(B=F[0][j.name]),B||(B={});const M=Object.keys(j.attrs).length!==0?vn({props:j.attrs},B):B,H=a.isValidElement(M),A=H&&hs(j,!0)&&!j.voidElement,S=C&&at(M)&&M.dummy&&!H,T=at(t)&&Object.hasOwnProperty.call(t,j.name);if(St(M)){const p=l.services.interpolator.interpolate(M,v,l.language);m.push(p)}else if(hs(M)||A){const p=I(M,j,F);x(M,p,m,g)}else if(S){const p=D(V,j.children,F);x(M,p,m,g)}else if(Number.isNaN(parseFloat(j.name)))if(T){const p=I(M,j,F);x(M,p,m,g,j.voidElement)}else if(d.transSupportBasicHtmlNodes&&f.indexOf(j.name)>-1)if(j.voidElement)m.push(a.createElement(j.name,{key:`${j.name}-${g}`}));else{const p=D(V,j.children,F);m.push(a.createElement(j.name,{key:`${j.name}-${g}`},p))}else if(j.voidElement)m.push(`<${j.name} />`);else{const p=D(V,j.children,F);m.push(`<${j.name}>${p}</${j.name}>`)}else if(at(M)&&!H){const p=j.children[0]?O:null;p&&m.push(p)}else x(M,O,m,g,j.children.length!==1||!O)}else if(j.type==="text"){const B=d.transWrapTextNodes,M=_?d.unescape(l.services.interpolator.interpolate(j.content,v,l.language)):l.services.interpolator.interpolate(j.content,v,l.language);B?m.push(a.createElement(B,{key:`${j.name}-${g}`},M)):m.push(M)}return m},[])},E=D([{dummy:!0,children:s||[]}],h,ot(s||[]));return xs(E[0])},$r=(s,t,i)=>{const l=s.key||t,d=a.cloneElement(s,{key:l});if(!d.props||!d.props.children||i.indexOf(`${t}/>`)<0&&i.indexOf(`${t} />`)<0)return d;function r(){return a.createElement(a.Fragment,null,d)}return a.createElement(r,{key:l})},kn=(s,t)=>s.map((i,l)=>$r(i,l,t)),Sn=(s,t)=>{const i={};return Object.keys(s).forEach(l=>{Object.assign(i,{[l]:$r(s[l],l,t)})}),i},wn=(s,t,i,l)=>s?Array.isArray(s)?kn(s,t):at(s)?Sn(s,t):(fr(i,"TRANS_INVALID_COMPONENTS",'<Trans /> "components" prop expects an object or array',{i18nKey:l}),null):null,Cn=s=>!at(s)||Array.isArray(s)?!1:Object.keys(s).reduce((t,i)=>t&&Number.isNaN(Number.parseFloat(i)),!0);function Tn({children:s,count:t,parent:i,i18nKey:l,context:d,tOptions:r={},values:_,defaults:f,components:C,ns:y,i18n:u,t:h,shouldUnescape:v,...I}){const x=u||yr();if(!x)return fr(x,"NO_I18NEXT_INSTANCE","Trans: You need to pass in an i18next instance using i18nextReactModule",{i18nKey:l}),s;const D=h||x.t.bind(x)||(S=>S),E={...Ca(),...x.options?.react};let w=y||D.ns||x.options?.defaultNS;w=St(w)?[w]:w||["translation"];const q=Lr(s,E,x,l),F=f||q||E.transEmptyNodeValue||l,{hashTransKey:V}=E,z=l||(V?V(q||F):q||F);x.options?.interpolation?.defaultVariables&&(_=_&&Object.keys(_).length>0?{..._,...x.options.interpolation.defaultVariables}:{...x.options.interpolation.defaultVariables});const m=_||t!==void 0&&!x.options?.interpolation?.alwaysFormat||!s?r.interpolation:{interpolation:{...r.interpolation,prefix:"#$?",suffix:"?$#"}},j={...r,context:d||r.context,count:t,..._,...m,defaultValue:F,ns:w},g=z?D(z,j):F,O=wn(C,g,x,l);let B=O||s,M=null;Cn(O)&&(M=O,B=s);const H=_n(B,M,g,x,E,j,v),A=i??E.defaultTransParent;return A?a.createElement(A,I,H):H}function _t({children:s,count:t,parent:i,i18nKey:l,context:d,tOptions:r={}
1,values:_,defaults:f,components:C,ns:y,i18n:u,t:h,shouldUnescape:v,...I}){const{i18n:x,defaultNS:D}=a.useContext(Ta)||{},E=u||x||yr(),w=h||E?.t.bind(E);return Tn({children:s,count:t,parent:i,i18nKey:l,context:d,tOptions:r,values:_,defaults:f,components:C,ns:y||w?.ns||D||E?.options?.defaultNS,i18n:E,t:h,shouldUnescape:v,...I})}function Pn({number:s,phoneNumberLimit:t,phoneNumberCounts:i,isEnterprisePlan:l,isFreemiumPlan:d,hasPaymentMethod:r,allowCreditsForPhoneNumbers:_}){const[f,C]=a.useState(!1),[y,u]=a.useState(!1),{t:h}=Re(),{toast:v}=We(),I=H=>{localStorage.setItem(rr,JSON.stringify({number:H,created_at:Date.now()}))},D=d&&r===!1||_,E=async H=>{if(D||r)return!0;try{if((await kt.getPaymentMethodStatus("phone_numbers")).has_payment_method)return!0;I(H),v({title:h("telephony.paymentMethodRequiredTitle"),description:h("telephony.paymentMethodRequiredDescription")});const S=await kt.createPaymentMethodCheckoutSession(sr);return window.location.href=S.checkout_url,!1}catch{return localStorage.removeItem(rr),v({title:h("toast.error"),description:h("messages.paymentMethodStatusFailed"),variant:"destructive"}),!1}},w=i?.locked??0,q=i?.active??0,F=i?.total??0,V=typeof t=="number"&&t>=0,z=V&&i?F>=(t??0):!1,m=w>1?h("telephony.phoneNumberLimitLockedInfo_other",{count:w}):"",j=V&&i?h(l?"telephony.phoneNumberLimitExceededEnterprise":"telephony.phoneNumberLimitExceeded",{limit:t,active:q,lockedInfo:m}):"",g=async H=>{try{if(C(!0),z){v({title:h("telephony.phoneNumberLimitTitle"),description:j,variant:"destructive"});return}if(!await E(H))return;I(H),window.location.href=`/payment-method/success?type=${sr}`}catch{v({title:h("toast.error"),description:h("messages.failedToPurchasePhoneNumber"),variant:"destructive"})}finally{C(!1)}},O=Ze(s.phone_number)||s.phone_number,B=D?h("telephony.buyNumberFreemiumDescription",{price:s.price.toFixed(2)}):h("telephony.buyNumberConfirmDescription",{price:s.price.toFixed(2),total:(s.price*1.19).toFixed(2)}),M=h(D?"telephony.buyNumberFreemiumConfirm":"telephony.buyNumberConfirmConfirm");return e.jsxs(ke,{className:"border border-gray-200 dark:bg-gray-800",children:[e.jsx($e,{className:"p-4",children:e.jsxs("div",{className:"space-y-2 text-sm text-gray-600 dark:text-gray-400",children:[e.jsx("div",{className:"font-medium text-black dark:text-gray-300",children:O}),e.jsx("div",{children:s.friendly_name}),e.jsx("div",{children:h(vr(s.line_type))}),e.jsxs("div",{className:"text-sm font-medium text-black dark:text-gray-300",children:[Ps(s.price),"/",h("month")," ",!D&&e.jsx("span",{className:"text-xs text-gray-500 dark:text-gray-400",children:h("telephony.vatNotice")})]}),e.jsx(Ue,{I:he.CREATE,a:me.PHONE_NUMBER,children:e.jsx(P,{size:"sm",className:"w-full bg-black dark:bg-slate-950 text-white hover:bg-gray-800 dark:hover:bg-gray-900",onClick:()=>u(!0),disabled:f,children:h(f?"common.buying":"common.buyNumber")})})]})}),e.jsx(Ue,{I:he.CREATE,a:me.PHONE_NUMBER,children:e.jsx(Gt,{open:y,onOpenChange:u,children:e.jsxs(Kt,{children:[e.jsxs(Zt,{children:[e.jsx(Jt,{children:h("telephony.buyNumberConfirmTitle",{phone:O})}),e.jsx(Qt,{className:"space-y-2 text-gray-600 dark:text-gray-300",children:e.jsx("span",{children:B})})]}),e.jsxs(Xt,{children:[e.jsx(es,{onClick:()=>u(!1),children:h("telephony.buyNumberConfirmCancel")}),e.jsx(ts,{className:"bg-black hover:bg-gray-800 text-white",onClick:()=>{u(!1),g(s)},disabled:f,children:f?h("common.buying"):M})]})]})})})]})}const En=()=>{try{return Intl.DateTimeFormat().resolvedOptions().timeZone||"UTC"}catch{return"UTC"}},An=s=>{if(!s)return!1;try{return new Intl.DateTimeFormat("en-US",{timeZone:s}).format(new Date),!0}catch{return!1}},Dn=s=>{const t=s?.trim()??"";return An(t)?t:En()},Fs=(s,t)=>{const i=new Intl.DateTimeFormat("en-US",{timeZone:t,hour12:!1,year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"}).formatToParts(s),l=d=>Number(i.find(r=>r.type===d)?.value??"0");return{year:l("year"),month:l("month"),day:l("day"),hour:l("hour")%24,minute:l("minute"),second:l("second")}},cr=(s,t)=>{const i=Fs(s,t);return Date.UTC(i.year,i.month-1,i.day,i.hour,i.minute,i.second)-s.getTime()},Fn=(s,t)=>{const i=Fs(s,t),l=d=>String(d).padStart(2,"0");return`${i.year}-${l(i.month)}-${l(i.day)}T${l(i.hour)}:${l(i.minute)}`},dr=(s,t)=>{if(!s)return null;const i=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(s)?`${s}:00`:s,l=Date.parse(`${i}Z`);if(Number.isNaN(l))return null;const d=l-cr(new Date(l),t),r=l-cr(new Date(d),t);return new Date(r)},ur=(s,t)=>{const i=Fs(s,t),l=d=>String(d).padStart(2,"0");return`${l(i.day)}.${l(i.month)}.${i.year}, ${l(i.hour)}:${l(i.minute)}`};function In({agentId:s,phoneNumberId:t,phoneNumber:i,agentName:l,trigger:d}){const{t:r}=Re(),{toast:_}=We(),[f,C]=a.useState(!1),[y,u]=a.useState(!1),[h,v]=a.useState(""),{data:I}=$a({queryKey:["agent",s],queryFn:()=>Ct.getAgent(s),enabled:f&&!!s,staleTime:5*60*1e3}),x=Dn(I?.agent?.conversation_config?.agent?.prompt?.timezone),D=()=>({to_number:"",first_message:"",prompt:""}
1),[E,w]=a.useState([D()]),[q,F]=a.useState({open:!1,instant:!1,overrideTime:void 0}),V=()=>{w([...E,D()])},z=A=>{E.length>1&&w(E.filter((S,T)=>T!==A))},m=(A,S,T)=>{const p=[...E];p[A]={...p[A],[S]:T},w(p)},j=(A=!1)=>{if(!A&&!h)return _({title:r("toast.validationError"),description:r("toast.selectDateAndTimeForCalls"),variant:"destructive"}),!1;if(!A){const S=new Date,T=dr(h,x);if(!T||T.getTime()<=S.getTime())return _({title:r("toast.validationError"),description:r("toast.futureTimeRequired",{time:ur(S,x),timezone:x}),variant:"destructive"}),!1}for(let S=0;S<E.length;S++)if(!E[S].to_number.trim())return _({title:r("toast.validationError"),description:r("toast.enterPhoneNumberFor",{item:S+1}),variant:"destructive"}),!1;return!0},g=()=>{const A=new Map;return E.map((S,T)=>{const p=vs(S.to_number);if(!p)throw new Error(r("toast.enterPhoneNumberFor",{item:T+1}));const X=A.get(p);if(X!==void 0)throw new Error(`Row ${T+1}: Duplicate phone number (${p}) also found in row ${X+1}`);A.set(p,T);const N=S.first_message?.trim()??"",de=S.prompt?.trim()??"",Y={phone:p};return N&&(Y.first_message=N.slice(0,1e3)),de&&(Y.prompt=de.slice(0,1e3)),Y})},O=A=>s?!!j(!!A):(_({title:r("toast.error"),description:r("toast.noAgentSelected"),variant:"destructive"}),!1),B=async A=>{if(O(A))try{u(!0);let S=A;if(!S){const X=dr(h,x);if(!X)throw new Error(r("toast.selectDateAndTimeForCalls"));S=X.toISOString()}const T=g(),p=await Ja.scheduleOutboundCall(s,t,T,S);if(!p.success)throw new Error(p.message||r("toast.failedToScheduleCalls"));_({title:r("toast.success"),description:p.message||r("toast.callsScheduledFor",{items:T.length})}),C(!1),v(""),w([D()])}catch(S){const T=Wt(S,r,r("toast.failedToScheduleCalls"));_({title:r("toast.error"),description:T,variant:"destructive"})}finally{u(!1)}},M=()=>new Date().toISOString().replace(/\.\d{3}Z$/,"Z"),H=A=>{const S=A?M():void 0;O(S)&&F({open:!0,instant:A,overrideTime:S})};return e.jsxs(we,{open:f,onOpenChange:C,children:[e.jsx(Sr,{asChild:!0,children:d||e.jsxs(P,{variant:"outline",size:"sm",className:"h-8",children:[e.jsx(ms,{className:"mr-2 h-4 w-4"}),r("common.scheduleCalls")]})}),e.jsxs(Ce,{className:"max-w-4xl max-h-[90vh] overflow-y-auto",children:[e.jsx(Te,{children:e.jsxs(Pe,{className:"text-2xl font-bold text-gray-900 flex items-center gap-2",children:[e.jsx(ms,{className:"h-6 w-6 text-purple-600"}),r("common.scheduleCalls")]})}),e.jsxs("div",{className:"space-y-6",children:[e.jsxs(ke,{className:"border-0 shadow-sm bg-gradient-to-r from-purple-50 to-indigo-50",children:[e.jsx(ze,{className:"pb-3",children:e.jsx(qe,{className:"text-lg font-semibold text-gray-900",children:r("calls.callDetails")})}),e.jsx($e,{className:"space-y-4",children:e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-blue-100 rounded-lg",children:e.jsx(Aa,{className:"h-4 w-4 text-blue-600"})}),e.jsxs("div",{children:[e.jsx(Be,{className:"text-sm font-medium text-gray-700",children:r("calls.phoneNumber")}),e.jsx("p",{className:"text-sm text-gray-900",children:i})]})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-green-100 rounded-lg",children:e.jsx(Da,{className:"h-4 w-4 text-green-600"})}),e.jsxs("div",{children:[e.jsx(Be,{className:"text-sm font-medium text-gray-700",children:r("calls.agent")}),e.jsx("p",{className:"text-sm text-gray-900",children:l})]})]})]})})]}),e.jsxs(ke,{className:"border-0 shadow-sm",children:[e.jsx(ze,{className:"pb-3",children:e.jsxs(qe,{className:"text-lg font-semibold text-gray-900 flex items-center gap-2",children:[e.jsx(Fa,{className:"h-5 w-5 text-orange-600"}),r("common.scheduleDateTime")]})}),e.jsx($e,{children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(Be,{htmlFor:"scheduledDateTime",children:r("common.when")}),e.jsx(re,{id:"scheduledDateTime",type:"datetime-local",value:h,onChange:A=>v(A.target.value),className:"w-full",min:Fn(new Date,x),placeholder:"Select date and time"}),e.jsxs("p",{className:"text-xs text-gray-500",children:[r("common.selectADate",{timezone:x}),e.jsx("br",{}),r("calls.currentTimeInTimezone",{timezone:x,time:ur(new Date,x)})]})]})})]}),e.jsxs(ke,{className:"border-0 shadow-sm",children:[e.jsx(ze,{className:"pb-3",children:e.jsxs(qe,{className:"text-lg font-semibold text-gray-900 flex items-center gap-2",children:[e.jsx(Qa,{className:"h-5 w-5 text-green-600"}),r("common.numbersToCall")]})}),e.jsx($e,{children:e.jsxs("div",{className:"space-y-4",children:[E.map((A,S)=>e.jsxs("div",{className:"p-4 border border-gray-200 rounded-lg bg-gray-50",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsxs(Be,{className:"text-sm font-medium text-gray-700",children:[r("common.call")," #",S+1]}),E.length>1&&e.jsx(P,{type:"button",variant:"ghost",size:"sm",onClick:()=>z(S),className:"h-8 w-8 p-0 text-red-600 hover:text-red-700 hover:bg-red-50",children:e.jsx(ss,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(Be,{htmlFor:`to_number-${S}`,children:[r("common.toNumber")," *"]}),e.jsx(re,{id:`to_number-${S}`,type:"tel",placeholder:"+1234567890",value:A.to_number,onChange:T=>m(S,"to_number",T.target.value),className:"w-full"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(Be,{htmlFor:`first_message-${S}`,children:r("common.firstMessage")}),e.jsx(zt,{id:`first_message-${S}`,placeholder:r("placeholders.enterFirstMessage"),value:A.first_message,onChange:T=>m(S,"first_message",T.target.value),className:"min-h-[80px]"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(Be,{htmlFor:`prompt-${S}`,children:r("common.promptLabel")}),e.jsx(zt,{id:`prompt-${S}`,placeholder:r("placeholders.enterPrompt"),value:A.prompt,onChange:T=>m(S,"prompt",T.target.value),className:"min-h-[80px]"})]})]})]},S)),e.jsxs(P,{type:"button",variant:"outline",onClick:V,className:"w-full border-dashed border-2 border-gray-300 hover:border-purple-400 hover:bg-purple-50",children:[e.jsx(Yt,{className:"mr-2 h-4 w-4"}
1),r("common.addAnotherNumber")]})]})})]}),e.jsx(ke,{className:"border-0 shadow-sm bg-gradient-to-r from-green-50 to-emerald-50",children:e.jsx($e,{className:"pt-6",children:e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm font-medium text-gray-700 mb-2",children:r("calls.summary")}),e.jsx("p",{className:"text-lg font-bold text-green-700",children:r("common.callsBeScheduled",{count:E.length})}),e.jsx("p",{className:"text-sm text-gray-600",children:r("common.AgentWillCall",{agentName:l,count:E.length})})]})})}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4",children:[e.jsx(P,{variant:"outline",onClick:()=>C(!1),disabled:y,children:r("common.cancel")}),e.jsx(P,{type:"button",variant:"outline",onClick:()=>H(!0),disabled:y,className:"bg-gray-50 border-gray-200 text-gray-700 hover:bg-gray-100",children:r("common.startNow")}),e.jsx(P,{onClick:()=>H(!1),disabled:y,className:"bg-purple-600 hover:bg-purple-700",children:y?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"}),r("common.scheduling")]}):e.jsxs(e.Fragment,{children:[e.jsx(ms,{className:"mr-2 h-4 w-4"}),r("common.scheduleCalls")]})})]})]})]}),e.jsx(Gt,{open:q.open,onOpenChange:A=>F(S=>({...S,open:A})),children:e.jsxs(Kt,{overlayClassName:"bg-black/40",children:[e.jsxs(Zt,{children:[e.jsx(Jt,{children:r("agents.batchDialog.confirmTitle")}),e.jsx(Qt,{children:q.instant?r("agents.batchDialog.confirmInstant"):r("agents.batchDialog.confirmScheduled")})]}),e.jsxs(Xt,{children:[e.jsx(es,{disabled:y,children:r("agents.batchDialog.cancel")}),e.jsx(ts,{disabled:y,onClick:()=>{const A=q.overrideTime;F(S=>({...S,open:!1})),B(A)},children:r("agents.batchDialog.confirmAction")})]})]})})]})}function On(s,t){return Es(s)?e.jsx(Ne,{className:"bg-red-100 dark:bg-red-950 text-red-800 dark:text-red-300 border-red-200 dark:border-red-900",children:t("telephony.paymentErrorStatus")}):Xa(s)?e.jsx(Ne,{className:"bg-amber-50 dark:bg-amber-950 text-amber-700 dark:text-amber-200 border-amber-200 dark:border-amber-900",children:t("telephony.paymentProcessingStatus")}):qt(s)?e.jsx(Ne,{className:"bg-gray-100 dark:bg-slate-900 text-gray-600 dark:text-gray-300 border-gray-200 dark:border-slate-700",children:t("telephony.subscriptionInactiveStatus")}):e.jsx(Ne,{className:"bg-green-100 dark:bg-green-950 text-green-800 dark:text-green-600 border-green-200 dark:border-green-950",children:t("agents.active")})}function Rn({number:s,agents:t,agentsLoading:i,loadPhoneNumbers:l,loading:d,setLoading:r,setPhoneNumbers:_,cb:f}){const{toast:C}=We(),y=Ss(),{t:u,i18n:h}=Re(),{outboundEnabled:v}=Nr(),I=ws(R=>R.ability),x=I.can(he.UPDATE,js(me.PHONE_NUMBER,{id:s.id})),D=I.can(he.DELETE,js(me.PHONE_NUMBER,{id:s.id})),E=I.can(he.CREATE,me.CALL),[w,q]=a.useState(null),[F,V]=a.useState(!1),[z,m]=a.useState(null),j=s.agent_id,g=vt(s),O=Es(g),B=qt(g),M=!B,H=Cr(s),A=en(s),S=O||B,T=S?"text-red-800 dark:text-red-200":"text-black dark:text-slate-300",p=S?"text-red-700 dark:text-red-200":"text-gray-700 dark:text-gray-400",X=S?"text-red-600 dark:text-red-300":"text-gray-400",N=t.find(R=>R.id===j),de=A&&!!j&&v&&!!N?.outbound_call_enabled,Y=()=>{if(!v){C({title:u("features.upgradeRequiredTitle"),description:u("features.scheduleNotAvailable"),variant:"default"});return}if(!N?.outbound_call_enabled){C({title:u("features.upgradeRequiredTitle"),description:u("features.agentOutboundDisabled"),variant:"default"});return}C({title:u("toast.error"),description:u("errors.somethingWentWrong"),variant:"destructive"})},ne=async R=>{if(R.agent_id)try{q(R.id);const k=t.find(n=>n.id===R.agent_id);if(!k)throw new Error("Agent not found");const $=await Ct.disconnectPhoneNumberFromAgent(k.id,R.id);if($&&$.id)C({title:u("toast.success"),description:u("toast.phoneNumberDisconnected")}),_(n=>n.map(b=>b.id===R.id?{...b,agent_id:null}:b));else throw new Error("Failed to disconnect phone number - invalid response")}catch(k){console.error("Error disconnecting phone number:",k),C({title:u("toast.error"),description:k instanceof Error?k.message:u("toast.failedToDisconnectPhone"),variant:"destructive"})}finally{q(null)}},L=Ze(s.phone_number)||s.phone_number,ie=s.expiration_at?new Date(s.expiration_at):null,ve=s.last_paid_at?new Date(s.last_paid_at):null,Z=ie&&!Number.isNaN(ie.getTime())?ie:ve&&!Number.isNaN(ve.getTime())?rn(ve,1):null,ue=a.useMemo(()=>new Intl.DateTimeFormat(h.language||"en",{year:"numeric",month:"short",day:"numeric"}),[h.language]),ge=Z&&!Number.isNaN(Z.getTime())?ue.format(Z):null,Ae=s.verified_at?new Date(s.verified_at):null,K=Ae&&!Number.isNaN(Ae.getTime())?ue.format(Ae):null,ee=()=>{if(i){C({title:u("toast.loadingAgents"),description:u("telephony.loadingAgentsMessage"),variant:"default"});return}if(t.length===0){C({title:u("toast.noAgentsAvailable"),description:u("telephony.noAgentsAvailableMessage"),variant:"destructive"});return}f()},le=async R=>{if(!R||!R.id){C({title:u("toast.error"),description:u("messages.phoneNumberIdFormatInvalid"),variant:"destructive"});return}
1if(!Ns.test(R.id)){C({title:u("toast.invalidPhoneNumberId"),description:u("messages.phoneNumberIdFormatInvalid"),variant:"destructive"});return}try{r(!0),await Se.deleteNumber(R.id),C({title:u("toast.success"),description:u("messages.phoneNumberDeletedSuccessfully")}),l()}catch(k){console.error("Delete number error:",k);const $=u("messages.failedToDeletePhoneNumber"),n=typeof k=="object"&&k!==null&&"detail"in k&&typeof k.detail=="string"?k.detail:void 0,b=_r(k)?Wt(k,u,$):k instanceof Error&&k.message||n||(typeof k=="string"?k:void 0)||$;C({title:u("toast.deleteFailed"),description:b,variant:"destructive"})}finally{r(!1)}},Ge=R=>{m(R),V(!0)},c=()=>{V(!1),m(null)};return e.jsxs(e.Fragment,{children:[e.jsxs(xe,{className:`border-b ${O?"border-red-200 dark:border-red-900 bg-red-100 dark:bg-red-950/40 hover:bg-red-200 dark:hover:bg-red-900/60 text-red-800 dark:text-red-200":B?"border-red-300 dark:border-red-900 bg-red-200 dark:bg-red-950/30 hover:bg-red-300 dark:hover:bg-red-900/70 text-red-800 dark:text-red-200":"border-gray-100 dark:border-slate-800 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-400"}`,children:[e.jsx(W,{className:`font-medium ${T}`,children:e.jsxs("div",{className:"flex flex-col",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("span",{children:L}),s.source==="sip_trunk"&&e.jsx("span",{className:"inline-flex items-center rounded-full border border-blue-200 dark:border-blue-900 bg-blue-50 dark:bg-blue-950/40 px-2 py-0.5 text-xs font-medium text-blue-700 dark:text-blue-300",title:u("telephony.sourceBadgeSipTooltip"),children:u("telephony.sourceBadgeSip")}),s.source==="manual"&&e.jsx("span",{className:"inline-flex items-center rounded-full border border-amber-200 dark:border-amber-900 bg-amber-50 dark:bg-amber-950/40 px-2 py-0.5 text-xs font-medium text-amber-700 dark:text-amber-300",title:u("telephony.sourceBadgeManualTooltip"),children:u("telephony.sourceBadgeManual")}),H&&e.jsx(_s,{})]}),H&&e.jsx("span",{className:"text-xs text-gray-500 dark:text-gray-400",children:K?u("importNumber.verifiedOn",{date:K}):s.verification_status==="pending"?u("importNumber.status.pending"):u("importNumber.status.failed")}),!H&&ge&&e.jsx("span",{className:"text-xs text-gray-500 dark:text-gray-400",children:u("telephony.nextChargeDate",{date:ge})})]})}),e.jsx(W,{className:p,children:s.country_code?s.country_code:"-"}),e.jsx(W,{className:p,children:u(vr(s.line_type||Ua.LOCAL))}),e.jsx(W,{className:p,children:s.agent_id?e.jsx("button",{onClick:()=>y(`/agents/${s.agent_id}`),className:`${O?"text-red-700 hover:text-red-900":"text-blue-600 hover:text-blue-800"} hover:underline cursor-pointer transition-colors`,children:t.find(R=>R.id===s.agent_id)?.name||u("common.unknownAgent")}):e.jsx("span",{className:X,children:u("common.unassigned")})}),e.jsx(W,{children:H?e.jsx(wr,{status:s.verification_status}):On(g,u)}),e.jsx(W,{className:`text-right ${p} font-medium tracking-tight`,children:Ps(s.price)}),e.jsx(W,{className:"text-right",children:e.jsxs("div",{className:"flex justify-end items-center space-x-2",children:[Pa.NODE_ENV==="development"&&e.jsxs("span",{className:"text-xs text-gray-400 mr-2",children:["Active: ",s.is_active?"Y":"N"," | Agents: ",t.length," ","| Loading: ",i?"Y":"N"]}),x&&e.jsx(P,{variant:"outline",size:"sm",className:`${!M||!A||i||t.length===0?"text-gray-400 border-gray-200 cursor-not-allowed dark:border-slate-800":O?"text-red-700 border-red-300 hover:text-red-900 hover:bg-red-50 dark:border-red-900":"text-blue-600 hover:text-blue-800 hover:bg-blue-50 border-blue-200 dark:border-slate-800"}`,onClick:()=>ee(),disabled:!M||!A||i||t.length===0,title:M?A?i?u("telephony.loadingAgents"):t.length===0?u("telephony.noAgentsAvailable"):s.agent_id?u("telephony.reassignToDifferentAgent"):u("telephony.connectToAgent"):u("importNumber.notVerifiedNotice"):u("telephony.inactiveNumberNotice"),children:i?u("common.loading"):s.agent_id?u("common.reassign"):u("common.connect")}),s.agent_id&&x&&e.jsx(P,{variant:"ghost",size:"sm",onClick:()=>ne(s),disabled:!M||w===s.id,className:"text-orange-600 hover:text-orange-800 hover:bg-orange-50 disabled:opacity-50",title:u(M?"telephony.disconnectFromAgent":"telep
1hony.inactiveNumberNotice"),children:w===s.id?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-orange-600 mr-2"}),u("common.disconnecting")]}):e.jsxs(e.Fragment,{children:[e.jsx(an,{className:"h-4 w-4 mr-2"}),u("common.disconnect")]})}),j&&M&&A&&E&&(de?e.jsx(In,{agentId:j,phoneNumberId:s.id,phoneNumber:L,agentName:N?.name||u("common.unknownAgent"),trigger:e.jsx(P,{variant:"ghost",size:"sm",className:"text-purple-600 hover:text-purple-800 hover:bg-purple-50",title:u("tooltips.scheduleCalls"),children:u("telephony.schedule")})}):e.jsx(P,{variant:"ghost",size:"sm",className:"text-purple-600 hover:text-purple-800 hover:bg-purple-50",onClick:Y,children:u("telephony.schedule")})),D&&e.jsx(P,{variant:"ghost",size:"sm",className:"text-red-600 hover:text-red-800 hover:bg-red-50",onClick:()=>Ge(s),disabled:d||!M,title:u("tooltips.deletePhoneNumber"),children:u("telephony.delete")})]})})]},s.id),e.jsx(Gt,{open:F,onOpenChange:V,children:e.jsxs(Kt,{children:[e.jsxs(Zt,{children:[e.jsx(Jt,{children:u("telephony.deletePhoneNumberModalTitle",{phone:L})}),e.jsx(Qt,{className:"space-y-3 text-gray-600 dark:text-gray-300",children:H?e.jsx("span",{children:u("importNumber.deleteDescription")}):e.jsxs(e.Fragment,{children:[e.jsx("span",{children:u("telephony.deletePhoneNumberModalDescription")}),e.jsx("span",{children:u("telephony.deletePhoneNumberModalWarning")})]})})]}),e.jsxs(Xt,{children:[e.jsx(es,{onClick:c,children:u("telephony.deletePhoneNumberModalCancel")}),e.jsx(ts,{className:"bg-red-600 hover:bg-red-700 text-white",onClick:async()=>{await le(z),c()},children:u("telephony.deletePhoneNumberModalConfirm")})]})]})})]})}const Mn={register_succeeded:{Icon:Ds,className:"text-green-600"},contact_reachable:{Icon:Tr,className:"text-green-600"},register_failed:{Icon:wt,className:"text-red-600"},contact_unreachable:{Icon:wt,className:"text-amber-600"}};function Ln(s){return Mn[s]??{Icon:Ir,className:"text-gray-500"}}function It(s,t){if(!s)return t;const i=Date.parse(s);if(Number.isNaN(i))return t;const l=Date.now()-i;return l<5e3?"just now":l<6e4?`${Math.round(l/1e3)}s ago`:l<36e5?`${Math.round(l/6e4)} min ago`:l<864e5?`${Math.round(l/36e5)}h ago`:`${Math.round(l/864e5)}d ago`}function ps(s){if(!s)return null;const t=Date.parse(s);return Number.isNaN(t)?s:new Date(t).toLocaleString()}function Ur({registration:s,endpointLabel:t,loading:i,error:l}){const{t:d}=Re(),r=a.useMemo(()=>[...s?.recent_events??[]].sort((v,I)=>{const x=Date.parse(v.at),D=Date.parse(I.at);return Number.isNaN(x)||Number.isNaN(D)?0:D-x}),[s?.recent_events]);if(i&&!s)return e.jsxs("div",{className:"flex items-center gap-2 text-xs text-nappai-text-secondary",children:[e.jsx(ae,{className:"h-3 w-3 animate-spin"}),d("regActivity.checking")]});if(l&&!s)return e.jsx("div",{className:"text-xs text-red-600",children:l});const _=It(s?.last_success_at,d("regActivity.never")),f=It(s?.last_failure_at,d("regActivity.never")),C=It(s?.last_state_change_at,d("regActivity.never")),y=ps(s?.last_success_at),u=ps(s?.last_failure_at);return e.jsxs("div",{className:"space-y-3 text-xs",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-medium text-nappai-text-primary",children:d("regActivity.title")}),t?e.jsx("div",{className:"truncate font-mono text-[11px] text-nappai-text-secondary",title:t,children:t}):null]}),e.jsxs("dl",{className:"grid grid-cols-[auto_1fr] gap-x-3 gap-y-1",children:[e.jsx("dt",{className:"text-nappai-text-secondary",children:d("regActivity.lastStateChange")}),e.jsx("dd",{children:C}),e.jsx("dt",{className:"text-nappai-text-secondary",children:d("regActivity.lastSuccess")}),e.jsx("dd",{title:y??void 0,children:_}),e.jsx("dt",{className:"text-nappai-text-secondary",children:d("regActivity.lastFailure")}),e.jsx("dd",{title:u??void 0,children:f})]}),s?.last_failure_reason?e.jsxs("div",{className:"rounded border border-red-200 bg-red-50 px-2 py-1 text-[11px] text-red-700",children:[e.jsx("div",{className:"font-medium",children:d("regActivity.lastFailure")}),e.jsx("div",{className:"whitespace-pre-wrap break-words",children:s.last_failure_reason})]}):null,e.jsxs("div",{children:[e.jsx("div",{className:"mb-1 text-nappai-text-secondary",children:d("regActivity.recentEvents")}),r.length===0?e.jsx("div",{className:"text-nappai-text-secondary italic",children:d("regActivity.noEvents")}):e.jsx("ul",{className:"max-h-48 space-y-1 overflow-y-auto pr-1",children:r.map((h,v)=>{const{Icon:I,className:x}=Ln(h.kind),D=ps(h.at)??h.at,E=(()=>{switch(h.kind){case"register_succeeded":return d("regActivity.eventRegistered");case"register_failed":return d("regActivity.eventFailed");case"contact_reachable":return d("regActivity.eventReachable");case"contact_unreachable":return d("regActivity.eventUnreachable");default:return h.kind}})();return e.jsxs("li",{className:"flex items-start gap-2",children:[e.jsx(I,{className:Ye("mt-0.5 h-3.5 w-3.5 flex-none",x)}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-baseline gap-x-2",children:[e.jsx("span",{className:"font-medium",children:E}),e.jsx("span",{className:"text-[10px] text-nappai-text-secondary",title:D,children:It(h.at,d("regActivity.never"))})]}),h.peer_ip?e.jsx("div",{className:"font-mono text-[10px] text-nappai-text-secondary",children:h.peer_ip}):null,h.reason?e.jsx("div",{className:"break-words text-[11px] text-nappai-text-secondary",children:h.reason}):null]})]},`${h.at}-${h.kind}-${v}`)})})]})]})}const $n=50,Un={register_succeeded:{Icon:Ds,className:"text-green-600"},contact_reachable:{Icon:Tr,className:"text-green-600"},register_failed:{Icon:wt,className:"text-red-600"},contact_unreachable:{Icon:wt,className:"text-amber-600"}};function Bn(s){return Un[s]??{Icon:Ir,className:"text-nappai-text-secondary"}}function Hn(s,t){if(!s)return t;const i=Date.parse(s);if(Number.isNaN(i))return t;const l=Date.now()-i;return l<5e3?"just now":l<6e4?`${Math.round(l/1e3)}s ago`:l<36e5?`${Math.round(l/6e4)} min ago`:l<864e5?`${Math.round(l/36e5)}h ago`:`${Math.round(l/864e5)}d ago`}function Vn(s){const t=Date.parse(s);return Number.isNaN(t)?s:new Date(t).toLocaleTimeString()}function Ht(s){const t=Date.parse(s);if(Number.isNaN(t))return"unknown";const i=new Date(t);return`${i.getFullYear()}
1-${String(i.getMonth()+1).padStart(2,"0")}-${String(i.getDate()).padStart(2,"0")}`}function zn(s,t,i){const l=Ht(s);if(l===t)return"Today";if(l===i)return"Yesterday";const d=Date.parse(s);return Number.isNaN(d)?l:new Date(d).toLocaleDateString(void 0,{weekday:"long",year:"numeric",month:"short",day:"numeric"})}function qn(s){const t=Ht(new Date().toISOString()),i=Ht(new Date(Date.now()-864e5).toISOString()),l=[];let d=null;for(const r of s){const _=Ht(r.at);(!d||d.key!==_)&&(d={key:_,events:[]},l.push({key:_,label:zn(r.at,t,i),events:d.events})),d.events.push(r)}return l}function Br({fetcher:s,pageSize:t=$n,enabled:i=!0}){const{t:l}=Re(),[d,r]=a.useState([]),[_,f]=a.useState(null),[C,y]=a.useState(!1),[u,h]=a.useState(null),[v,I]=a.useState(!1),[x,D]=a.useState(!1),E=a.useRef(0),w=a.useCallback(async({cursor:z,append:m})=>{const j=++E.current;I(!0),m||h(null);try{const g=await s({limit:t,cursor:z??void 0});if(j!==E.current)return;y(!!g.unsupported),h(g.error??null),f(g.next_cursor??null),r(O=>m?[...O,...g.events]:g.events)}catch(g){if(j!==E.current)return;const O=g instanceof Error?g.message:typeof g=="string"?g:l("regTimeline.errorGeneric");h(O),m||(r([]),f(null))}finally{j===E.current&&(I(!1),D(!0))}},[s,t,l]);a.useEffect(()=>{!i||x||w({cursor:null,append:!1})},[i]);const q=a.useCallback(()=>{w({cursor:null,append:!1})},[w]),F=a.useCallback(()=>{!_||v||w({cursor:_,append:!0})},[w,_,v]),V=a.useMemo(()=>qn(d),[d]);return!x&&v?e.jsxs("div",{className:"flex items-center justify-center gap-2 py-8 text-sm text-nappai-text-secondary",children:[e.jsx(ae,{className:"h-4 w-4 animate-spin"}),l("regTimeline.loading")]}):e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("div",{className:"text-sm text-nappai-text-secondary",children:d.length>0?l("regTimeline.eventCount",{count:d.length}):l("regTimeline.empty")}),e.jsxs(P,{variant:"ghost",size:"sm",onClick:q,disabled:v,className:"h-7 px-2 text-xs",children:[e.jsx(As,{className:Ye("mr-1 h-3 w-3",v&&"animate-spin")}),l("regTimeline.refresh")]})]}),C?e.jsx("div",{className:"rounded border border-amber-200 bg-amber-50 px-3 py-2 text-xs text-amber-800",children:l("regTimeline.unsupported")}):null,u&&!C?e.jsxs("div",{className:"rounded border border-red-200 bg-red-50 px-3 py-2 text-xs text-red-700",children:[l("regTimeline.errorPrefix")," ",u]}):null,d.length===0&&!u&&!C?e.jsx("div",{className:"rounded border border-dashed border-gray-200 px-3 py-6 text-center text-xs text-nappai-text-secondary",children:l("regTimeline.noEventsYet")}):null,V.length>0?e.jsx("ol",{className:"space-y-4",children:V.map(z=>e.jsxs("li",{children:[e.jsx("div",{className:"mb-1 text-[11px] font-medium uppercase tracking-wide text-nappai-text-secondary",children:z.label}),e.jsx("ul",{className:"space-y-1 border-l border-gray-200 pl-3",children:z.events.map(m=>{const{Icon:j,className:g}=Bn(m.kind),O=Vn(m.at),B=Hn(m.at,l("regActivity.never")),M=(()=>{switch(m.kind){case"register_succeeded":return l("regActivity.eventRegistered");case"register_failed":return l("regActivity.eventFailed");case"contact_reachable":return l("regActivity.eventReachable");case"contact_unreachable":return l("regActivity.eventUnreachable");default:return m.kind}})();return e.jsxs("li",{className:"flex items-start gap-2 py-0.5",children:[e.jsx(j,{className:Ye("mt-0.5 h-3.5 w-3.5 flex-none",g)}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-baseline gap-x-2",children:[e.jsx("span",{className:"text-xs font-medium text-nappai-text-primary",children:M}),e.jsx("span",{className:"text-[10px] text-nappai-text-secondary",title:B,children:O})]}),m.peer_ip?e.jsx("div",{className:"font-mono text-[10px] text-nappai-text-secondary",children:m.peer_ip}):null,m.reason?e.jsx("div",{className:"break-words text-[11px] text-nappai-text-secondary",children:m.reason}):null,m.source&&m.source!=="ari"?e.jsxs("div",{className:"text-[10px] uppercase tracking-wide text-nappai-text-secondary opacity-60",children:[l("regTimeline.sourcePrefix")," ",m.source]}):null]})]},`${m.id??m.at}-${m.kind}-${m.peer_ip??""}`)})})]},z.key))}):null,_?e.jsx("div",{className:"flex justify-center pt-1",children:e.jsxs(P,{variant:"outline",size:"sm",onClick:F,disabled:v,className:"h-7 text-xs",children:[v?e.jsx(ae,{className:"mr-1 h-3 w-3 animate-spin"}):null,l("regTimeline.loadMore")]})}):null]})}const mr=60,Yn=1200,Wn=250;
1function gs(s){if(s instanceof jr){const t=s.status,l=s.details?.detail;if(t===403)return{kind:"disabled"};if(t===409){let d=null;if(l&&typeof l=="object"&&"retry_after_s"in l){const r=l.retry_after_s;typeof r=="number"&&Number.isFinite(r)&&(d=r)}return{kind:"busy",retryAfterS:d}}if(t===429){let d=null;if(l&&typeof l=="object"&&"retry_after_s"in l){const r=l.retry_after_s;typeof r=="number"&&Number.isFinite(r)&&(d=r)}return{kind:"rate_limited",retryAfterS:d}}return t===400?(typeof l=="string"?l:null)==="peer_ip_unresolved"?{kind:"peer_ip_unresolved"}:{kind:"generic",message:s.message}:t===502||t===503||t===504?{kind:"edge_unreachable",message:s.message}:{kind:"generic",message:s.message}}return{kind:"generic",message:s instanceof Error?s.message:String(s)}}function bs(s){switch(s){case"queued":return{label:"queued",cls:"bg-amber-100 text-amber-800 border-amber-200"};case"running":return{label:"running",cls:"bg-sky-100 text-sky-800 border-sky-200"};case"completed":return{label:"completed",cls:"bg-emerald-100 text-emerald-800 border-emerald-200"};case"expired":return{label:"expired",cls:"bg-gray-100 text-gray-700 border-gray-200"};case"failed":return{label:"failed",cls:"bg-red-100 text-red-700 border-red-200"};default:return{label:s??"unknown",cls:"bg-gray-100 text-gray-700 border-gray-200"}}}function Gn(s){if(!s)return null;const t=new Date(s).getTime();return Number.isFinite(t)?t:null}function Kn(s){return s.direction==="in"?"in":s.direction==="out"?"out":"unknown"}function Zn(s,t){if(!s)return t;const i=new Date(s),l=i.getTime();if(!Number.isFinite(l))return String(s);const d=(r,_=2)=>r.toString().padStart(_,"0");return`${d(i.getHours())}:${d(i.getMinutes())}:${d(i.getSeconds())}.${d(i.getMilliseconds(),3)}`}function Jn(s){if(s.method)return s.method.toUpperCase();if(s.status!=null){const t=(s.status!=null,"");return`${s.status}${t}`}return"SIP"}function Qn(s){if(s.method){const t=s.method.toUpperCase();return t==="REGISTER"?"border-l-violet-500":t==="INVITE"?"border-l-sky-500":t==="BYE"||t==="CANCEL"?"border-l-orange-500":t==="OPTIONS"?"border-l-gray-400":t==="ACK"?"border-l-emerald-500":"border-l-gray-300"}if(s.status!=null){const t=Number(s.status);if(Number.isFinite(t)){if(t>=500)return"border-l-red-500";if(t>=400)return"border-l-amber-500";if(t>=300)return"border-l-blue-400";if(t>=200)return"border-l-emerald-500";if(t>=100)return"border-l-sky-400"}}return"border-l-gray-300"}function Xn(s){return s==null||!Number.isFinite(s)?null:s<1024?`${s} B`:s<1024*1024?`${(s/1024).toFixed(1)} KB`:`${(s/(1024*1024)).toFixed(2)} MB`}function Hr({open:s,onOpenChange:t,trunkId:i,trunkLabel:l,starter:d,fetcher:r,downloader:_}){const{t:f}=Re(),[C,y]=a.useState(!1),[u,h]=a.useState(!1),[v,I]=a.useState(null),[x,D]=a.useState(null),[E,w]=a.useState(null),[q,F]=a.useState(null),[V,z]=a.useState(()=>Date.now()),[m,j]=a.useState(!1),[g,O]=a.useState(null),[B,M]=a.useState(()=>new Set),H=a.useRef(null),A=a.useRef(null),S=a.useRef(!1),T=a.useCallback(()=>{y(!1),h(!1),I(null),D(null),w(null),F(null),j(!1),O(null),M(new Set)},[]),p=a.useCallback(()=>{H.current&&(clearInterval(H.current),H.current=null),A.current&&(clearInterval(A.current),A.current=null)},[]);a.useEffect(()=>{if(!s){S.current=!0,p(),T();return}return S.current=!1,()=>{S.current=!0,p()}},[s,p,T]);const X=a.useCallback(async()=>{F(null),D(null),w(null),I(null),y(!0);try{const K=await d(i,{seconds:mr});if(S.current)return;w(K),I(K.trace_id),z(Date.now())}catch(K){S.current||F(gs(K))}finally{S.current||y(!1)}},[d,i]),N=a.useCallback(async()=>{if(!_||!v)return;j(!0),O(null);let K=null;try{const ee=await _(i,v);K=ee.blobUrl;const le=document.createElement("a");le.href=ee.blobUrl,le.download=ee.filename,le.rel="noopener",document.body.appendChild(le),le.click(),document.body.removeChild(le)}catch(ee){if(ee instanceof Ba)O(Wt(ee,f,f("liveTrace.errors.pcapDownloadFailed")));else{const le=gs(ee);O(le.kind==="generic"&&le.message?le.message:f("liveTrace.errors.pcapDownloadFailed"))}}finally{K&&setTimeout(()=>{try{URL.revokeObjectURL(K)}catch{}},6e4),j(!1)}},[_,v,i,f]);a.useEffect(()=>{if(!s||!v)return;S.current=!1,h(!0);const K=async()=>{try{const ee=await r(i,v);if(S.current)return;D(ee),(ee.status==="completed"||ee.status==="expired"||ee.status==="failed")&&(h(!1),H.current&&(clearInterval(H.current),H.current=null))}catch(ee){if(S.current)return;F(gs(ee)),h(!1),H.current&&(clearInterval(H.current),H.current=null)}};return K(),H.current=setInterval(()=>void K(),Yn),A.current=setInterval(()=>
1z(Date.now()),Wn),()=>{H.current&&(clearInterval(H.current),H.current=null),A.current&&(clearInterval(A.current),A.current=null)}},[s,v,i,r]);const de=a.useMemo(()=>Gn(x?.expires_at??E?.expires_at??null),[x?.expires_at,E?.expires_at]),Y=a.useMemo(()=>de?de-V:null,[de,V]),ne=x?.status??(v?"running":null),L=q?ei(q,f):null,ie=x?.peer_ip??E?.peer_ip??null,ve=x?.endpoint??null,Z=x?.messages??[],ue=!!_&&!!x?.pcap_available&&x.status!=="running",ge=Xn(x?.pcap_size_bytes??null),Ae=K=>{M(ee=>{const le=new Set(ee);return le.has(K)?le.delete(K):le.add(K),le})};return e.jsx(we,{open:s,onOpenChange:K=>{K||(S.current=!0,p()),t(K)},children:e.jsxs(Ce,{className:"sm:max-w-3xl",onPointerDownOutside:K=>{(u||C)&&K.preventDefault()},onInteractOutside:K=>{(u||C)&&K.preventDefault()},children:[e.jsxs(Te,{children:[e.jsxs(Pe,{className:"flex items-center gap-2",children:[f("liveTrace.title"),ne?e.jsx(Ne,{className:Ye("border",bs(ne).cls),children:f(`liveTrace.status.${bs(ne).label}`,{defaultValue:bs(ne).label})}):null]}),e.jsx(Oe,{children:l?f("liveTrace.descriptionWithLabel",{name:l}):f("liveTrace.description")})]}),e.jsxs("div",{className:"space-y-3",children:[L,!q&&!v?e.jsx("div",{className:"rounded-md border border-dashed border-gray-200 p-4 text-sm text-gray-600 dark:border-slate-700 dark:text-slate-300",children:f("liveTrace.preStartHint",{seconds:mr})}):null,v?e.jsxs("div",{className:"flex flex-wrap items-center gap-3 text-xs text-gray-600 dark:text-slate-300",children:[e.jsxs("span",{className:"inline-flex items-center gap-1",children:[e.jsx(nn,{className:"h-3.5 w-3.5"}),Y!=null?f("liveTrace.expiresIn",{time:Ka(Math.ceil(Y/1e3),"long")}):f("liveTrace.runningNoExpiry")]}),ie?e.jsxs("span",{className:"font-mono",children:[f("liveTrace.peerIp"),": ",ie]}):null,ve?e.jsxs("span",{className:"font-mono truncate max-w-[16rem]",children:[f("liveTrace.endpoint"),": ",ve]}):null,e.jsx("span",{className:"text-gray-400",children:f("liveTrace.frameCount",{count:Z.length})})]}):null,v?e.jsx("div",{className:"rounded-md border border-gray-200 bg-gray-50 dark:border-slate-700 dark:bg-slate-900",children:ti({messages:Z,polling:u,expanded:B,onToggle:Ae,t:f})}):null,ue?e.jsxs("div",{className:"flex items-center justify-between gap-3 rounded-md border border-gray-200 bg-white p-3 text-sm dark:border-slate-700 dark:bg-slate-900",children:[e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"font-medium text-gray-900 dark:text-slate-100",children:f("liveTrace.pcap.title")}),e.jsx("span",{className:"text-xs text-gray-500 dark:text-slate-400",children:ge?f("liveTrace.pcap.subtitleWithSize",{size:ge}):f("liveTrace.pcap.subtitle")}),g?e.jsx("span",{className:"mt-1 text-xs text-red-600",children:g}):null]}),e.jsxs(P,{variant:"outline",size:"sm",onClick:()=>void N(),disabled:m,children:[m?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(on,{className:"mr-2 h-4 w-4"}),f("liveTrace.pcap.download")]})]}):null]}),e.jsx(Ee,{className:"mt-4 gap-2 sm:gap-2",children:v?e.jsxs(e.Fragment,{children:[e.jsxs(P,{variant:"outline",onClick:()=>{S.current=!0,p(),T()},children:[e.jsx(ln,{className:"mr-2 h-4 w-4"}),f("liveTrace.runAgain")]}),e.jsxs(P,{variant:"outline",onClick:()=>t(!1),children:[e.jsx(kr,{className:"mr-2 h-4 w-4"}),f("common.close")]})]}):e.jsxs(e.Fragment,{children:[e.jsx(P,{variant:"outline",onClick:()=>t(!1),disabled:C,children:f("common.cancel")}),e.jsxs(P,{onClick:()=>void X(),disabled:C,className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium",children:[C?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,f("liveTrace.startCapture")]})]})})]})})}function ei(s,t){const i=s.kind==="disabled"?"bg-gray-50 border-gray-200 text-gray-700 dark:bg-slate-800 dark:border-slate-700 dark:text-slate-200":s.kind==="edge_unreachable"?"bg-amber-50 border-amber-200 text-amber-800":"bg-red-50 border-red-200 text-red-800",l=(()=>{switch(s.kind){case"disabled":return t("liveTrace.errors.disabled");case"busy":return s.retryAfterS?t("liveTrace.errors.busyRetry",{seconds:Math.ceil(s.retryAfterS)}):t("liveTrace.errors.busy");case"rate_limited":return s.retryAfterS?t("liveTrace.errors.rateLimitedRetry",{seconds:Math.ceil(s.retryAfterS)}):t("liveTrace.errors.rateLimited");case"peer_ip_unresolved":return t("liveTrace.errors.peerIpUnresolved");case"edge_unreachable":return t("liveTrace.errors.edgeUnreachable");default:return s.message||t("liveTrace.errors.generic")}})();return e.jsx("div",{className:Ye("rounded-md border p-3 text-sm",i),children:l})}function ti({messages:s,polling:t,expanded:i,onToggle:l,t:d}){return s.length===0?e.jsx("div",{className:"p-4 text-sm text-gray-500 dark:text-slate-400 flex items-center gap-2",children:t?e.jsxs(e.Fragment,{children:[e.jsx(ae,{className:"h-4 w-4 animate-spin"}),e.jsx("span",{children:d("liveTrace.waitingForPackets")})]}):e.jsx("span",{children:d("liveTrace.noPackets")})}):e.jsx("ol",{className:"max-h-[28rem] overflow-auto divide-y divide-gray-100 dark:divide-slate-800 font-mono text-xs",children:s.map((r,_)=>{const f=Kn(r),C=f==="in"?"<-":f==="out"?"->":"··",y=f==="in"?"text-emerald-700 dark:text-emerald-300":f==="out"?"text-sky-700 dark:text-sky-300":"text-gray-500 dark:text-slate-400",u=Qn(r),h=i.has(_),v=!!(r.headers||r.body||r.reason),I=Zn(r.at,d("liveTrace.noTimestamp")),x=Jn(r),D=r.reason?` ${r.reason}`:"";return e.jsxs("li",{className:Ye("border-l-4 px-2 py-1.5 transition-colors",u,h?"bg-white dark:bg-slate-950":""),children:[e.jsxs("button",{type:"button",className:"w-full text-left flex items-baseline gap-2 cursor-pointer",onClick:()=>v?l(_):void 0,"aria-expanded":h,children:[v?h?e.jsx(cn,{className:"h-3 w-3 mt-0.5 text-gray-400 shrink-0"}):e.jsx(Ia,{className:"h-3 w-3 mt-0.5 text-gray-400 shrink-0"}):e.jsx("span",{className:"inline-block w-3 shrink-0"}),e.jsx("span",{className:"text-gray-500 tabular-nums",children:I}
1),e.jsx("span",{className:Ye("font-bold",y),children:C}),e.jsx("span",{className:"text-gray-700 dark:text-slate-200 font-bold",children:x}),D?e.jsx("span",{className:"text-gray-500 dark:text-slate-400 truncate",children:D}):null,e.jsx("span",{className:"text-gray-400 truncate ml-auto pl-2 max-w-[18rem]",children:(r.src??"?")+" → "+(r.dst??"?")})]}),h&&v?e.jsx("pre",{className:"mt-1 whitespace-pre-wrap break-all text-gray-700 dark:text-slate-300 bg-gray-50 dark:bg-slate-900 p-2 rounded",children:[r.headers,r.body].filter(Boolean).join(` 2 3`)}):null]},`${r.at??"no-ts"}-${_}`)})})}const si=/^\+[1-9]\d{1,14}$/;function Vr({open:s,onOpenChange:t,trunkId:i,trunkLabel:l,agents:d,fetcher:r,adder:_,remover:f,onChanged:C}){const{t:y}=Re(),{toast:u}=We(),[h,v]=a.useState([]),[I,x]=a.useState(!1),[D,E]=a.useState(!1),[w,q]=a.useState(null),[F,V]=a.useState(null),[z,m]=a.useState(""),[j,g]=a.useState(""),[O,B]=a.useState(null),M=a.useCallback(async()=>{try{x(!0);const T=await r(i);v(T)}catch{u({title:y("toast.error"),description:y("sipTrunkNumbers.loadFailed"),variant:"destructive"})}finally{x(!1)}},[r,i,y,u]);a.useEffect(()=>{s&&(m(""),g(""),B(null),M())},[s,M]);const H=async()=>{const T=z.trim();if(!T){B(y("sipTrunkNumbers.errPhoneRequired"));return}const p=vs(T);if(!si.test(p)){B(y("sipTrunkNumbers.errPhoneInvalid"));return}if(h.some(X=>X.phone_number===p)){B(y("sipTrunkNumbers.errPhoneDuplicate"));return}if(!j){u({title:y("toast.error"),description:y("sipTrunkNumbers.selectAgent"),variant:"destructive"});return}try{E(!0),await _(i,{phone_number:p,agent_id:j}),u({title:y("toast.success"),description:y("sipTrunkNumbers.addSuccess")}),m(""),g(""),B(null),await M(),C&&await C()}catch(X){u({title:y("toast.error"),description:X instanceof Error?X.message:y("sipTrunkNumbers.addFailed"),variant:"destructive"})}finally{E(!1)}},A=async(T,p)=>{try{V(T),await Se.connectNumber({phone_number_id:T,agent_id:p}),u({title:y("toast.success"),description:y("sipTrunkNumbers.assignSuccess")}),await M(),C&&await C()}catch(X){u({title:y("toast.error"),description:X instanceof Error?X.message:y("sipTrunkNumbers.assignFailed"),variant:"destructive"})}finally{V(null)}},S=async T=>{if(window.confirm(y("sipTrunkNumbers.removeConfirm")))try{q(T),await f(i,T),u({title:y("toast.success"),description:y("sipTrunkNumbers.removeSuccess")}),await M(),C&&await C()}catch(p){u({title:y("toast.error"),description:p instanceof Error?p.message:y("sipTrunkNumbers.removeFailed"),variant:"destructive"})}finally{q(null)}};return e.jsx(we,{open:s,onOpenChange:t,children:e.jsxs(Ce,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsxs(Te,{children:[e.jsxs(Pe,{className:"text-xl font-semibold text-gray-900 dark:text-white",children:[y("sipTrunkNumbers.title"),l?` — ${l}`:""]}),e.jsx(Oe,{children:y("sipTrunkNumbers.description")})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(xt,{children:[e.jsx(pt,{children:e.jsxs(xe,{className:"bg-gray-50 dark:bg-slate-800/60",children:[e.jsx(Q,{children:y("sipTrunkNumbers.colNumber")}),e.jsx(Q,{children:y("sipTrunkNumbers.colAgent")}),e.jsx(Q,{className:"text-right",children:y("common.actions")})]})}),e.jsx(gt,{children:I?e.jsx(xe,{children:e.jsx(W,{colSpan:3,children:e.jsx("div",{className:"flex items-center justify-center py-6",children:e.jsx(ae,{className:"h-5 w-5 animate-spin text-gray-400"})})})}):h.length===0?e.jsx(xe,{children:e.jsx(W,{colSpan:3,className:"py-6 text-center text-sm text-gray-500 dark:text-gray-400",children:y("sipTrunkNumbers.empty")})}):h.map(T=>e.jsxs(xe,{className:"border-b border-gray-50 dark:border-gray-900",children:[e.jsx(W,{className:"text-gray-700 dark:text-gray-300",children:e.jsx("code",{className:"text-xs bg-gray-100 dark:bg-slate-800 px-2 py-1 rounded",children:T.phone_number})}),e.jsx(W,{children:e.jsxs(be,{value:T.agent_id||"",onValueChange:p=>void A(T.id,p),disabled:F===T.id,children:[e.jsx(fe,{className:"w-[220px]",children:e.jsx(ye,{placeholder:y("sipTrunkNumbers.unassigned")})}),e.jsx(je,{children:d.length===0?e.jsx(J,{value:"__none",disabled:!0,children:y("sipTrunkNumbers.noAgents")}):d.map(p=>e.jsx(J,{value:p.id,children:p.name},p.id))})]})}),e.jsx(W,{className:"text-right",children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30",onClick:()=>void S(T.id),disabled:w===T.id,"aria-label":y("sipTrunkNumbers.remove"),children:w===T.id?e.jsx(ae,{className:"h-4 w-4 animate-spin"}):e.jsx(ss,{className:"h-4 w-4"})})})]},T.id))})]})}),e.jsxs("div",{className:"rounded-lg border border-gray-200 dark:border-gray-700 p-4 space-y-3",children:[e.jsx("p",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:y("sipTrunkNumbers.addTitle")}),e.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-start",children:[e.jsxs("div",{className:"flex-1 space-y-1",children:[e.jsx(re,{value:z,onChange:T=>{m(T.target.value),O&&B(null)},onBlur:()=>m(T=>T.trim()?vs(T.trim()):T),placeholder:y("sipTrunkNumbers.addPlaceholder"),className:O?"border-red-500 focus-visible:border-red-500 focus-visible:ring-red-500":""}),O?e.jsx("p",{className:"text-xs text-red-600",children:O}):null]}),e.jsxs(be,{value:j,onValueChange:g,children:[e.jsx(fe,{className:"sm:w-[220px]",children:e.jsx(ye,{placeholder:y("sipTrunkNumbers.selectAgent")})}),e.jsx(je,{children:d.length===0?e.jsx(J,{value:"__none",disabled:!0,children:y("sipTrunkNumbers.noAgents")}):d.map(T=>e.jsx(J,{value:T.id,children:T.name},T.id))})]}),e.jsxs(P,{onClick:H,disabled:D,className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium",children:[D?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(Yt,{className:"mr-2 h-4 w-4"}),y("sipTrunkNumbers.add")]})]})]})]})]})})}const ri={liveTraceEnabled:!1};function ai(s,t){return!s||typeof s!="object"?!1:s[t]===!0}function zr(){const s=Ha(t=>t.workspaceUser);return a.useMemo(()=>
3{if(!s)return ri;const t=s.features??null;return{liveTraceEnabled:ai(t,"live_trace_enabled")}},[s])}const ni=["udp","tcp","tls"],qr="voico.ai",hr={name:"",sip_username:"",sip_password:"",sip_realm:qr,transport:"udp",callback_url:"",transfer_ring_timeout_seconds:7,notes:""},ii=15e3,xr={loading:!1,data:null,error:null};function li({onRefreshPhoneNumbers:s}){const{t}=Re(),{toast:i}=We(),{liveTraceEnabled:l}=zr(),[d,r]=a.useState([]),[_,f]=a.useState([]),[C,y]=a.useState(!1),[u,h]=a.useState(!1),[v,I]=a.useState(!1),[x,D]=a.useState(!1),[E,w]=a.useState("settings"),[q,F]=a.useState(!1),[V,z]=a.useState(!1),[m,j]=a.useState(null),[g,O]=a.useState(!1),[B,M]=a.useState(null),[H,A]=a.useState(!1),[S,T]=a.useState(null),[p,X]=a.useState(null),[N,de]=a.useState({...hr}),[Y,ne]=a.useState({}),[L,ie]=a.useState({}),ve=a.useRef([]);ve.current=d.map(n=>n.id);const Z=a.useCallback(async()=>{try{y(!0);const n=await Me.list();r(n)}catch{i({title:t("toast.error"),description:t("inboundSip.loadFailed"),variant:"destructive"})}finally{y(!1)}},[t,i]),ue=a.useCallback(async()=>{try{const n=await Ct.getAgents();f(n.data)}catch{}},[]);a.useEffect(()=>{Z(),ue()},[Z,ue]);const ge=a.useCallback(async n=>{ie(b=>({...b,[n]:{...b[n]??xr,loading:!0,error:null}}));try{const b=await Me.getRegistrationStatus(n);ie(G=>({...G,[n]:{loading:!1,data:b,error:null}}))}catch(b){ie(G=>({...G,[n]:{loading:!1,data:G[n]?.data??null,error:b instanceof Error?b.message:"request failed"}}))}},[]);a.useEffect(()=>{let n=!1;const b=async()=>{const ce=ve.current;n||ce.length===0||await Promise.all(ce.map(te=>ge(te)))};b();const G=setInterval(()=>{b()},ii);return()=>{n=!0,clearInterval(G)}},[ge]),a.useEffect(()=>{ie(n=>{const b=new Set(d.map(ce=>ce.id)),G={};for(const ce of b)G[ce]=n[ce]??xr;return G});for(const n of d)L[n.id]||ge(n.id)},[d]);const Ae=()=>{de({...hr})},K=async()=>{if(!N.name.trim()){i({title:t("toast.error"),description:t("inboundSip.errNameRequired"),variant:"destructive"});return}if(!N.sip_username.trim()||!N.sip_realm.trim()){i({title:t("toast.error"),description:t("inboundSip.errSipFieldsRequired"),variant:"destructive"});return}if((N.sip_password||"").length<12){i({title:t("toast.error"),description:t("inboundSip.errPasswordLength"),variant:"destructive"});return}const n=N.transfer_ring_timeout_seconds??7;if(n<1||n>35){i({title:t("toast.error"),description:t("inboundSip.errTransferRingTimeoutRange"),variant:"destructive"});return}const b={name:N.name.trim(),sip_username:N.sip_username.trim(),sip_password:N.sip_password,sip_realm:N.sip_realm.trim(),transport:N.transport,callback_url:N.callback_url?.trim()||void 0,transfer_ring_timeout_seconds:N.transfer_ring_timeout_seconds??7,notes:N.notes?.trim()||void 0};try{h(!0),await Me.create(b),i({title:t("toast.success"),description:t("inboundSip.createSuccess")}),I(!1),Ae(),await Z(),s&&await s()}catch(G){i({title:t("toast.error"),description:G instanceof Error?G.message:t("inboundSip.createFailed"),variant:"destructive"})}finally{h(!1)}},ee=n=>{j(n),ne({name:n.name||"",sip_password:"",transport:n.transport||"udp",callback_url:n.callback_url||"",transfer_ring_timeout_seconds:n.transfer_ring_timeout_seconds??7,is_active:n.is_active,notes:n.notes||""}),w("settings"),D(!0)},le=async()=>{if(!m)return;const n={};if(Y.name!==void 0&&(n.name=Y.name?.trim()),Y.sip_password){if(Y.sip_password.length<12){i({title:t("toast.error"),description:t("inboundSip.errPasswordLength"),variant:"destructive"});return}n.sip_password=Y.sip_password}if(Y.transport&&(n.transport=Y.transport),Y.callback_url!==void 0&&(n.callback_url=Y.callback_url||null),Y.transfer_ring_timeout_seconds!==void 0&&Y.transfer_ring_timeout_seconds!==m.transfer_ring_timeout_seconds){const b=Y.transfer_ring_timeout_seconds;if(b<1||b>35){i({title:t("toast.error"),description:t("inboundSip.errTransferRingTimeoutRange"),variant:"destructive"});return}n.transfer_ring_timeout_seconds=Y.transfer_ring_timeout_seconds}Y.is_active!==void 0&&(n.is_active=Y.is_active),Y.notes!==void 0&&(n.notes=Y.notes||null);try{h(!0),await Me.update(m.id,n),i({title:t("toast.success"),description:t("inboundSip.updateSuccess")}),D(!1),j(null),await Z(),s&&await s()}catch(b){i({title:t("toast.error"),description:b instanceof Error?b.message:t("inboundSip.updateFailed"),variant:"destructive"})}finally{h(!1)}},Ge=async()=>
3{if(m)try{h(!0),await Me.delete(m.id),i({title:t("toast.success"),description:t("inboundSip.deleteSuccess")}),F(!1),j(null),await Z(),s&&await s()}catch(n){i({title:t("toast.error"),description:n instanceof Error?n.message:t("inboundSip.deleteFailed"),variant:"destructive"})}finally{h(!1)}},c=(n,b)=>{navigator.clipboard.writeText(n),X(b),setTimeout(()=>X(null),1500)},R=n=>n?_.find(G=>G.id===n)?.name||n:t("inboundSip.noAgentAssigned"),k=n=>{if(!n||n.loading&&!n.data)return e.jsxs(Ne,{className:"bg-gray-100 text-gray-500 border-gray-200",children:[e.jsx(ae,{className:"mr-1 h-3 w-3 animate-spin"}),t("inboundSip.regChecking")]});const b=n.data?.state??"edge_unreachable",G=(()=>{switch(b){case"registered":return t("inboundSip.regRegistered");case"unregistered":return t("inboundSip.regUnregistered");case"auth_failed":return t("inboundSip.regAuthFailed");case"not_provisioned":return t("inboundSip.regNotProvisioned");case"edge_unreachable":return t("inboundSip.regEdgeUnreachable");case"unknown":default:return t("inboundSip.regUnknown")}})(),ce=(()=>{switch(b){case"registered":return"bg-green-50 text-green-700 border-green-200";case"unregistered":return"bg-amber-50 text-amber-700 border-amber-200";case"auth_failed":return"bg-red-600 text-white border-red-700";case"not_provisioned":return"bg-blue-50 text-blue-700 border-blue-200";case"edge_unreachable":return"bg-red-50 text-red-700 border-red-200";case"unknown":default:return"bg-gray-100 text-gray-600 border-gray-200"}})(),te=n.error?n.error:n.data?.error?n.data.error:n.data?.checked_at?`${t("inboundSip.regCheckedAt")} ${new Date(n.data.checked_at).toLocaleTimeString()}`:null,oe=b==="auth_failed"?[t("inboundSip.regAuthFailedHint"),n.data?.last_failure_reason].filter(Boolean).join(` 4`):te,Tt=e.jsxs(Ne,{className:Ye(ce,"cursor-pointer transition-shadow hover:shadow-sm"),title:oe??void 0,children:[b==="auth_failed"?e.jsx(ks,{className:"mr-1 h-3 w-3 flex-none"}):null,G]});return e.jsxs(Ar,{children:[e.jsx(Dr,{asChild:!0,children:e.jsx("span",{className:"inline-flex",children:Tt})}),e.jsx(Fr,{className:"w-80",align:"start",children:e.jsx(Ur,{registration:n.data,endpointLabel:n.data?.endpoint_name??null,loading:n.loading,error:n.error})})]})},$=(n,b,G)=>{const ce=b;return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("inboundSip.fieldName")," *"]}),e.jsx(re,{value:ce.name||"",onChange:te=>G(oe=>({...oe,name:te.target.value})),placeholder:t("inboundSip.placeholderName")})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("inboundSip.fieldSipUsername")," *"]}),e.jsx(re,{value:n&&m?m.sip_username:ce.sip_username||"",disabled:n,onChange:te=>G(oe=>({...oe,sip_username:te.target.value})),placeholder:"voico-trunk-01"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("inboundSip.fieldSipRealm")," *"]}),e.jsx(re,{value:n&&m?m.sip_realm:ce.sip_realm||"",disabled:n,onChange:te=>G(oe=>({...oe,sip_realm:te.target.value})),placeholder:qr})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:n?t("inboundSip.fieldSipPasswordRotate"):`${t("inboundSip.fieldSipPassword")} *`}),e.jsx(re,{type:"password",value:ce.sip_password||"",onChange:te=>G(oe=>({...oe,sip_password:te.target.value})),placeholder:n?t("inboundSip.placeholderPasswordRotate"):""})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("inboundSip.fieldTransport")}),e.jsxs(be,{value:ce.transport||"udp",onValueChange:te=>G(oe=>({...oe,transport:te})),children:[e.jsx(fe,{children:e.jsx(ye,{})}),e.jsx(je,{children:ni.map(te=>e.jsx(J,{value:te,children:te.toUpperCase()},te))})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("inboundSip.fieldCallbackUrl")}),e.jsx(re,{value:ce.callback_url||"",onChange:te=>G(oe=>({...oe,callback_url:te.target.value})),placeholder:"https://customer.example.com/CallUpdate"}),e.jsx("p",{className:"text-xs text-gray-500",children:t("inboundSip.callbackHint")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("inboundSip.fieldTransferRingTimeout")}),e.jsx(re,{type:"number",min:1,max:35,value:ce.transfer_ring_timeout_seconds??7,onChange:te=>G(oe=>({...oe,transfer_ring_timeout_seconds:Number(te.target.value)||7}))}),e.jsx("p",{className:"text-xs text-gray-500",children:t("inboundSip.transferRingTimeoutHint")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("inboundSip.fieldNotes")}),e.jsx(zt,{value:ce.notes||"",onChange:te=>G(oe=>({...oe,notes:te.target.value})),rows:2})]}),n?e.jsxs("div",{className:"flex items-center justify-between rounded-lg border border-gray-200 dark:border-gray-700 px-4 py-3",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("inboundSip.fieldTrunkEnabled")}),e.jsx("p",{className:"text-xs text-gray-500 dark:text-gray-400",children:t("inboundSip.fieldTrunkEnabledHint")})]}),e.jsx(Vt,{checked:ce.is_active??!0,onCheckedChange:te=>G(oe=>({...oe,is_active:te}))})]}):null]})};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(ke,{className:"border border-gray-200 dark:border-gray-800",children:[e.jsx(ze,{className:"border-b border-gray-100 dark:border-gray-800",children:e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsxs("div",{children:[e.jsx(qe,{className:"text-lg font-medium text-black dark:text-gray-300",children:t("inboundSip.title")}),e.jsx(ct,{className:"text-gray-600 dark:text-gray-400",children:t("inboundSip.description")})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(P,{variant:"outline",onClick:()=>z(!0),className:"border-gray-200",children:[e.jsx(Or,{className:"mr-2 h-4 w-4"}),t("inboundSip.howToConnect")]}),e.jsxs(P,{onClick:()=>I(!0),className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium shadow-sm",children:[e.jsx(Yt,{className:"mr-2 h-4 w-4"}),t("inboundSip.import")]})]})]})}),e.jsx($e,{className:"pt-6",children:C?e.jsx("div",{className:"flex items-center justify-center py-10",children:e.jsx(ae,{className:"h-6 w-6 animate-spin text-gray-400"})}):d.length===0?e.jsx("div",{className:"py-8 text-center text-sm text-gray-500 dark:text-gray-400",children:t("inboundSip.empty")}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(xt,{children:[e.jsx(pt,{children:e.jsxs(xe,{className:"bg-gray-50 dark:bg-slate-800/60",children:[e.jsx(Q,{children:t("inboundSip.colName")}),e.jsx(Q,{children:t("inboundSip.colPhoneNumber")}),e.jsx(Q,{children:t("inboundSip.colSipIdentity")}),e.jsx(Q,{children:t("inboundSip.colTransport")}),e.jsx(Q,{children:t("inboundSip.colAgent")}),e.jsx(Q,{children:t("inboundSip.colStatus")}),e.jsx(Q,{children:t("inboundSip.colRegistration")}),e.jsx(Q,{className:"text-right",children:t("common.actions")})]})}),e.jsx(gt,{children:d.map(n=>e.jsxs(xe,{className:"border-b border-gray-50 dark:border-gray-900",children:[e.jsx(W,{className:"font-medium text-gray-900 dark:text-gray-100",children:n.name||t("inboundSip.unnamedTrunk")}),e.jsx(W,{className:"text-gray-700 dark:text-gray-300",children:n.phone_number?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("code",{className:"text-xs bg-gray-100 dark:bg-slate-800 px-2 py-1 rounded",children:n.phone_number}),e.jsx(P,{variant:"ghost",size:"icon",className:"h-7 w-7",onClick:()=>c(n.phone_number||"",n.id),children:p===n.id?e.jsx(dn,{className:"h-3.5 w-3.5 text-green-600"}):e.jsx(un,{className:"h-3.5 w-3.5 text-gray-400"})})]}):t("inboundSip.noPhoneNumber")}),e.jsx(W,{className:"text-gray-700 dark:text-gray-300 max-w-[260px] truncate",children:e.jsx("code",{className:"text-xs bg-gray-100 dark:bg-slate-800 px-2 py-1 rounded",children:n.sip_username.includes("@")?n.sip_username:`${n.sip_username}@${n.sip_realm}`})}),e.jsx(W,{children:e.jsx(Ne,{className:"bg-gray-100 dark:bg-slate-800 text-gray-700 dark:text-gray-300 border-gray-200 text-xs uppercase",children:n.transport||"default"})}),e.jsx(W,{className:"text-gray-700 dark:text-gray-300",children:R(n.agent_id)}),e.jsx(W,{children:n.is_active?e.jsx(Ne,{className:"bg-green-50 text-green-700 border-green-200",children:t("inboundSip.statusActive")}
4):e.jsx(Ne,{className:"bg-gray-100 text-gray-600 border-gray-200",children:t("inboundSip.statusDisabled")})}),e.jsx(W,{children:k(L[n.id])}),e.jsx(W,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(dt,{delayDuration:200,children:e.jsxs(ut,{children:[e.jsx(mt,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>void ge(n.id),disabled:L[n.id]?.loading,"aria-label":t("inboundSip.testNow"),children:e.jsx(As,{className:"h-4 w-4 "+(L[n.id]?.loading?"animate-spin":"")})})}),e.jsx(ht,{children:t("inboundSip.testNow")})]})}),l?e.jsx(dt,{delayDuration:200,children:e.jsxs(ut,{children:[e.jsx(mt,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>{M(n),O(!0)},"aria-label":t("liveTrace.openButton"),children:e.jsx(Pr,{className:"h-4 w-4"})})}),e.jsx(ht,{children:t("liveTrace.openButton")})]})}):null,e.jsx(dt,{delayDuration:200,children:e.jsxs(ut,{children:[e.jsx(mt,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>{T(n),A(!0)},"aria-label":t("sipTrunkNumbers.manageButton"),children:e.jsx(Er,{className:"h-4 w-4"})})}),e.jsx(ht,{children:t("sipTrunkNumbers.manageButton")})]})}),e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>ee(n),children:e.jsx(Rr,{className:"h-4 w-4"})}),e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30",onClick:()=>{j(n),F(!0)},children:e.jsx(ss,{className:"h-4 w-4"})})]})})]},n.id))})]})})})]}),e.jsx(we,{open:v,onOpenChange:n=>{I(n),n||Ae()},children:e.jsxs(Ce,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto",onPointerDownOutside:n=>n.preventDefault(),onInteractOutside:n=>n.preventDefault(),children:[e.jsxs(Te,{children:[e.jsx(Pe,{className:"text-xl font-semibold text-gray-900 dark:text-white",children:t("inboundSip.import")}),e.jsx(Oe,{children:t("inboundSip.importHelp")})]}),$(!1,N,n=>de(b=>n(b))),e.jsxs(Ee,{className:"mt-4",children:[e.jsx(P,{variant:"outline",onClick:()=>I(!1),disabled:u,children:t("common.cancel")}),e.jsxs(P,{onClick:K,disabled:u,className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium",children:[u?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("inboundSip.import")]})]})]})}),e.jsx(we,{open:x,onOpenChange:n=>{D(n),n||(j(null),ne({}),w("settings"))},children:e.jsxs(Ce,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto",onPointerDownOutside:n=>n.preventDefault(),onInteractOutside:n=>n.preventDefault(),children:[e.jsxs(Te,{children:[e.jsx(Pe,{className:"text-xl font-semibold text-gray-900 dark:text-white",children:t("inboundSip.editTitle")}),e.jsx(Oe,{children:t("inboundSip.editHelp")})]}),e.jsxs(Cs,{value:E,onValueChange:n=>w(n),className:"mt-2",children:[e.jsxs(Ts,{children:[e.jsx(He,{value:"settings",children:t("regTimeline.tabSettings")}),e.jsx(He,{value:"activity",children:t("regTimeline.tabActivity")})]}),e.jsxs(Ve,{value:"settings",className:"mt-3",children:[$(!0,Y,n=>ne(b=>n(b))),e.jsxs(Ee,{className:"mt-4",children:[e.jsx(P,{variant:"outline",onClick:()=>D(!1),disabled:u,children:t("common.cancel")}),e.jsxs(P,{onClick:le,disabled:u,className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium",children:[u?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("common.save")]})]})]}),e.jsx(Ve,{value:"activity",className:"mt-3",children:m?e.jsx(Br,{enabled:E==="activity",fetcher:n=>Me.getRegistrationEvents(m.id,n)},`inbound-${m.id}`):null})]})]})}),e.jsx(we,{open:q,onOpenChange:F,children:e.jsxs(Ce,{className:"sm:max-w-md",children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:t("inboundSip.deleteTitle")}),e.jsx(Oe,{children:t("inboundSip.deleteConfirm")})]}),e.jsxs(Ee,{children:[e.jsx(P,{variant:"outline",onClick:()=>F(!1),disabled:u,children:t("common.cancel")}),e.jsxs(P,{variant:"destructive",onClick:Ge,disabled:u,children:[u?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("common.delete")]})]})]})}),e.jsx(we,{open:V,onOpenChange:z,children:e.jsxs(Ce,{className:"sm:max-w-lg",children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:t("inboundSip.howToConnect")}),e.jsx(Oe,{children:t("inboundSip.helpIntro")})]}),e.jsxs("div",{className:"space-y-3 text-sm text-gray-700 dark:text-gray-300",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium mb-1",children:t("inboundSip.helpStep1")}),e.jsx("p",{children:t("inboundSip.helpStep1Detail")})]}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium mb-1",children:t("inboundSip.helpStep2")}),e.jsx("p",{children:t("inboundSip.helpStep2Detail")}),e.jsxs("ul",{className:"list-disc list-inside mt-1 space-y-1",children:[e.jsxs("li",{children:[e.jsx("span",{className:"font-medium",children:"SIP server:"})," ",e.jsx("code",{className:"text-xs bg-gray-100 dark:bg-slate-800 px-1 rounded",children:"3.124.206.245"})," ",e.jsx("span",{className:"text-xs text-gray-500 dark:text-slate-400",children:"(port 5060)"})]}),e.jsxs("li",{children:[e.jsxs("span",{className:"font-medium",children:[t("inboundSip.helpUsern
4ame"),":"]})," ",t("inboundSip.helpUsernameValue")]}),e.jsxs("li",{children:[e.jsxs("span",{className:"font-medium",children:[t("inboundSip.helpRealm"),":"]})," ",t("inboundSip.helpRealmValue")]}),e.jsxs("li",{children:[e.jsxs("span",{className:"font-medium",children:[t("inboundSip.helpTransport"),":"]})," ","UDP / TCP"]})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium mb-1",children:t("inboundSip.helpStep3")}),e.jsx("p",{children:t("inboundSip.helpStep3Detail")})]})]}),e.jsx(Ee,{children:e.jsx(P,{onClick:()=>z(!1),children:t("common.close")||"Close"})})]})}),l&&B?e.jsx(Hr,{open:g,onOpenChange:n=>{O(n),n||M(null)},trunkId:B.id,trunkLabel:B.name,starter:(n,b)=>Me.startLiveTrace(n,b),fetcher:(n,b)=>Me.getLiveTrace(n,b),downloader:(n,b)=>Me.downloadLiveTracePcap(n,b)}):null,S?e.jsx(Vr,{open:H,onOpenChange:n=>{A(n),n||T(null)},trunkId:S.id,trunkLabel:S.name,agents:_,fetcher:n=>Me.listNumbers(n),adder:(n,b)=>Me.addNumber(n,b),remover:(n,b)=>Me.removeNumber(n,b),onChanged:s}):null]})}const oi=["udp","tcp","tls"],pr={name:"",provider_host:"",provider_port:5060,transport:"udp",aor_host:"",outbound_proxy:"",username:"",password:"",contact_user:"",from_user:"",from_domain:"",match_hosts:"",transfer_ring_timeout_seconds:7,tls_verify_server:!0,tls_server_name:"",notes:""},ci=5061,di=/[\s\x00-\x1F\x7F]/;function Nt(s,t){return s&&di.test(s)?t("outboundSip.errUsernameWhitespace"):null}const ui=15e3,gr={loading:!1,data:null,error:null};function mi({onRefreshPhoneNumbers:s}={}){const{t}=Re(),{toast:i}=We(),{liveTraceEnabled:l}=zr(),[d,r]=a.useState([]),[_,f]=a.useState([]),[C,y]=a.useState(!1),[u,h]=a.useState(!1),[v,I]=a.useState(!1),[x,D]=a.useState(!1),[E,w]=a.useState("settings"),[q,F]=a.useState(!1),[V,z]=a.useState(!1),[m,j]=a.useState(null),[g,O]=a.useState(!1),[B,M]=a.useState(null),[H,A]=a.useState(!1),[S,T]=a.useState(null),[p,X]=a.useState({...pr}),[N,de]=a.useState({}),[Y,ne]=a.useState({}),L=a.useRef([]);L.current=d.map(c=>c.id);const ie=a.useCallback(async()=>{try{y(!0);const c=await Le.list();r(c)}catch{i({title:t("toast.error"),description:t("outboundSip.loadFailed"),variant:"destructive"})}finally{y(!1)}},[t,i]),ve=a.useCallback(async()=>{try{const c=await Ct.getAgents();f(c.data)}catch{}},[]);a.useEffect(()=>{ie(),ve()},[ie,ve]);const Z=a.useCallback(async c=>{ne(R=>({...R,[c]:{...R[c]??gr,loading:!0,error:null}}));try{const R=await Le.getRegistrationStatus(c);ne(k=>({...k,[c]:{loading:!1,data:R,error:null}}))}catch(R){ne(k=>({...k,[c]:{loading:!1,data:k[c]?.data??null,error:R instanceof Error?R.message:"request failed"}}))}},[]);a.useEffect(()=>{let c=!1;const R=async()=>{const $=L.current;c||$.length===0||await Promise.all($.map(n=>Z(n)))};R();const k=setInterval(()=>{R()},ui);return()=>{c=!0,clearInterval(k)}},[Z]),a.useEffect(()=>{ne(c=>{const R=new Set(d.map($=>$.id)),k={};for(const $ of R)k[$]=c[$]??gr;return k});for(const c of d)Y[c.id]||Z(c.id)},[d]);const ue=()=>X({...pr}),ge=async()=>{if(!p.name.trim()){i({title:t("toast.error"),description:t("outboundSip.errNameRequired"),variant:"destructive"});return}if(!p.provider_host.trim()){i({title:t("toast.error"),description:t("outboundSip.errProviderHostRequired"),variant:"destructive"});return}if(!p.username.trim()){i({title:t("toast.error"),description:t("outboundSip.errUsernameRequired"),variant:"destructive"});return}const c=Nt(p.username,t);if(c){i({title:t("toast.error"),description:c,variant:"destructive"});return}if(!(p.password||"").trim()){i({title:t("toast.error"),description:t("outboundSip.errPasswordRequired"),variant:"destructive"});return}if(p.password&&p.password!==p.password.trim()){i({title:t("toast.error"),description:t("outboundSip.errPasswordWhitespace"),variant:"destructive"});return}if(!p.contact_user.trim()){i({title:t("toast.error"),description:t("outboundSip.errContactUserRequired"),variant:"destructive"});return}const R=p.transfer_ring_timeout_seconds??7;if(R<1||R>35){i({title:t("toast.error"),description:t("outboundSip.errTransferRingTimeoutRange"),variant:"destructive"});return}const k={name:p.name.trim(),provider_host:p.provider_host.trim(),provider_port:p.provider_port??5060,transport:p.transport,aor_host:p.aor_host?.trim()||void 0,outbound_proxy:p.outbound_proxy?.trim()||void 0,username:p.username.trim(),password:p.password,contact_user:p.contact_user.trim(),from_user:p.from_user?.trim()||void 0,from_domain:p.from_domain?.trim()||void 0,match_hosts:p.match_hosts?.trim()||void 0,transfer_ring_timeout_seconds:p.transfer_ring_timeout_seconds??7,notes:p.notes?.trim()||void 0};if(p.transport==="tls"){k.tls_verify_server=p.tls_verify_server??!0;const $=p.tls_server_name?.trim();$&&(k.tls_server_name=$)}try{h(!0),await Le.create(k),i({title:t("toast.success"),description:t("outboundSip.createSuccess")}),I(!1),ue(),await ie(),s&&await s()}catch($){i({title:t("toast.error"),description:$ instanceof Error?$.message:t("outboundSip.createFailed"),variant:"destructive"})}finally{h(!1)}},Ae=c=>{j(c),de({name:c.name,provider_host:c.provider_host,provider_port:c.provider_port,transport:c.transport,aor_host:c.aor_host,outbound_proxy:c.outbound_proxy||"",username:c.username,password:"",contact_user:c.contact_user,from_user:c.from_user||"",from_domain:c.from_domain||"",match_hosts:c.match_hosts,transfer_ring_timeout_seconds:c.transfer_ring_timeout_seconds,tls_verify_server:c.tls_verify_server??!0,tls_server_name:c.tls_server_name||"",is_active:c.is_active,notes:c.notes||""}),w("settings"),D(!0)},K=async()=>
4{if(!m)return;const c={};if(N.name!==void 0&&N.name!==m.name&&(c.name=N.name?.trim()),N.provider_host!==void 0&&N.provider_host!==m.provider_host&&(c.provider_host=N.provider_host?.trim()),N.provider_port!==void 0&&N.provider_port!==m.provider_port&&(c.provider_port=N.provider_port),N.transport!==void 0&&N.transport!==m.transport&&(c.transport=N.transport),N.aor_host!==void 0&&N.aor_host!==m.aor_host&&(c.aor_host=N.aor_host?.trim()),N.outbound_proxy!==void 0){const k=N.outbound_proxy?.trim()||null;k!==(m.outbound_proxy??null)&&(c.outbound_proxy=k)}if(N.username!==void 0&&N.username!==m.username){const k=Nt(N.username,t);if(k){i({title:t("toast.error"),description:k,variant:"destructive"});return}c.username=N.username?.trim()}if(N.password){if(N.password!==N.password.trim()){i({title:t("toast.error"),description:t("outboundSip.errPasswordWhitespace"),variant:"destructive"});return}c.password=N.password}if(N.contact_user!==void 0&&N.contact_user!==m.contact_user&&(c.contact_user=N.contact_user?.trim()),N.from_user!==void 0){const k=N.from_user?.trim()||null;k!==(m.from_user??null)&&(c.from_user=k)}if(N.from_domain!==void 0){const k=N.from_domain?.trim()||null;k!==(m.from_domain??null)&&(c.from_domain=k)}if(N.match_hosts!==void 0&&N.match_hosts!==m.match_hosts&&(c.match_hosts=N.match_hosts?.trim()),N.transfer_ring_timeout_seconds!==void 0&&N.transfer_ring_timeout_seconds!==m.transfer_ring_timeout_seconds){const k=N.transfer_ring_timeout_seconds;if(k<1||k>35){i({title:t("toast.error"),description:t("outboundSip.errTransferRingTimeoutRange"),variant:"destructive"});return}c.transfer_ring_timeout_seconds=N.transfer_ring_timeout_seconds}if((N.transport??m.transport)==="tls"&&(N.tls_verify_server!==void 0&&N.tls_verify_server!==m.tls_verify_server&&(c.tls_verify_server=N.tls_verify_server),N.tls_server_name!==void 0)){const k=N.tls_server_name?.trim()||null;k!==(m.tls_server_name??null)&&(c.tls_server_name=k)}N.is_active!==void 0&&N.is_active!==m.is_active&&(c.is_active=N.is_active),N.notes!==void 0&&(c.notes=N.notes||null);try{h(!0),await Le.update(m.id,c),i({title:t("toast.success"),description:t("outboundSip.updateSuccess")}),D(!1),j(null),await ie(),s&&await s()}catch(k){i({title:t("toast.error"),description:k instanceof Error?k.message:t("outboundSip.updateFailed"),variant:"destructive"})}finally{h(!1)}},ee=async()=>{if(m)try{h(!0),await Le.delete(m.id),i({title:t("toast.success"),description:t("outboundSip.deleteSuccess")}),F(!1),j(null),await ie(),s&&await s()}catch(c){i({title:t("toast.error"),description:c instanceof Error?c.message:t("outboundSip.deleteFailed"),variant:"destructive"})}finally{h(!1)}},le=c=>{if(!c||c.loading&&!c.data)return e.jsxs(Ne,{className:"bg-gray-100 text-gray-500 border-gray-200",children:[e.jsx(ae,{className:"mr-1 h-3 w-3 animate-spin"}),t("outboundSip.regChecking")]});const R=c.data?.state??"edge_unreachable",k=(()=>{switch(R){case"registered":return t("outboundSip.regRegistered");case"unregistered":return t("outboundSip.regUnregistered");case"auth_failed":return t("outboundSip.regAuthFailed");case"not_provisioned":return t("outboundSip.regNotProvisioned");case"edge_unreachable":return t("outboundSip.regEdgeUnreachable");case"unknown":default:return t("outboundSip.regUnknown")}})(),$=(()=>{switch(R){case"registered":return"bg-green-50 text-green-700 border-green-200";case"unregistered":return"bg-amber-50 text-amber-700 border-amber-200";case"auth_failed":return"bg-red-600 text-white border-red-700";case"not_provisioned":return"bg-blue-50 text-blue-700 border-blue-200";case"edge_unreachable":return"bg-red-50 text-red-700 border-red-200";case"unknown":default:return"bg-gray-100 text-gray-600 border-gray-200"}})(),n=c.error?c.error:c.data?.error?c.data.error:c.data?.checked_at?`${t("outboundSip.regCheckedAt")} ${new Date(c.data.checked_at).toLocaleTimeString()}`:null,b=R==="auth_failed"?[t("outboundSip.regAuthFailedHint"),c.data?.last_failure_reason].filter(Boolean).join(` 5`):n,G=e.jsxs(Ne,{className:Ye($,"cursor-pointer transition-shadow hover:shadow-sm"),title:b??void 0,children:[R==="auth_failed"?e.jsx(ks,{className:"mr-1 h-3 w-3 flex-none"}):null,k]});return e.jsxs(Ar,{children:[e.jsx(Dr,{asChild:!0,children:e.jsx("span",{className:"inline-flex",children:G})}),e.jsx(Fr,{className:"w-80",align:"start",children:e.jsx(Ur,{registration:c.data,endpointLabel:c.data?.endpoint_name??null,loading:c.loading,error:c.error})})]})},Ge=(c,R,k)=>{const $=R;return e.jsxs("div",{className:"space-y-4 max-h-[60vh] overflow-y-auto pr-1",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("outboundSip.fieldName")," *"]}),e.jsx(re,{value:$.name||"",onChange:n=>k(b=>({...b,name:n.target.value})),placeholder:t("outboundSip.placeholderName")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldTransport")}),e.jsxs(be,{value:$.transport||"udp",onValueChange:n=>k(b=>{const G=n,ce=G==="tls"&&(b.provider_port??5060)===5060;return{...b,transport:G,...ce?{provider_port:ci}:{}}}),children:[e.jsx(fe,{children:e.jsx(ye,{})}),e.jsx(je,{children:oi.map(n=>e.jsx(J,{value:n,children:n.toUpperCase()},n))})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("outboundSip.fieldProviderHost")," *"]}),e.jsx(re,{value:$.provider_host||"",onChange:n=>k(b=>({...b,provider_host:n.target.value})),placeholder:"sbc.example.com"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldProviderPort")}),e.jsx(re,{type:"number",min:1,max:65535,value:$.provider_port??5060,onChange:n=>k(b=>({...b,provider_port:Number(n.target.value)||5060}))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldAorHost")}),e.jsx(re,{value:$.aor_host||"",onChange:n=>k(b=>({...b,aor_host:n.target.value})),placeholder:t("outboundSip.placeholderAorHost")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldOutboundProxy")}),e.jsx(re,{value:$.outbound_proxy||"",onChange:n=>k(b=>({...b,outbound_proxy:n.target.value})),placeholder:"host:port"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("outboundSip.fieldUsername")," *"]}),e.jsx(re,{value:$.username||"",onChange:n=>k(b=>({...b,username:n.target.value})),placeholder:"voico-trunk-01","aria-invalid":Nt($.username,t)!==null?!0:void 0}),Nt($.username,t)&&e.jsx("p",{className:"text-xs text-red-600 dark:text-red-400",children:Nt($.username,t)}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.usernameHint")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:c?t("outboundSip.fieldPasswordRotate"):`${t("outboundSip.fieldPassword")} *`}),e.jsx(re,{type:"password",value:$.password||"",onChange:n=>k(b=>({...b,password:n.target.value})),placeholder:c?t("outboundSip.placeholderPasswordRotate"):"","aria-invalid":$.password&&$.password!==$.password.trim()?!0:void 0}),$.password&&$.password!==$.password.trim()&&e.jsx("p",{className:"text-xs text-red-600 dark:text-red-400",children:t("outboundSip.errPasswordWhitespace")})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:[t("outboundSip.fieldContactUser")," *"]}),e.jsx(re,{value:$.contact_user||"",onChange:n=>k(b=>({...b,contact_user:n.target.value})),placeholder:"+15551234567"}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.contactUserHint")})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldFromUser")}),e.jsx(re,{value:$.from_user||"",onChange:n=>k(b=>({...b,from_user:n.target.value})),placeholder:t("outboundSip.placeholderFromUser")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldFromDomain")}),e.jsx(re,{value:$.from_domain||"",onChange:n=>k(b=>({...b,from_domain:n.target.value})),placeholder:t("outboundSip.placeholderFromDomain")})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldMatchHosts")}),e.jsx(re,{value:$.match_hosts||"",onChange:n=>k(b=>({...b,match_hosts:n.target.value})),placeholder:t("outboundSip.placeholderMatchHosts")}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.matchHostsHint")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldTransferRingTimeout")}),e.jsx(re,{type:"number",min:1,max:35,value:$.transfer_ring_timeout_seconds??7,onChange:n=>k(b=>({...b,transfer_ring_timeout_seconds:Number(n.target.value)||7}))}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.transferRingTimeoutHint")})]}),$.transport==="tls"?e.jsxs("div",{className:"space-y-4 rounded-lg border border-gray-200 dark:border-gray-700 p-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.tlsSectionTitle")}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.tlsSectionHint")})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"space-y-0.5 pr-4",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldTlsVerifyServer")}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.tlsVerifyServerHint")})]}),e.jsx(Vt,{checked:$.tls_verify_server??!0,onCheckedChange:n=>k(b=>({...b,tls_verify_server:n}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldTlsServerName")}),e.jsx(re,{value:$.tls_server_name||"",onChange:n=>k(b=>({...b,tls_server_name:n.target.value})),placeholder:$.aor_host||$.provider_host||""}),e.jsx("p",{className:"text-xs text-gray-500",children:t("outboundSip.tlsServerNameHint")})]})]}):null,c?e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldEnabled")}),e.jsxs(be,{value:$.is_active?"true":"false",onValueChange:n=>k(b=>({...b,is_active:n==="true"})),children:[e.jsx(fe,{children:e.jsx(ye,{})}),e.jsxs(je,{children:[e.jsx(J,{value:"true",children:t("outboundSip.statusActive")}
5),e.jsx(J,{value:"false",children:t("outboundSip.statusDisabled")})]})]})]}):null,e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("outboundSip.fieldNotes")}),e.jsx(zt,{value:$.notes||"",onChange:n=>k(b=>({...b,notes:n.target.value})),rows:2})]})]})};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(ke,{className:"border border-gray-200 dark:border-gray-800",children:[e.jsx(ze,{className:"border-b border-gray-100 dark:border-gray-800",children:e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsxs("div",{children:[e.jsx(qe,{className:"text-lg font-medium text-black dark:text-gray-300",children:t("outboundSip.title")}),e.jsx(ct,{className:"text-gray-600 dark:text-gray-400",children:t("outboundSip.description")})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(P,{variant:"outline",onClick:()=>z(!0),className:"border-gray-200",children:[e.jsx(Or,{className:"mr-2 h-4 w-4"}),t("outboundSip.howToConnect")]}),e.jsxs(P,{onClick:()=>I(!0),className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium shadow-sm",children:[e.jsx(Yt,{className:"mr-2 h-4 w-4"}),t("outboundSip.add")]})]})]})}),e.jsx($e,{className:"pt-6",children:C?e.jsx("div",{className:"flex items-center justify-center py-10",children:e.jsx(ae,{className:"h-6 w-6 animate-spin text-gray-400"})}):d.length===0?e.jsx("div",{className:"py-8 text-center text-sm text-gray-500 dark:text-gray-400",children:t("outboundSip.empty")}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(xt,{children:[e.jsx(pt,{children:e.jsxs(xe,{className:"bg-gray-50 dark:bg-slate-800/60",children:[e.jsx(Q,{children:t("outboundSip.colName")}),e.jsx(Q,{children:t("outboundSip.colProvider")}),e.jsx(Q,{children:t("outboundSip.colContactUser")}),e.jsx(Q,{children:t("outboundSip.colTransport")}),e.jsx(Q,{children:t("outboundSip.colStatus")}),e.jsx(Q,{children:t("outboundSip.colRegistration")}),e.jsx(Q,{className:"text-right",children:t("common.actions")})]})}),e.jsx(gt,{children:d.map(c=>e.jsxs(xe,{className:"border-b border-gray-50 dark:border-gray-900",children:[e.jsx(W,{className:"font-medium text-gray-900 dark:text-gray-100",children:c.name}),e.jsx(W,{className:"text-gray-700 dark:text-gray-300 max-w-[260px] truncate",children:e.jsxs("code",{className:"text-xs bg-gray-100 dark:bg-slate-800 px-2 py-1 rounded",children:[c.provider_host,":",c.provider_port]})}),e.jsx(W,{className:"text-gray-700 dark:text-gray-300",children:e.jsx("code",{className:"text-xs bg-gray-100 dark:bg-slate-800 px-2 py-1 rounded",children:c.contact_user})}),e.jsx(W,{children:e.jsx(Ne,{className:"bg-gray-100 dark:bg-slate-800 text-gray-700 dark:text-gray-300 border-gray-200 text-xs uppercase",children:c.transport})}),e.jsx(W,{children:c.is_active?e.jsx(Ne,{className:"bg-green-50 text-green-700 border-green-200",children:t("outboundSip.statusActive")}):e.jsx(Ne,{className:"bg-gray-100 text-gray-600 border-gray-200",children:t("outboundSip.statusDisabled")})}),e.jsx(W,{children:le(Y[c.id])}),e.jsx(W,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(dt,{delayDuration:200,children:e.jsxs(ut,{children:[e.jsx(mt,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>void Z(c.id),disabled:Y[c.id]?.loading,"aria-label":t("outboundSip.testNow"),children:e.jsx(As,{className:"h-4 w-4 "+(Y[c.id]?.loading?"animate-spin":"")})})}),e.jsx(ht,{children:t("outboundSip.testNow")})]})}),l?e.jsx(dt,{delayDuration:200,children:e.jsxs(ut,{children:[e.jsx(mt,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>{M(c),O(!0)},"aria-label":t("liveTrace.openButton"),children:e.jsx(Pr,{className:"h-4 w-4"})})}),e.jsx(ht,{children:t("liveTrace.openButton")})]})}):null,e.jsx(dt,{delayDuration:200,children:e.jsxs(ut,{children:[e.jsx(mt,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>{T(c),A(!0)},"aria-label":t("sipTrunkNumbers.manageButton"),children:e.jsx(Er,{className:"h-4 w-4"})})}),e.jsx(ht,{children:t("sipTrunkNumbers.manageButton")})]})}),e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-slate-700",onClick:()=>Ae(c),children:e.jsx(Rr,{className:"h-4 w-4"})}),e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8 text-gray-400 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30",onClick:()=>{j(c),F(!0)},children:e.jsx(ss,{className:"h-4 w-4"})})]})})]},c.id))})]})})})]}),e.jsx(we,{open:v,onOpenChange:c=>{I(c),c||ue()},children:e.jsxs(Ce,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto",onPointerDownOutside:c=>c.preventDefault(),onInteractOutside:c=>c.preventDefault(),children:[e.jsxs(Te,{children:[e.jsx(Pe,{className:"text-xl font-semibold text-gray-900 dark:text-white",children:t("outboundSip.add")}),e.jsx(Oe,{children:t("outboundSip.addHelp")})]}),Ge(!1,p,c=>X(R=>c(R))),e.jsxs(Ee,{className:"mt-4",children:[e.jsx(P,{variant:"outline",onClick:()=>I(!1),disabled:u,children:t("common.cancel")}),e.jsxs(P,{onClick:ge,disabled:u,className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium",children:[u?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("outboundSip.add")]})]})]})}),e.jsx(we,{open:x,onOpenChange:c=>{D(c),c||(j(null),de({}),w("settings"))},children:e.jsxs(Ce,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto",onPointerDownOutside:c=>c.preventDefault(),onInteractOutside:c=>c.preventDefault(),children:[e.jsxs(Te,{children:[e.jsx(Pe,{className:"text-xl font-semibold text-gray-900 dark:text-white",children:t("outboundSip.editTitle")}),e.jsx(Oe,{children:t("outboundSip.editHelp")})]}),e.jsxs(Cs,{value:E,onValueChange:c=>w(c),className:"mt-2",children:[e.jsxs(Ts,{children:[e.jsx(He,{value:"settings",children:t("regTimeline.tabSettings")}),e.jsx(He,{value:"activity",children:t("regTimeline.tabActivity")})]}),e.jsxs(Ve,{value:"settings",className:"mt-3",children:[Ge(!0,N,c=>de(R=>c(R))),e.jsxs(Ee,{className:"mt-4",children:[e.jsx(P,{variant:"outline",onClick:()=>D(!1),disabled:u,children:t("common.cancel")}),e.jsxs(P,{onClick:K,disabled:u,className:"bg-[#FDDF5C] hover:bg-[#FDDF5C]/90 text-black font-medium",children:[u?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("common.save")]})]})]}),e.jsx(Ve,{value:"activity",className:"mt-3",children:m?e.jsx(Br,{enabled:E==="activity",fetcher:c=>Le.getRegistrationEvents(m.id,c)},`outbound-${m.id}`):null})]})]})}),e.jsx(we,{open:q,onOpenChange:F,children:e.jsxs(Ce,{className:"sm:max-w-md",children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:t("outboundSip.deleteTitle")}),e.jsx(Oe,{children:t("outboundSip.deleteConfirm")})]}),e.jsxs(Ee,{children:[e.jsx(P,{variant:"outline",onClick:()=>F(!1),disabled:u,children:t("common.cancel")}),e.jsxs(P,{variant:"destructive",onClick:ee,disabled:u,children:[u?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("common.delete")]})]})]})}),e.jsx(we,{open:V,onOpenChange:z,children:e.jsxs(Ce,{className:"sm:max-w-lg",children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:t("outboundSip.howToConnect")}),e.jsx(Oe,{children:t("outboundSip.helpIntro")})]}),e.jsxs("div",{className:"space-y-3 text-sm text-gray-700 dark:text-gray-300",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium mb-1",children:t("outboundSip.helpStep1")}),e.jsx("p",{children:t("outboundSip.helpStep1Detail")})]}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium mb-1",children:t("outboundSip.helpStep2")}),e.jsx("p",{children:t("outboundSip.helpStep2Detail")})]}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium mb-1",children:t("outboundSip.helpStep3")}),e.jsx("p",{children:t("outboundSip.helpStep3Detail")})]})]}),e.jsx(Ee,{children:e.jsx(P,{onClick:()=>z(!1),children:t("common.close")||"Close"})})]})}),l&&B?e.jsx(Hr,{open:g,onOpenChange:c=>{O(c),c||M(null)},trunkId:B.id,trunkLabel:B.name,starter:(c,R)=>Le.startLiveTrace(c,R),fetcher:(c,R)=>Le.getLiveTrace(c,R),downloader:(c,R)=>Le.downloadLiveTracePcap(c,R)}):null,S?e.jsx(Vr,{open:H,onOpenChange:c=>{A(c),c||T(null)},trunkId:S.id,trunkLabel:S.name,agents:_,fetcher:c=>Le.listNumbers(c),adder:(c,R)=>Le.addNumber(c,R),remover:(c,R)=>Le.removeNumber(c,R),onChanged:s}):null]})}const hi=3e3,Ot="none",Rt=e.jsx("strong",{className:"font-semibold text-gray-900 dark:text-gray-200"});function xi({open:s,onOpenChange:t,agents:i,target:l,onChanged:d}){const{t:r,i18n:_}=Re(),{toast:f}
5=We(),[C,y]=a.useState("DE"),[u,h]=a.useState(""),[v,I]=a.useState(Ot),[x,D]=a.useState(!1),[E,w]=a.useState(null),[q,F]=a.useState(!1),V=a.useRef(!1),z=a.useRef(d);a.useEffect(()=>{z.current=d},[d]);const m=a.useMemo(()=>i.filter(L=>L.outbound_call_enabled),[i]),j=a.useMemo(()=>{try{return new Intl.DisplayNames([_.language||"en"],{type:"region"})}catch{return null}},[_.language]),g=tn(C,u),O=!!(l&&!E),B=E?.status,M=E?Ze(E.phone_number)||E.phone_number:l?Ze(l.phone_number)||l.phone_number:"";a.useEffect(()=>{s&&(w(null),D(!1),F(!1),h(""),y("DE"),I(l?.agent_id||Ot),l?.verification_status==="pending"&&Se.getVerifiedCallerId(l.id).then(w).catch(()=>{}))},[s,l]);const H=E?.id,A=E?.status==="pending";a.useEffect(()=>{if(!s||!H||!A)return;const L=window.setInterval(async()=>{if(!V.current){V.current=!0;try{const ie=await Se.getVerifiedCallerId(H);w(ie),ie.status!=="pending"&&z.current(),ie.status==="verified"&&f({title:r("toast.success"),description:r("importNumber.verifiedToast")})}catch{return}finally{V.current=!1}}},hi);return()=>window.clearInterval(L)},[s,H,A,r,f]);const S=L=>{if(L instanceof jr){const ie=typeof L.message=="string"?L.message:"";if(L.status===400)return r("importNumber.errors.invalidNumber");if(L.status===403)return r("importNumber.errors.planRequired");if(L.status===409)return ie.includes("telephony provider")?r("importNumber.errors.contactSupport"):ie.includes("already on your account")?r("importNumber.errors.alreadyOnAccount"):ie.includes("already verified")?r("importNumber.errors.alreadyVerified"):r("importNumber.errors.alreadyRegistered")}return r("importNumber.errors.generic")},T=async()=>{if(!x&&(F(!0),!(!O&&!g))){D(!0);try{const L=O&&l?await Se.retryVerifiedCallerId(l.id):await Se.createVerifiedCallerId({phone_number:g,agent_id:v===Ot?null:v});w(L),d()}catch(L){f({title:r("toast.error"),description:S(L),variant:"destructive"})}finally{D(!1)}}},p=async()=>{if(!(!E||x)){D(!0);try{const L=await Se.retryVerifiedCallerId(E.id);w(L),d()}catch(L){f({title:r("toast.error"),description:S(L),variant:"destructive"})}finally{D(!1)}}},X=e.jsxs("div",{className:"space-y-4 text-sm text-gray-600 dark:text-gray-400",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"font-semibold text-gray-700 dark:text-gray-300",children:r("importNumber.hintTitle")}),e.jsxs("ul",{className:"list-disc space-y-1 pl-5",children:[e.jsx("li",{children:e.jsx(_t,{i18nKey:"importNumber.hintOutboundOnly",components:{bold:Rt}})}),e.jsx("li",{children:e.jsx(_t,{i18nKey:"importNumber.hintNoInbound",components:{bold:Rt}})})]}),e.jsx("p",{children:e.jsx(_t,{i18nKey:"importNumber.hintCode",components:{bold:Rt}})}),e.jsx("p",{children:e.jsx(_t,{i18nKey:"importNumber.hintCall",components:{bold:Rt}})})]}),e.jsxs("div",{className:"rounded-lg border border-amber-200 dark:border-amber-900 bg-amber-50 dark:bg-amber-950/30 p-3 text-amber-900 dark:text-amber-200",children:[e.jsx("p",{className:"mb-1 font-semibold",children:r("importNumber.pitfallsTitle")}),e.jsxs("ul",{className:"list-disc space-y-1 pl-5",children:[e.jsx("li",{children:r("importNumber.pitfallKeypad")}),e.jsx("li",{children:r("importNumber.pitfallPerson")}),e.jsx("li",{children:r("importNumber.pitfallCloudPbx")}),e.jsx("li",{children:r("importNumber.pitfallOneAtATime")})]})]})]}),N=()=>e.jsxs(e.Fragment,{children:[X,O?e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(Be,{children:r("importNumber.phoneNumber")}),e.jsx("p",{className:"text-base font-medium text-gray-900 dark:text-gray-200",children:M})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(Be,{htmlFor:"import-number-input",children:r("importNumber.phoneNumber")}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(be,{value:C,onValueChange:y,children:[e.jsx(fe,{className:"w-[120px] shrink-0","aria-label":r("importNumber.country"),children:e.jsx(ye,{children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx("span",{children:nr(C)}),e.jsx("span",{children:ir(C)})]})})}),e.jsx(je,{children:sn.map(L=>e.jsx(J,{value:L,children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx("span",{children:nr(L)}),e.jsx("span",{children:j?.of(L)??L}),e.jsx("span",{className:"text-gray-500",children:ir(L)})]})},L))})]}),e.jsx(re,{id:"import-number-input",inputMode:"tel",autoComplete:"tel",value:u,onChange:L=>h(L.target.value),placeholder:r("importNumber.phoneNumberPlaceholder"),className:q&&!g?"border-red-500 focus-visible:ring-red-500":""})]}),e.jsx("p",{className:`text-xs ${q&&!g?"text-red-600":"text-gray-500"}`,children:q&&!g?r("importNumber.invalidNumber"):g?r("importNumber.willVerify",{number:Ze(g)||g}):r("importNumber.phoneNumberHelp")})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(Be,{children:r("importNumber.assistant")}),e.jsxs(be,{value:v,onValueChange:I,children:[e.jsx(fe,{children:e.jsx(ye,{placeholder:r("importNumber.noAssistant")})}),e.jsxs(je,{children:[e.jsx(J,{value:Ot,children:r("importNumber.noAssistant")}),m.map(L=>e.jsx(J,{value:L.id,children:L.name},L.id))]})]}),e.jsx("p",{className:"text-xs text-gray-500",children:r("importNumber.assistantHelp")})]})]}),e.jsxs(Ee,{className:"gap-2 sm:gap-0",children:[e.jsx(P,{variant:"outline",onClick:()=>t(!1),disabled:x,children:r("common.cancel")}),e.jsxs(P,{className:"bg-black text-white hover:bg-gray-800",onClick:T,disabled:x,children:[x&&e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}),r(O?"importNumber.callAgain":"importNumber.submit")]})]})]}),de=()=>e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-col items-center gap-4 py-2 text-center",children:[e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:r("importNumber.codeLabel")}),e.jsx("div",{className:"font-mono text-5xl font-bold tracking-[0.3em] text-gray-900 dark:text-gray-100","data-testid":"verification-code",children:E?.validation_code}),e.jsxs("div",{className:"flex items-center gap-2 rounded-lg bg-blue-50 dark:bg-slate-800 px-4 py-3 text-sm text-blue-800 dark:text-blue-200",children:[e.jsx(ae,{className:"h-4 w-4 shrink-0 animate-spin"}),e.jsx("span",{children:r("importNumber.calling",{number:M})})]}),e.jsx("p",{className:"text-xs text-gray-500",children:r("importNumber.pendingHelp")})]}),e.jsx("div",{className:"rounded-lg border border-amber-200 dark:border-amber-900 bg-amber-50 dark:bg-amber-950/30 p-3 text-sm text-amber-900 dark:text-amber-200",children:e.jsxs("ul",{className:"list-disc space-y-1 pl-5",children:[e.jsx("li",{children:r("importNumber.pitfallKeypad")}),e.jsx("li",{children:r("importNumber.pitfallPerson")}),e.jsx("li",{children:r("importNumber.pitfallCloudPbx")})]})}),e.jsx(Ee,{children:e.jsx(P,{variant:"outline",onClick:()=>t(!1),children:r("importNumber.closeAndContinue")})})]}),Y=()=>e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-col items-center gap-3 py-4 text-center",children:[e.jsx(Ds,{className:"h-12 w-12 text-green-600"}),e.jsx("p",{className:"text-lg font-semibold text-gray-900 dark:text-gray-100",children:r("importNumber.verifiedTitle")}),e.jsx("p",{className:"text-base font-medium",children:M}),e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:r("importNumber.verifiedDescription")})]}),e.jsx(Ee,{children:e.jsx(P,{className:"bg-black text-white hover:bg-gray-800",onClick:()=>t(!1),children:r("importNumber.done")})})]}),ne=()=>e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-col items-center gap-3 py-2 text-center",children:[e.jsx(wt,{className:"h-12 w-12 text-red-600"}),e.jsx("p",{className:"text-lg font-semibold text-gray-900 dark:text-gray-100",children:r("importNumber.failedTitle")}),e.jsx("p",{className:"text-base font-medium",children:M}),e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:r("importNumber.failedDescription")})]}),e.jsx("div",{className:"rounded-lg border border-amber-200 dark:border-amber-900 bg-amber-50 dark:bg-amber-950/30 p-3 text-sm text-amber-900 dark:text-amber-200",children:e.jsxs("ul",{className:"list-disc space-y-1 pl-5",children:[e.jsx("li",{children:r("importNumber.pitfallKeypad")}),e.jsx("li",{children:r("importNumber.pitfallPerson")}),e.jsx("li",{children:r("importNumber.pitfallCloudPbx")}),e.jsx("li",{children:r("importNumber.pitfallOneAtATime")})]})}),e.jsxs(Ee,{className:"gap-2 sm:gap-0",children:[e.jsx(P,{variant:"outline",onClick:()=>t(!1),disabled:x,children:r("common.cancel")}),e.jsxs(P,{className:"bg-black text-white hover:bg-gray-800",onClick:p,disabled:x,children:[x?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(Za,{className:"mr-2 h-4 w-4"}),r("importNumber.retry")]})]})]});return e.jsx(we,{open:s,onOpenChange:t,children:e.jsxs(Ce,{className:"max-h-[90vh] overflow-y-auto sm:max-w-xl",children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:r(O?"importNumber.retryTitle":"importNumber.title")}),e.jsx(Oe,{children:r("importNumber.dialogDescription")})]}),B==="pending"?de():B==="verified"?Y():B==="failed"?ne():N()]})})}function pi({phoneNumbers:s,agents:t,loading:i,outboundEnabled:l,onChanged:d}){const{t:r,i18n:_}=Re(),{toast:f}=We(),C=Ss(),y=ws(g=>
5g.ability),[u,h]=a.useState(!1),[v,I]=a.useState(null),[x,D]=a.useState(null),[E,w]=a.useState(!1),q=a.useRef(!1),F=a.useMemo(()=>s.filter(Cr),[s]),V=a.useMemo(()=>new Intl.DateTimeFormat(_.language||"en",{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"}),[_.language]);a.useEffect(()=>{if(q.current)return;const g=F.filter(O=>O.verification_status==="pending");g.length!==0&&(q.current=!0,Promise.all(g.map(O=>Se.getVerifiedCallerId(O.id).catch(()=>null))).then(O=>{O.some(B=>B&&B.status!=="pending")&&d()}))},[F,d]);const z=g=>{I(g),h(!0)},m=g=>{if(!g)return null;const O=new Date(g);return Number.isNaN(O.getTime())?null:V.format(O)},j=async()=>{if(x){w(!0);try{await Se.deleteNumber(x.id),f({title:r("toast.success"),description:r("importNumber.deleted")}),d()}catch{f({title:r("toast.deleteFailed"),description:r("importNumber.deleteFailed"),variant:"destructive"})}finally{w(!1),D(null)}}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(ke,{className:"border border-gray-200 dark:border-gray-800",children:[e.jsx(ze,{className:"border-b border-gray-100 dark:border-gray-800",children:e.jsxs("div",{className:"flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(qe,{className:"text-lg font-medium text-black dark:text-gray-300",children:r("importNumber.tabTitle")}),e.jsx(ct,{className:"text-gray-600 dark:text-gray-400",children:r("importNumber.tabDescription")})]}),e.jsxs(P,{className:"bg-black text-white hover:bg-gray-800",onClick:()=>z(null),disabled:!l,children:[e.jsx(mn,{className:"mr-2 h-4 w-4"}),r("importNumber.submit")]})]})}),e.jsxs($e,{className:"space-y-3 px-6 pb-6 pt-4 text-sm text-gray-600 dark:text-gray-400",children:[e.jsx("div",{className:"flex flex-wrap items-center gap-2",children:e.jsx(_s,{})}),e.jsxs("ul",{className:"list-disc space-y-1 pl-5",children:[e.jsx("li",{children:r("importNumber.tabPointOutbound")}),e.jsx("li",{children:r("importNumber.tabPointFree")}),e.jsx("li",{children:r("importNumber.tabPointVerification")})]}),!l&&e.jsx("p",{className:"rounded-lg border border-amber-200 bg-amber-50 p-3 text-amber-900 dark:border-amber-900 dark:bg-amber-950/30 dark:text-amber-200",children:r("importNumber.errors.planRequired")})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-medium text-black dark:text-gray-300",children:r("importNumber.listTitle")}),e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:r("importNumber.listDescription")})]}),e.jsx(ke,{className:"border border-gray-200 dark:border-slate-800",children:e.jsxs(xt,{children:[e.jsx(pt,{children:e.jsxs(xe,{children:[e.jsx(Q,{children:r("telephony.phoneNumber")}),e.jsx(Q,{children:r("status.status")}),e.jsx(Q,{children:r("importNumber.verificationDate")}),e.jsx(Q,{children:r("telephony.assignedTo")}),e.jsx(Q,{className:"text-right",children:r("common.actions")})]})}),e.jsx(gt,{children:i&&F.length===0?e.jsx(xe,{children:e.jsx(W,{colSpan:5,className:"py-8 text-center",children:e.jsx(ae,{className:"mx-auto h-5 w-5 animate-spin text-gray-400"})})}):F.length===0?e.jsx(xe,{children:e.jsx(W,{colSpan:5,className:"py-8 text-center text-gray-500",children:r("importNumber.empty")})}):F.map(g=>{const O=t.find(A=>A.id===g.agent_id),B=y.can(he.DELETE,js(me.PHONE_NUMBER,{id:g.id})),M=m(g.verified_at),H=m(g.verification_requested_at);return e.jsxs(xe,{children:[e.jsx(W,{className:"font-medium text-black dark:text-slate-300",children:e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("span",{children:Ze(g.phone_number)||g.phone_number}),e.jsx(_s,{})]})}),e.jsx(W,{children:e.jsx(wr,{status:g.verification_status})}),e.jsx(W,{className:"text-gray-700 dark:text-gray-400",children:M?r("importNumber.verifiedOn",{date:M}):H?r("importNumber.requestedOn",{date:H}):"-"}),e.jsx(W,{children:g.agent_id?e.jsx("button",{onClick:()=>C(`/agents/${g.agent_id}`),className:"text-blue-600 hover:text-blue-800 hover:underline",children:O?.name||r("common.unknownAgent")}):e.jsx("span",{className:"text-gray-400",children:r("common.unassigned")})}),e.jsx(W,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[g.verification_status==="pending"&&e.jsx(P,{variant:"outline",size:"sm",onClick:()=>z(g),children:r("importNumber.showCode")}),g.verification_status==="failed"&&e.jsx(P,{variant:"outline",size:"sm",onClick:()=>z(g),disabled:!l,children:r("importNumber.retry")}),B&&e.jsx(P,{variant:"ghost",size:"sm",className:"text-red-600 hover:bg-red-50 hover:text-red-800",onClick:()=>
5D(g),disabled:E,children:r("telephony.delete")})]})})]},g.id)})})]})}),e.jsx(xi,{open:u,onOpenChange:h,agents:t,target:v,onChanged:d}),e.jsx(Gt,{open:!!x,onOpenChange:g=>!g&&D(null),children:e.jsxs(Kt,{children:[e.jsxs(Zt,{children:[e.jsx(Jt,{children:r("importNumber.deleteTitle",{phone:x?Ze(x.phone_number)||x.phone_number:""})}),e.jsx(Qt,{children:r("importNumber.deleteDescription")})]}),e.jsxs(Xt,{children:[e.jsx(es,{disabled:E,children:r("common.cancel")}),e.jsx(ts,{className:"bg-red-600 text-white hover:bg-red-700",onClick:j,disabled:E,children:r("importNumber.deleteConfirm")})]})]})})]})}const gi=[{label:"telephony.unitedStates",value:"US"},{label:"telephony.germany",value:"DE"}];class fs{static async listCustomerPhones(t=1,i=10,l){const d=Lt(),r=new URL(`${$t}/customer-phones/`);r.searchParams.set("page",String(t)),r.searchParams.set("size",String(i)),l?.search&&r.searchParams.set("customer_phone_number__ilike",l.search),l?.blocked!==void 0&&r.searchParams.set("blocked",String(l.blocked));const _=await fetch(r.toString(),{headers:Ut(d),credentials:"include"});return Bt(_)}static async blockCustomerPhone(t){const i=Lt(),l=await fetch(`${$t}/customer-phones/block`,{method:"POST",headers:Ut(i),credentials:"include",body:JSON.stringify(t)});return Bt(l)}static async createCustomerPhone(t){const i=Lt(),l=await fetch(`${$t}/customer-phones/create`,{method:"POST",headers:Ut(i),credentials:"include",body:JSON.stringify(t)});return Bt(l)}}class bi{static async request(t,i={}){const l=Lt(),d=`${$t}/lockers${t}`,r=await fetch(d,{...i,headers:Ut(l)});return Bt(r)}static async getPhoneNumberCounts(){return this.request("/phone-numbers/count")}}const ys=Ea(s=>({counts:null,isLoading:!1,hasLoaded:!1,fetchCounts:async()=>{s({isLoading:!0});try{const t=await bi.getPhoneNumberCounts();s({counts:t,isLoading:!1,hasLoaded:!0})}catch{s({isLoading:!1,hasLoaded:!0})}}})),br=10,Mt="all";function fi(){const{toast:s}=We(),{t}=Re(),i=Ss(),l=ws(o=>o.ability),d=l.can(he.VIEW,me.PHONE_NUMBER),r=l.can(he.CREATE,me.PHONE_NUMBER),_=l.can(he.VIEW,me.CUSTOMER_PHONE_NUMBER),{subscription:f,isLoading:C}=Va(),{plan:y,allowGermanNumbers:u,outboundEnabled:h}=Nr(),[v,I]=a.useState([]),[x,D]=a.useState([]),[E,w]=a.useState([]),[q,F]=a.useState([]),[V,z]=a.useState(!1),[m,j]=a.useState(!1),[g,O]=a.useState(!1),[B,M]=a.useState(!1),[H,A]=a.useState(!1),[S,T]=a.useState(!1),[p,X]=a.useState(null),[N,de]=a.useState(""),[Y,ne]=za(),L=Y.get("tab"),ve=L===De?De:L===Fe?Fe:L===jt?jt:L===rt?rt:d?Ie:r?De:_?Fe:Ie,[Z,ue]=a.useState(ve),[ge,Ae]=a.useState(!1),[K,ee]=a.useState(1),[le,Ge]=a.useState(1),[c,R]=a.useState(0),[k,$]=a.useState(null),[n,b]=a.useState(""),[G,ce]=a.useState("all"),[te,oe]=a.useState(""),[Tt,Is]=a.useState(!1),[Yr,rs]=a.useState(!1),as=a.useCallback(o=>{if(o==="blocked")return!0;if(o==="unblocked")return!1},[]),[_e,Pt]=a.useState({country_code:"US",limit:50,kind:Mt,contains:""}),[Os,Wr]=a.useState(!1),[Je,Rs]=a.useState(!1),[Qe,Ms]=a.useState(""),[Xe,Ls]=a.useState("all"),[nt,$s]=a.useState("all"),[Ke,Us]=a.useState("all"),[it,Bs]=a.useState("all"),lt=ys(o=>o.counts),ns=ys(o=>o.fetchCounts),Gr=ys(o=>o.hasLoaded),[Kr,Hs]=a.useState(null),[Zr,Vs]=a.useState(!1),[zs,qs]=a.useState(!1),Et=f?.custom_config?.enabled?f?.custom_config?.custom_phone_number_limit:y?.PHONE_NUMBER_LIMIT,Jr=typeof Et=="number"?Et:null,Qr=typeof Et=="number"&&Et>=0?Jr:null,Ys=f?.subscription_plan_name,Xr=Ys===tr.ENTERPRISE,ea=Ys===tr.FREEMIUM,is=!!(f?.custom_config?.enabled&&f?.custom_config?.allow_credits_for_phone_numbers),Ws=!!qa(o=>o.user)?.company_pays_phone_numbers?!0:Kr,et=!u;a.useEffect(()=>{if(C)return;if(Os){Je||Rs(!0);return}const o=et?"US":"DE";if(_e.country_code!==o){Pt(U=>({...U,country_code:o}));return}Je||Rs(!0)},[Os,Je,et,C,_e.country_code]),a.useEffect(()=>{d&&(tt(),ns()),la(),ia()},[d]);const ta=a.useMemo(()=>v.filter(o=>Es(vt(o))),[v]),sa=a.useMemo(()=>v.filter(o=>qt(vt(o))),[v]),ra=ta.length,aa=sa.length,Gs=ra+aa,Ks=Gs>0,na=Ks?t("telephony.billingIssuesBannerTitle",{count:Gs}):"",ia=a.useCallback(async()=>{try{Vs(!0);const o=await kt.getPaymentMethodStatus("phone_numbers");Hs(!!o.has_payment_method)}catch{Hs(null)}finally{Vs(!1)}},[]),ls=a.useCallback(async(o,U,se)=>{try{M(!0);const pe=await fs.listCustomerPhones(o,br,{search:U,blocked:se});F(pe.items),R(pe.total),Ge(Math.max(pe.pages??1,1))}
5catch{s({title:t("toast.error"),description:t("telephony.failedToLoadCustomerNumbers"),variant:"destructive"})}finally{M(!1)}},[s,t]),tt=async()=>{try{z(!0);const o=await Se.getMyNumbers();I(o.data),ns()}catch{s({title:t("toast.error"),description:t("messages.failedToLoadPhoneNumbers"),variant:"destructive"})}finally{z(!1)}},la=async()=>{try{O(!0);const o=await Ct.getAgents();D(o.data)}catch{}finally{O(!1)}},oa=async()=>{try{qs(!0);const o=await kt.createPaymentMethodCheckoutSession(Ga);window.location.href=o.checkout_url}catch{s({title:t("toast.error"),description:t("messages.paymentMethodStatusFailed"),variant:"destructive"})}finally{qs(!1)}},ca=async()=>{try{Ae(!0);const o=await kt.createPaymentMethodCheckoutSession(Wa);window.location.href=o.checkout_url}catch{s({title:t("toast.error"),description:t("account.updatePaymentMethodFailed"),variant:"destructive"})}finally{Ae(!1)}},da=async()=>{try{A(!0),await Se.retryFailedSubscriptions(),await tt(),ns(),s({title:t("toast.success"),description:t("telephony.retryChargesOnlySuccess")})}catch{s({title:t("toast.error"),description:t("telephony.retryChargesOnlyFailed"),variant:"destructive"})}finally{A(!1)}},At=a.useCallback(o=>{if(o&&o!==Mt){if(o==="toll-free")return"toll_free";if(o==="local"||o==="mobile")return o}},[]),Zs=a.useCallback(async o=>{if(!r){w([]);return}try{if(j(!0),et&&o.country_code==="DE"){w([]);return}const U=At(o.kind),se=await Se.getNumbersToBuy({...o,kind:U});w(se.data)}catch{s({title:t("toast.error"),description:t("messages.failedToSearchNumbers"),variant:"destructive"})}finally{j(!1)}},[r,et,At,t,s]),Js=a.useCallback(async(o,U)=>{if(!r){w([]);return}try{if(j(!0),et){w([]);return}const se=At(U),pe=await Se.getNumbersToBuy({country_code:"DE",limit:_e.limit,contains:o??null,kind:se??null});w(pe.data)}catch{s({title:t("toast.error"),description:t("messages.failedToSearchNumbers"),variant:"destructive"})}finally{j(!1)}},[r,et,At,_e.limit,t,s]),Dt=a.useCallback(async o=>{if(!r){w([]);return}if(o.country_code==="DE"){const U=o.contains?.trim();await Js(U||void 0,o.kind)}else await Zs(o)},[r,Js,Zs]),os=a.useRef(_e);a.useEffect(()=>{os.current=_e},[_e]);const bt=a.useRef(null),[cs,Qs]=a.useState("");a.useEffect(()=>{!Je||!r||Dt(os.current)},[r,Je,_e.country_code,_e.kind,Dt]);const Xs=a.useRef(!1),ua=_e.contains||"";a.useEffect(()=>{if(!Xs.current){Xs.current=!0;return}if(!Je||!r)return;const o=setTimeout(()=>{Dt(os.current)},1e3);return()=>clearTimeout(o)},[r,ua,Je,Dt]);const ma=async()=>{if(!p||!N){s({title:t("toast.error"),description:t("messages.pleaseSelectBoth"),variant:"destructive"});return}if(!Ns.test(p.id)){s({title:t("toast.invalidPhoneNumberId"),description:t("messages.phoneNumberIdFormatInvalid"),variant:"destructive"});return}if(!Ns.test(N)){s({title:t("toast.invalidAgentId"),description:t("messages.agentIdFormatInvalid"),variant:"destructive"});return}try{z(!0);const o={phone_number_id:p.id,agent_id:N};await Se.connectNumber(o),s({title:t("toast.success"),description:t("messages.phoneNumberConnectedSuccessfully")}),tt(),T(!1),X(null),de("")}catch(o){const U=t("messages.failedToConnectPhoneNumber"),se=typeof o=="object"&&o!==null&&"detail"in o&&typeof o.detail=="string"?o.detail:void 0,pe=_r(o)?Wt(o,t,U):o instanceof Error&&o.message||se||(typeof o=="string"?o:void 0)||U;s({title:t("toast.connectionFailed"),description:pe,variant:"destructive"})}finally{z(!1)}},ha=()=>v.filter(o=>ar(vt(o))).reduce((o,U)=>o+U.price,0),ds=a.useMemo(()=>v.filter(o=>{const U=vt(o),se=Qe===""||o.phone_number.toLowerCase().includes(Qe.toLowerCase())||o.agent_id&&x.find(Sa=>Sa.id===o.agent_id)?.name?.toLowerCase().includes(Qe.toLowerCase()),pe=Xe==="all"||Xe==="active"&&ar(U)||Xe==="inactive"&&qt(U),Ft=nt==="all"||o.country_code===nt,_a=Ke==="all"||Ke==="unassigned"&&!o.agent_id||Ke!=="unassigned"&&o.agent_id===Ke,ka=(o.source||"twilio").toString();return se&&pe&&Ft&&_a&&(it==="all"||ka===it)}),[v,Qe,Xe,nt,Ke,it,x]),xa=()=>{Ms(""),Ls("all"),$s("all"),Us("all"),Bs("all")},pa=async(o,U)=>{try{$(o.id);const se=await fs.blockCustomerPhone({customer_phone_number:o.customer_phone_number,blocked:U});F(pe=>pe.map(Ft=>Ft.id===se.id?se:Ft)),s({title:t("toast.success"),description:t("telep
5hony.customerNumberUpdated")})}catch{s({title:t("toast.error"),description:t("telephony.customerNumberUpdateFailed"),variant:"destructive"})}finally{$(null)}},ga=async()=>{const o=te.trim();if(!o){s({title:t("toast.error"),description:t("telephony.customerNumberRequired"),variant:"destructive"});return}try{Is(!0),await fs.createCustomerPhone({customer_phone_number:o,blocked:!0}),oe(""),rs(!1),ee(1),await ls(1,cs||void 0,as(G)),s({title:t("toast.success"),description:t("telephony.customerNumberAdded")})}catch{s({title:t("toast.error"),description:t("telephony.customerNumberAddFailed"),variant:"destructive"})}finally{Is(!1)}},ba=()=>{ee(o=>Math.max(o-1,1))},fa=()=>{ee(o=>Math.min(o+1,le||o+1))},ya=()=>[...new Set(v.map(U=>U.country_code))].sort().filter(U=>U);a.useEffect(()=>{L&&L!==Z&&(L===Ie||L===De||L===Fe||L===jt||L===rt)&&ue(L)},[Z,L]),a.useEffect(()=>{if(Z!==Fe){Qs(n);return}return bt.current&&clearTimeout(bt.current),ee(1),bt.current=setTimeout(()=>{Qs(n)},500),()=>{bt.current&&clearTimeout(bt.current)}},[n,Z]),a.useEffect(()=>{Z===Fe&&ee(1)},[G,Z]),a.useEffect(()=>{Z===Fe&&ls(K,cs||void 0,as(G))},[Z,K,cs,G,as,ls]);const er=q.length===0?0:(K-1)*br+1,ja=q.length===0?0:er+q.length-1,ft=lt!==null?lt.total:null,st=d&&(ft!==null?ft>0||!r:!0),yt="inline-flex items-center justify-center whitespace-nowrap px-4 py-2 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-voico-yellow-600 data-[state=active]:text-black data-[state=active]:shadow-sm data-[state=active]:font-semibold hover:bg-gray-50 dark:hover:bg-gray-500 dark:hover:text-gray-300 first:rounded-l-md last:rounded-r-md",Na=o=>{const U=st?Ie:r?De:_?Fe:Ie;let se=o;se===Ie&&!st&&(se=U),se===De&&!r&&(se=U),se===Fe&&!_&&(se=U),se===rt&&!r&&(se=U),ue(se);const pe=new URLSearchParams(Y);pe.set("tab",se),ne(pe,{replace:!0})};a.useEffect(()=>{if(ft===0&&Z===Ie&&r){const o=new URLSearchParams(Y);o.set("tab",De),ne(o,{replace:!0}),ue(De)}},[Z,r,Y,ne,ft]),a.useEffect(()=>{const o=st?Ie:r?De:_?Fe:Ie;if(Z===Ie&&!st){const U=new URLSearchParams(Y);U.set("tab",o),ne(U,{replace:!0}),ue(o)}if(Z===De&&!r){const U=new URLSearchParams(Y);U.set("tab",o),ne(U,{replace:!0}),ue(o)}if(Z===Fe&&!_){const U=new URLSearchParams(Y);U.set("tab",o),ne(U,{replace:!0}),ue(o)}if(Z===rt&&!r){const U=new URLSearchParams(Y);U.set("tab",o),ne(U,{replace:!0}),ue(o)}},[Z,r,_,Y,ne,st]);const va=ft===0&&Z===Ie&&r?De:Z;return d&&!Gr?null:e.jsxs("div",{className:"space-y-8",children:[e.jsxs(Cs,{value:va,onValueChange:Na,className:"w-full",children:[e.jsxs(Ts,{className:"inline-flex h-12 items-center justify-center rounded-lg bg-white dark:bg-slate-700 border border-gray-200 dark:border-slate-800 p-1 text-gray-600 dark:text-slate-300 shadow-sm w-auto",children:[st&&e.jsx(He,{value:Ie,className:yt,children:t("telephony.manageNumbers")}),e.jsx(Ue,{I:he.CREATE,a:me.PHONE_NUMBER,children:e.jsx(He,{value:De,className:yt,children:t("telephony.buyNumbers")})}),e.jsx(Ue,{I:he.VIEW,a:me.CUSTOMER_PHONE_NUMBER,children:e.jsx(He,{value:Fe,className:yt,children:t("telephony.customerNumbersTab")})}),e.jsx(He,{value:jt,className:yt,children:t("telephony.sipTrunksTab")}),e.jsx(Ue,{I:he.CREATE,a:me.PHONE_NUMBER,children:e.jsx(He,{value:rt,className:yt,children:t("importNumber.tab")})})]}),st&&e.jsxs(Ve,{value:Ie,className:"space-y-6",children:[Ks&&e.jsxs("div",{className:"flex flex-col gap-4 md:flex-row md:items-center border border-red-200 dark:border-red-900 bg-red-50/80 dark:bg-red-950/30 rounded-lg p-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("p",{className:"font-semibold text-red-800 dark:text-red-200",children:na}),e.jsx("p",{className:"text-sm text-red-700 dark:text-red-300 mt-1",children:t("telephony.billingIssuesBannerDescription")})]}),e.jsxs("div",{className:"flex flex-col gap-2 sm:flex-row",children:[e.jsx(P,{variant:"outline",className:"border-red-300 text-red-700 hover:bg-red-100",onClick:da,disabled:H,children:H?e.jsxs(e.Fragment,{children:[e.jsx(ae,{className:"h-4 w-4 mr-2 animate-spin"}),t("common.loading")]}
5):t("telephony.retryChargesOnly")}),e.jsx(P,{className:"bg-red-600 hover:bg-red-700 text-white",onClick:ca,disabled:ge,children:ge?e.jsxs(e.Fragment,{children:[e.jsx(ae,{className:"h-4 w-4 mr-2 animate-spin"}),t("common.loading")]}):t("telephony.updatePaymentMethodRetryAll")})]})]}),Ws===!1&&!is&&(lt?.active??0)>0&&e.jsx(ke,{className:"border border-blue-200 dark:border-blue-900 bg-blue-50 dark:bg-slate-900/50",children:e.jsxs($e,{className:"p-5 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-base font-semibold text-blue-900 dark:text-blue-100",children:t("telephony.addPaymentMethodTitle")}),e.jsx("p",{className:"text-sm text-blue-800 dark:text-blue-200 mt-1",children:t("telephony.addPaymentMethodDescription")})]}),e.jsx(P,{className:"bg-blue-600 hover:bg-blue-700 text-white",onClick:oa,disabled:Zr||zs,children:t(zs?"common.loading":"telephony.addPaymentMethodCTA")})]})}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-medium text-black dark:text-gray-300",children:t("telephony.yourPhoneNumbers")}),e.jsxs("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:[t("telephony.manageAndAssignNumbers"),(Qe||Xe!=="all"||nt!=="all"||Ke!=="all"||it!=="all")&&e.jsxs("span",{className:"ml-2 text-blue-600",children:["(",ds.length," ",t("common.of")," ",v.length," ",t("common.shown"),")"]})]})]}),e.jsxs("div",{className:"flex space-x-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(lr,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400"}),e.jsx(re,{placeholder:t("placeholders.searchNumbers"),className:"w-64 pl-10",value:Qe,onChange:o=>Ms(o.target.value)})]}),e.jsxs(be,{value:Xe,onValueChange:Ls,children:[e.jsx(fe,{className:"w-[150px]",children:e.jsx(ye,{placeholder:t("placeholders.status")})}),e.jsxs(je,{children:[e.jsx(J,{value:"all",children:t("telephony.allStatus")}),e.jsx(J,{value:"active",children:t("status.active")}),e.jsx(J,{value:"inactive",children:t("status.inactive")})]})]}),e.jsxs(be,{value:nt,onValueChange:$s,children:[e.jsx(fe,{className:"w-[150px]",children:e.jsx(ye,{placeholder:t("telephony.allCountries")})}),e.jsxs(je,{children:[e.jsx(J,{value:"all",children:t("telephony.allCountries")}),ya().map(o=>e.jsx(J,{value:o,children:o},o))]})]}),e.jsxs(be,{value:Ke,onValueChange:Us,children:[e.jsx(fe,{className:"w-[180px]",children:e.jsx(ye,{placeholder:t("dashboard.allAgents")})}),e.jsxs(je,{children:[e.jsx(J,{value:"all",children:t("dashboard.allAgents")}),e.jsx(J,{value:"unassigned",children:t("unassigned")}),x.map(o=>e.jsx(J,{value:o.id,children:o.name||"Unnamed Agent"},o.id))]})]}),e.jsxs(be,{value:it,onValueChange:Bs,children:[e.jsx(fe,{className:"w-[160px]",children:e.jsx(ye,{placeholder:t("telephony.sourceFilterAll")})}),e.jsxs(je,{children:[e.jsx(J,{value:"all",children:t("telephony.sourceFilterAll")}),e.jsx(J,{value:"twilio",children:t("telephony.sourceFilterTwilio")}),e.jsx(J,{value:"sip_trunk",children:t("telephony.sourceFilterSip")}),e.jsx(J,{value:"manual",children:t("telephony.sourceFilterManual")}),e.jsx(J,{value:"verified",children:t("importNumber.sourceFilter")})]})]}),(Qe||Xe!=="all"||nt!=="all"||Ke!=="all"||it!=="all")&&e.jsxs(P,{variant:"outline",size:"sm",onClick:xa,className:"text-gray-600 hover:text-gray-800",children:[e.jsx(kr,{className:"h-4 w-4 mr-1"}),t("common.clear")]})]})]}),e.jsx(ke,{className:"border border-gray-200 dark:border-slate-800",children:e.jsxs(xt,{className:"rounded-xl",children:[e.jsx(pt,{children:e.jsxs(xe,{className:"border-b border-gray-200 dark:border-slate-800 text-gray-700 dark:text-gray-400 font-medium",children:[e.jsx(Q,{className:"",children:t("telephony.phoneNumber")}),e.jsx(Q,{className:"",children:t("telephony.country")}),e.jsx(Q,{className:"",children:t("telephony.type")}),e.jsx(Q,{className:"",children:t("telephony.assignedTo")}),e.jsx(Q,{className:"",children:t("status.status")}),e.jsx(Q,{className:"text-right ",children:t("telephony.monthly")}),e.jsx(Q,{className:"text-right ",children:t("common.actions")})]})}),e.jsx(gt,{children:V?e.jsx(xe,{children:e.jsx(W,{colSpan:7,className:"text-center py-8 dark:text-slate-300 dark:animate-pulse",children:t("telep
5hony.loadingPhoneNumbers")})}):ds.length===0?e.jsx(xe,{children:e.jsx(W,{colSpan:7,className:"text-center py-8 text-gray-500",children:v.length===0?lt?.locked&<.locked>0?t("telephony.provisioningPhoneNumbersMessage"):t("telephony.noPhoneNumbersFound"):t("telephony.noPhoneNumbersMatchCriteria")})}):ds.map((o,U)=>e.jsx(Rn,{cb:()=>{X(o),de(""),T(!0)},agents:x,agentsLoading:g,loadPhoneNumbers:tt,loading:V,number:o,setLoading:z,setPhoneNumbers:I},U))})]})}),v.length>0&&(g||x.length===0)&&e.jsx("div",{className:"p-4 bg-blue-50 dark:bg-slate-800 border border-blue-200 dark:border-slate-700 rounded-lg",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-2 h-2 bg-blue-500 rounded-full animate-pulse"}),e.jsx("p",{className:"text-sm text-blue-700",children:t(g?"telephony.helpMessageLoading":"telephony.helpMessageNoAgents")})]})}),e.jsxs("div",{className:"p-4 bg-yellow-50 dark:bg-slate-900 border border-yellow-200 dark:border-slate-800 rounded-lg flex justify-between items-center",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"font-medium text-black dark:text-gray-300",children:t("telephony.monthlyTelephonyCost")}),e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:t("telephony.totalForAllActiveNumbers")}),e.jsx("p",{className:"text-xs text-gray-500 dark:text-gray-400 mt-1",children:t(is?"telephony.monthlyTelephonyInvoiceNote":"telephony.monthlyTelephonyAutoChargeNote")})]}),e.jsx("div",{className:"text-xl font-bold text-black dark:text-gray-300",children:Ps(ha())})]})]}),e.jsx(Ue,{I:he.CREATE,a:me.PHONE_NUMBER,children:e.jsx(Ve,{value:De,className:"space-y-6 ",children:e.jsxs(ke,{className:"border border-gray-200 dark:border-gray-800",children:[e.jsxs(ze,{className:"border-b border-gray-100 dark:border-gray-800",children:[e.jsx(qe,{className:"text-lg font-medium text-black dark:text-gray-300",children:t("telephony.purchaseNewNumbers")}),e.jsx(ct,{className:"text-gray-600 dark:text-gray-400",children:t("telephony.searchAndBuyAvailableNumbers")})]}),e.jsxs($e,{className:"space-y-6 pt-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6 text-sm font-medium text-gray-700 dark:text-gray-400",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"",children:t("telephony.country")}),e.jsxs(be,{value:_e.country_code,onValueChange:o=>{Wr(!0),Pt(U=>({...U,country_code:o}))},children:[e.jsx(fe,{children:e.jsx(ye,{})}),e.jsx(je,{className:"dark:bg-slate-800",children:gi.map((o,U)=>e.jsx(J,{value:o.value,children:t(o.label)},U))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"",children:t("telephony.numberType")}),e.jsxs(be,{value:_e.kind??Mt,onValueChange:o=>Pt(U=>({...U,kind:o})),children:[e.jsx(fe,{children:e.jsx(ye,{})}),e.jsxs(je,{className:"dark:bg-slate-800",children:[e.jsx(J,{value:Mt,children:t("telephony.All Types")}),e.jsx(J,{value:"mobile",children:t("telephony.mobile")}),e.jsx(J,{value:"local",children:t("telephony.local")}),e.jsx(J,{value:"toll-free",children:t("telephony.tollFree")})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-400",children:t("telephony.containsOptional")}),e.jsx(re,{placeholder:t("placeholders.areaCodeExample"),value:_e.contains||"",onChange:o=>Pt(U=>({...U,contains:o.target.value}))})]}),m?e.jsxs("div",{className:"p-6 bg-gray-50 dark:bg-slate-700 border border-gray-200 dark:border-slate-900 rounded-lg flex items-center justify-center gap-3 text-gray-600 dark:text-gray-300",children:[e.jsx(ae,{className:"h-5 w-5 animate-spin"}),t("common.searching")]}):et&&_e.country_code==="DE"?e.jsxs(ke,{className:"border border-gray-200 dark:border-slate-800 rounded-2xl shadow-sm bg-white dark:bg-slate-900 max-w-md mx-auto",children:[e.jsxs(ze,{className:"items-center text-center",children:[e.jsx(qe,{className:"text-lg font-semibold text-gray-900 dark:text-gray-100",children:t("telephony.germanNumbers")}),e.jsx(ct,{className:"text-gray-600 dark:text-gray-400 max-w-sm",children:t("telep
5hony.germanNumbersContent")})]}),e.jsx($e,{className:"flex justify-center pb-6",children:e.jsx(P,{onClick:()=>i("/pricing"),className:"px-6",children:t("pricing.upgradePlan")||"Upgrade plan"})})]}):E.length>0?e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium text-black dark:text-gray-300",children:t("telephony.availableNumbers")}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:E.map((o,U)=>e.jsx(Pn,{number:o,phoneNumberLimit:Qr,phoneNumberCounts:lt,isEnterprisePlan:Xr,isFreemiumPlan:ea,hasPaymentMethod:Ws,allowCreditsForPhoneNumbers:is},U))})]}):e.jsx("div",{className:"p-6 bg-gray-50 dark:bg-slate-700 border border-gray-200 dark:border-slate-900 rounded-lg",children:e.jsx("p",{className:"text-center text-gray-500 dark:text-gray-300",children:t("telephony.noNumbersSearchedYet")})})]})]})})}),e.jsx(Ue,{I:he.VIEW,a:me.CUSTOMER_PHONE_NUMBER,children:e.jsx(Ve,{value:Fe,className:"space-y-6",children:e.jsxs(ke,{className:"border border-gray-200 dark:border-slate-800 rounded-2xl shadow-sm bg-white dark:bg-slate-900",children:[e.jsxs(ze,{className:"px-6 py-5 border-b border-gray-100 dark:border-slate-800",children:[e.jsx(qe,{className:"text-lg font-semibold text-gray-900 dark:text-gray-100",children:t("telephony.customerNumbersTitle")}),e.jsx(ct,{className:"text-gray-600 dark:text-gray-400",children:t("telephony.customerNumbersDescription")})]}),e.jsxs($e,{className:"p-0",children:[e.jsxs("div",{className:"flex flex-col gap-4 border-b border-gray-100 dark:border-slate-800 px-6 py-5",children:[e.jsxs("div",{className:"flex flex-col gap-3 lg:flex-row lg:items-center",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(lr,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400"}),e.jsx(re,{value:n,onChange:o=>b(o.target.value),placeholder:t("telephony.customerNumberFilterPlaceholder"),className:"pl-9"})]}),e.jsxs(be,{value:G,onValueChange:ce,children:[e.jsx(fe,{className:"w-full lg:w-[200px]",children:e.jsx(ye,{placeholder:t("telephony.customerBlockedFilterAll")})}),e.jsxs(je,{children:[e.jsx(J,{value:"all",children:t("telephony.customerBlockedFilterAll")}),e.jsx(J,{value:"blocked",children:t("telephony.customerBlockedFilterBlocked")}),e.jsx(J,{value:"unblocked",children:t("telephony.customerBlockedFilterAllowed")})]})]})]}),e.jsx("div",{className:"flex justify-end",children:e.jsxs(we,{open:Yr,onOpenChange:o=>{rs(o),o||oe("")},children:[e.jsx(Ue,{I:he.CREATE,a:me.CUSTOMER_PHONE_NUMBER,children:e.jsx(Sr,{asChild:!0,children:e.jsx(P,{className:"bg-red-600 hover:bg-red-700 text-white",children:t("telephony.openAddCustomerNumber")})})}),e.jsxs(Ce,{children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:t("telephony.blockNumberDialogTitle")}),e.jsx(Oe,{children:t("telephony.blockNumberDialogDescription")})]}),e.jsx(re,{value:te,onChange:o=>oe(o.target.value),placeholder:t("telephony.customerNumberInputPlaceholder")}),e.jsxs(Ee,{className:"sm:justify-end gap-2",children:[e.jsx(P,{variant:"outline",onClick:()=>{rs(!1),oe("")},children:t("common.cancel")}),e.jsx(P,{onClick:ga,disabled:Tt,className:"bg-red-600 hover:bg-red-700 text-white",children:t(Tt?"common.loading":"telephony.blockNumberConfirm")})]})]})]})})]}),B?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(ae,{className:"h-6 w-6 animate-spin text-gray-400"})}):q.length===0?e.jsx("p",{className:"text-sm text-gray-500 dark:text-gray-400 px-6 py-10",children:t(n||G!=="all"?"telephony.noCustomerNumbersFiltered":"telephony.noCustomerNumbers")}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(xt,{children:[e.jsx(pt,{children:e.jsxs(xe,{className:"bg-gray-50 dark:bg-slate-800/60",children:[e.jsx(Q,{className:"text-gray-600 dark:text-gray-300",children:t("telephony.customerNumberColumn")}),e.jsx(Q,{className:"text-center text-gray-600 dark:text-gray-300",children:t("agents.inboundCalls")}),e.jsx(Q,{className:"text-center text-gray-600 dark:text-gray-300",children:t("agents.outboundCalls")}),e.jsx(Q,{className:"text-gray-600 dark:text-gray-300",children:t("telep
5hony.lastCallColumn")}),e.jsx(Q,{className:"text-gray-600 dark:text-gray-300",children:t("telephony.blockedColumn")})]})}),e.jsx(gt,{children:q.map(o=>{const U=Ze(o.customer_phone_number)||o.customer_phone_number,se=o.last_call_at?hn(new Date(o.last_call_at).getTime()/1e3):t("common.none");return e.jsxs(xe,{children:[e.jsx(W,{children:e.jsx("div",{className:"font-medium text-gray-900 dark:text-gray-100",children:U})}),e.jsx(W,{className:"text-center font-semibold text-gray-900 dark:text-gray-100",children:o.inbound_call_count}),e.jsx(W,{className:"text-center font-semibold text-gray-900 dark:text-gray-100",children:o.outbound_call_count}),e.jsx(W,{className:"text-sm text-gray-600 dark:text-gray-300",children:se}),e.jsx(W,{children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Ue,{I:he.UPDATE,a:me.CUSTOMER_PHONE_NUMBER,children:e.jsx(Vt,{id:`block-${o.id}`,checked:o.blocked,disabled:k===o.id||B,onCheckedChange:pe=>pa(o,pe),"aria-label":t("telephony.blockNumberLabel"),className:"data-[state=checked]:bg-red-600 data-[state=unchecked]:bg-gray-200"})}),e.jsx(Ya,{I:he.UPDATE,a:me.CUSTOMER_PHONE_NUMBER,children:e.jsx(Vt,{id:`block-${o.id}`,checked:o.blocked,disabled:!0,"aria-label":t("telephony.blockNumberLabel"),className:"data-[state=checked]:bg-red-600 data-[state=unchecked]:bg-gray-200"})}),e.jsx("span",{className:"text-sm text-gray-700 dark:text-gray-300",children:o.blocked?t("telephony.blockedStatus"):t("telephony.allowedStatus")})]})})]},o.id)})})]})}),e.jsxs("div",{className:"flex flex-col gap-4 md:flex-row md:items-center md:justify-between border-t border-gray-100 dark:border-slate-800 px-6 py-4",children:[e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:t("telephony.customerNumbersSummary",{start:er,end:ja,total:c})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(P,{variant:"outline",size:"sm",onClick:ba,disabled:B||K<=1,children:t("common.previous")}),e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:le>0?`${K} ${t("common.of")} ${le}`:""}),e.jsx(P,{variant:"outline",size:"sm",onClick:fa,disabled:B||K>=le||c===0,children:t("common.next")})]})]})]})]})]})})}),e.jsxs(Ve,{value:jt,className:"space-y-6",children:[e.jsxs(Oa,{className:"rounded-xl border-red-300 bg-red-50 px-6 py-5 text-red-900 [&>svg]:text-red-600",children:[e.jsx(ks,{className:"h-4 w-4"}),e.jsx(Ra,{className:"text-red-600",children:t("telephony.sipTrunkNoticeTitle")}),e.jsx(Ma,{className:"text-red-900",children:e.jsx(_t,{i18nKey:"telephony.sipTrunkNoticeDescription",components:{email:e.jsx("a",{href:"mailto:[email protected]",className:"font-medium underline"})}})})]}),e.jsx("div",{className:"inbound-sip-section",children:e.jsx(li,{onRefreshPhoneNumbers:tt})}),e.jsx("div",{className:"outbound-sip-section",children:e.jsx(mi,{onRefreshPhoneNumbers:tt})})]}),e.jsx(Ue,{I:he.CREATE,a:me.PHONE_NUMBER,children:e.jsx(Ve,{value:rt,className:"space-y-6",children:e.jsx(pi,{phoneNumbers:v,agents:x,loading:V,outboundEnabled:h,onChanged:tt})})})]}),e.jsx(we,{open:S,onOpenChange:T,children:e.jsxs(Ce,{children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:t("telephony.connectPhoneNumberToAgent")}),e.jsxs(Oe,{children:[t("selectAnAgentToConnectTo")," ",p?.phone_number]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700",children:t("telephony.selectAgent")}),e.jsxs(be,{value:N,onValueChange:de,disabled:g,children:[e.jsx(fe,{children:e.jsx(ye,{placeholder:t(g?"telephony.loadingAgents":"telephony.chooseAnAgent")})}),e.jsx(je,{children:g?e.jsx(J,{value:"",disabled:!0,children:t("telephony.loadingAgentsMessage")}):x.length===0?e.jsx(J,{value:"",disabled:!0,children:t("telephony.noAgentsAvailableMessage")}):x.map(o=>e.jsxs(J,{value:o.id,children:[o.name," ",!o.is_active&&"(Inactive)"]},o.id))})]}),g&&e.jsx("p",{className:"text-sm text-gray-600",children:t("telephony.loadingAgentsMessage")}),!g&&x.length===0&&e.jsx("p",{className:"text-sm text-red-600",children:t("telephony.noAgentsFound")})]}),e.jsxs("div",{className:"flex justify-end space-x-2",children:[e.jsx(P,{variant:"outline",onClick:()=>{T(!1),X(null),de("")}
5,disabled:V,children:t("telephony.cancel")}),e.jsx(P,{onClick:ma,disabled:!N||V||g||x.length===0,className:"bg-black text-white hover:bg-gray-800",children:t(V?"common.connecting":"common.connect")})]})]})]})})]})}const dl=()=>{const{t:s}=Re();return e.jsx(La,{children:e.jsxs("div",{className:"space-y-8 ml-5",children:[e.jsx("p",{className:"text-sm text-muted-foreground dark:text-gray-400 ml-1",children:s("telephony.manageTelephonyConfig")}),e.jsx("div",{className:"phone-search-section",children:e.jsx(fi,{})})]})})};export{dl 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.