import{a as e,n as t}from"./react-dom-stZKkA6x.js";import{a as n,d as r,r as i}from"./theme-DjPzgo6g.js";import{i as a,kt as o,n as s,st as c,t as l}from"./jsx-runtime-dm82oUI8.js";import{a as ee,o as u}from"./grid-BmaFEMOO.js";import{t as d}from"./table-CSW7F8Qw.js";import{n as te,r as ne,t as f}from"./StatusPill-IFsKXy8H.js";import{t as re}from"./input-number-5vwSxUlw.js";import{t as p}from"./descriptions-vs1os_w7.js";import{t as m}from"./form-CvEcrSb3.js";import{t as h}from"./input-0_Xo22Fl.js";import{t as g}from"./popconfirm-BStiKU4v.js";import{t as _}from"./switch-CsMGEdLR.js";import{t as v}from"./CopyOutlined-DMc5GYOD.js";import{t as y}from"./DeleteOutlined-B1WEjJYP.js";import{t as ie}from"./EditOutlined-_SvZHznu.js";import{t as b}from"./PlusOutlined-DWXMp6Ds.js";import{t as x}from"./ReloadOutlined-C3S2BDne.js";import{b as S,h as ae,n as oe}from"./index-BCIgegZI.js";import{t as C}from"./EmptyState-rI31ioMm.js";var w=e(t(),1),T=l();function E(){let{message:e}=r.useApp(),{locale:t,t:l}=n(),{config:v}=oe(),[E,se]=ae(),[j]=m.useForm(),[M]=m.useForm(),[N,ce]=(0,w.useState)(null),[P,le]=(0,w.useState)([]),[F,ue]=(0,w.useState)([]),[I,L]=(0,w.useState)(!0),[R,z]=(0,w.useState)(``),[B,V]=(0,w.useState)(``),[de,H]=(0,w.useState)(!1),[U,W]=(0,w.useState)(null),[fe,G]=(0,w.useState)(!1),[K,q]=(0,w.useState)(null),[J,Y]=(0,w.useState)(null),X=E.get(`tab`),Z=X===`relays`||X===`connection`?X:`credentials`,Q=(0,w.useCallback)(async()=>{L(!0),z(``);try{let[e,t,n]=await Promise.all([i.smtpCredential(),i.smtpCredentials(),i.smtpRelays()]);ce(e.credential||null),le(t.credentials||[]),ue(n.relays||[])}catch(e){z(e instanceof Error?e.message:l(`common.error`))}finally{L(!1)}},[l]);(0,w.useEffect)(()=>{Q()},[Q]);function pe(e){let t=new URLSearchParams(E);t.set(`tab`,e),se(t)}async function me(t){!t||t===`—`||(await navigator.clipboard.writeText(t),e.success(l(`common.copied`)))}function he(){W(null),j.resetFields(),j.setFieldsValue({username:P.length===0&&(N?.username||v?.submission?.username)||``,password:``}),H(!0)}function ge(e){W(e),j.resetFields(),j.setFieldsValue({username:e.username,password:``}),H(!0)}async function _e(){let t=await j.validateFields(),n=U?.id?`credential:save:${U.id}`:`credential:create`;V(n);try{await i.saveSmtpLoginCredential(t,U?.id),e.success(l(U?`actions.smtpUpdated`:`actions.smtpCreated`)),H(!1),W(null),j.resetFields(),await Q()}catch(t){e.error(t instanceof Error?t.message:l(`common.error`))}finally{V(``)}}async function ve(t){if(t.id){V(`credential:delete:${t.id}`);try{await i.deleteSmtpCredential(t.id),e.success(l(`actions.smtpDeleted`)),Y(null),await Q()}catch(t){e.error(t instanceof Error?t.message:l(`common.error`))}finally{V(``)}}}function $(){q(null),M.resetFields(),M.setFieldsValue({name:``,host:``,port:587,secure:!1,username:``,password:``,helo:``,isDefault:F.length===0}),G(!0)}function ye(e){q(e),M.resetFields(),M.setFieldsValue({name:e.name,host:e.host,port:e.port,secure:e.secure,username:e.username,password:``,helo:e.helo,isDefault:e.isDefault}),G(!0)}async function be(){let t={...await M.validateFields()};K&&!t.password&&delete t.password;let n=K?`relay:save:${K.id}`:`relay:create`;V(n);try{await i.saveSmtpRelay(t,K?.id),e.success(l(K?`actions.smtpRelayUpdated`:`actions.smtpRelayCreated`)),G(!1),q(null),M.resetFields(),await Q()}catch(t){e.error(t instanceof Error?t.message:l(`common.error`))}finally{V(``)}}async function xe(t){V(`relay:delete:${t.id}`);try{await i.deleteSmtpRelay(t.id),e.success(l(`actions.smtpRelayDeleted`)),Y(null),await Q()}catch(t){e.error(t instanceof Error?t.message:l(`common.error`))}finally{V(``)}}let Se=[{title:l(`smtp.username`),dataIndex:`username`,render:e=>(0,T.jsx)(s.Text,{code:!0,children:e})},{title:l(`smtp.password`),dataIndex:`passwordSet`,width:140,render:e=>(0,T.jsx)(f,{tone:e?`success`:`warning`,children:l(e?`smtp.passwordSet`:`smtp.passwordEmpty`)})},{title:l(`tokens.createdAt`),dataIndex:`createdAt`,width:190,render:A},{title:l(`common.actions`),width:140,render:(e,t)=>(0,T.jsxs)(a,{onClick:e=>e.stopPropagation(),children:[(0,T.jsx)(o,{"aria-label":`${l(`common.edit`)} ${t.username}`,icon:(0,T.jsx)(ie,{}),onClick:()=>ge(t),disabled:!t.id}),(0,T.jsx)(g,{title:l(`smtp.deleteConfirm`),onConfirm:()=>void ve(t),disabled:!t.id,children:(0,T.jsx)(o,{"aria-label":`${l(`common.delete`)} ${t.username}`,danger:!0,icon:(0,T.jsx)(y,{}),loading:B===`credential:delete:${t.id}`,disabled:!t.id})})]})}],Ce=[{title:l(`smtpRelay.name`),dataIndex:`name`,render:(e,t)=>(0,T.jsxs)(a,{wrap:!0,children:[(0,T.jsx)(s.Text,{strong:!0,children:e}),t.isDefault?(0,T.jsx)(f,{tone:`success`,children:l(`smtpRelay.default`)}):null]})},{title:l(`smtpRelay.server`),render:(e,t)=>(0,T.jsxs)(s.Text,{code:!0,children:[t.host,`:`,t.port]})},{title:l(`smtpRelay.username`),dataIndex:`username`,render:e=>e||`—`},{title:l(`smtpRelay.password`),dataIndex:`passwordSet`,width:140,render:e=>(0,T.jsx)(f,{tone:e?`success`:`warning`,children:l(e?`smtpRelay.passwordSet`:`smtpRelay.passwordEmpty`)})},{title:l(`common.actions`),width:140,render:(e,t)=>(0,T.jsxs)(a,{onClick:e=>e.stopPropagation(),children:[(0,T.jsx)(o,{"aria-label":`${l(`common.edit`)} ${t.name}`,icon:(0,T.jsx)(ie,{}),onClick:()=>ye(t)}),(0,T.jsx)(g,{title:l(`smtpRelay.deleteConfirm`),onConfirm:()=>void xe(t),children:(0,T.jsx)(o,{"aria-label":`${l(`common.delete`)} ${t.name}`,danger:!0,icon:(0,T.jsx)(y,{}),loading:B===`relay:delete:${t.id}`})})]})}],we=Z===`credentials`?(0,T.jsx)(o,{type:`primary`,icon:(0,T.jsx)(b,{}),onClick:he,style:{minHeight:44},children:l(`smtp.create`)}):Z===`relays`?(0,T.jsx)(o,{type:`primary`,icon:(0,T.jsx)(b,{}),onClick:$,style:{minHeight:44},children:l(`smtpRelay.create`)}):null;return(0,T.jsxs)(a,{direction:`vertical`,size:20,className:`full-width`,children:[(0,T.jsx)(ne,{title:l(`nav.smtp`),subtitle:l(`smtp.connectionTitle`),extra:we}),R?(0,T.jsx)(u,{type:`error`,showIcon:!0,message:R,action:(0,T.jsx)(o,{icon:(0,T.jsx)(x,{}),onClick:()=>void Q(),children:l(`common.refresh`)})}):null,(0,T.jsx)(te,{children:(0,T.jsx)(ee,{activeKey:Z,onChange:pe,items:[{key:`credentials`,label:l(`smtp.loginCredentialsTitle`),children:I?(0,T.jsx)(c,{active:!0,paragraph:{rows:5}}):P.length?(0,T.jsx)(d,{rowKey:e=>e.id||e.username,columns:Se,dataSource:P,scroll:{x:760},onRow:e=>({onClick:()=>Y({type:`credential`,value:e}),style:{cursor:`pointer`}})}):(0,T.jsx)(C,{description:l(`smtp.passwordUnavailable`),action:(0,T.jsx)(o,{icon:(0,T.jsx)(b,{}),onClick:he,children:l(`smtp.create`)})})},{key:`relays`,label:l(`smtpRelay.title`),children:I?(0,T.jsx)(c,{active:!0,paragraph:{rows:5}}):F.length?(0,T.jsx)(d,{rowKey:`id`,columns:Ce,dataSource:F,scroll:{x:860},onRow:e=>({onClick:()=>Y({type:`relay`,value:e}),style:{cursor:`pointer`}})}):(0,T.jsx)(C,{description:l(`common.notConfigured`),action:(0,T.jsx)(o,{icon:(0,T.jsx)(b,{}),onClick:$,children:l(`smtpRelay.create`)})})},{key:`connection`,label:l(`smtp.connectionTitle`),children:I?(0,T.jsx)(c,{active:!0}):(0,T.jsxs)(p,{bordered:!0,column:1,size:`small`,children:[(0,T.jsx)(p.Item,{label:`SMTP Host`,children:k(v?.submission?.host||`—`,me,l(`common.copy`))}),(0,T.jsx)(p.Item,{label:`SMTP Port`,children:(0,T.jsx)(a,{wrap:!0,children:(v?.submission?.ports||[]).map(e=>(0,T.jsxs)(f,{tone:`info`,children:[e.port,` · `,e.protocol]},e.port))})}),(0,T.jsx)(p.Item,{label:`TLS / SSL`,children:v?.submission?.tls?`TLS`:`STARTTLS`}),(0,T.jsx)(p.Item,{label:l(`smtp.username`),children:k(N?.username||v?.submission?.username||`—`,me,l(`common.copy`))}),(0,T.jsx)(p.Item,{label:l(`smtp.password`),children:(0,T.jsx)(s.Text,{type:`secondary`,children:l(`smtp.resetToCopy`)})})]})}]})}),(0,T.jsxs)(S,{title:l(U?`smtp.editTitle`:`smtp.createTitle`),width:560,open:de,onClose:()=>H(!1),destroyOnHidden:!0,footer:(0,T.jsx)(D,{t:l,loading:B===`credential:create`||B.startsWith(`credential:save:`),onCancel:()=>H(!1),onSave:()=>void _e()}),children:[(0,T.jsx)(u,{type:`info`,showIcon:!0,message:t.startsWith(`en`)?`Saved SMTP passwords are never returned. Leave the password blank when editing to keep it unchanged.`:`已保存的 SMTP 密码不会返回。编辑时留空即可保留原密码。`,style:{marginBottom:20}}),(0,T.jsxs)(m,{form:j,layout:`vertical`,children:[(0,T.jsx)(m.Item,{name:`username`,label:l(`smtp.username`),rules:[{required:!0,message:l(`smtp.usernameRequired`)}],children:(0,T.jsx)(h,{autoComplete:`off`})}),(0,T.jsx)(m.Item,{name:`password`,label:l(`smtp.password`),rules:[{required:!U,message:l(`smtp.passwordRequired`)}],children:(0,T.jsx)(h.Password,{autoComplete:`new-password`})}),(0,T.jsx)(o,{icon:(0,T.jsx)(x,{}),onClick:()=>j.setFieldValue(`password`,O()),children:l(`smtp.regenerate`)})]})]}),(0,T.jsxs)(S,{title:l(K?`smtpRelay.editTitle`:`smtpRelay.createTitle`),width:600,open:fe,onClose:()=>G(!1),destroyOnHidden:!0,footer:(0,T.jsx)(D,{t:l,loading:B===`relay:create`||B.startsWith(`relay:save:`),onCancel:()=>G(!1),onSave:()=>void be()}),children:[(0,T.jsx)(u,{type:`info`,showIcon:!0,message:t.startsWith(`en`)?`Saved relay passwords are write-only. Leave the password blank when editing to keep it unchanged.`:`已保存的中继密码为只写字段。编辑时留空即可保留原密码。`,style:{marginBottom:20}}),(0,T.jsxs)(m,{form:M,layout:`vertical`,children:[(0,T.jsx)(m.Item,{name:`name`,label:l(`smtpRelay.name`),rules:[{required:!0,message:l(`smtpRelay.nameRequired`)}],children:(0,T.jsx)(h,{autoComplete:`off`,placeholder:`Amazon SES`})}),(0,T.jsx)(m.Item,{name:`host`,label:`SMTP Host`,rules:[{required:!0,message:l(`smtpRelay.hostRequired`)}],children:(0,T.jsx)(h,{autoComplete:`off`,placeholder:`email-smtp.us-east-1.amazonaws.com`})}),(0,T.jsx)(m.Item,{name:`port`,label:`SMTP Port`,rules:[{required:!0,message:l(`smtpRelay.portRequired`)}],children:(0,T.jsx)(re,{min:1,max:65535,className:`full-width`})}),(0,T.jsx)(m.Item,{name:`secure`,label:`SSL/TLS`,valuePropName:`checked`,children:(0,T.jsx)(_,{})}),(0,T.jsx)(m.Item,{name:`username`,label:l(`smtpRelay.username`),children:(0,T.jsx)(h,{autoComplete:`off`})}),(0,T.jsx)(m.Item,{name:`password`,label:l(`smtpRelay.password`),children:(0,T.jsx)(h.Password,{autoComplete:`new-password`})}),(0,T.jsx)(o,{icon:(0,T.jsx)(x,{}),onClick:()=>M.setFieldValue(`password`,O()),children:l(`smtpRelay.generatePassword`)}),(0,T.jsx)(m.Item,{name:`helo`,label:`HELO`,extra:l(`smtpRelay.heloExtra`),style:{marginTop:20},children:(0,T.jsx)(h,{autoComplete:`off`,placeholder:v?.mailHostname||`mail.example.com`})}),(0,T.jsx)(m.Item,{name:`isDefault`,label:l(`smtpRelay.default`),valuePropName:`checked`,children:(0,T.jsx)(_,{})})]})]}),(0,T.jsx)(S,{title:J?.type===`credential`?J.value.username:J?.value.name,width:480,open:!!J,onClose:()=>Y(null),children:J?.type===`credential`?(0,T.jsxs)(p,{bordered:!0,column:1,size:`small`,children:[(0,T.jsx)(p.Item,{label:l(`smtp.username`),children:(0,T.jsx)(s.Text,{code:!0,children:J.value.username})}),(0,T.jsx)(p.Item,{label:l(`smtp.password`),children:(0,T.jsx)(f,{tone:J.value.passwordSet?`success`:`warning`,children:J.value.passwordSet?l(`smtp.passwordSet`):l(`smtp.passwordEmpty`)})}),(0,T.jsx)(p.Item,{label:l(`tokens.createdAt`),children:A(J.value.createdAt)})]}):J?.type===`relay`?(0,T.jsxs)(p,{bordered:!0,column:1,size:`small`,children:[(0,T.jsx)(p.Item,{label:l(`smtpRelay.server`),children:(0,T.jsxs)(s.Text,{code:!0,children:[J.value.host,`:`,J.value.port]})}),(0,T.jsx)(p.Item,{label:`TLS`,children:J.value.secure?`SSL/TLS`:`STARTTLS / Plain`}),(0,T.jsx)(p.Item,{label:l(`smtpRelay.username`),children:J.value.username||`—`}),(0,T.jsx)(p.Item,{label:l(`smtpRelay.password`),children:(0,T.jsx)(f,{tone:J.value.passwordSet?`success`:`warning`,children:J.value.passwordSet?l(`smtpRelay.passwordSet`):l(`smtpRelay.passwordEmpty`)})}),(0,T.jsx)(p.Item,{label:`HELO`,children:J.value.helo||`—`})]}):null})]})}function D({t:e,loading:t,onCancel:n,onSave:r}){return(0,T.jsxs)(a,{style:{display:`flex`,justifyContent:`flex-end`},children:[(0,T.jsx)(o,{onClick:n,children:e(`common.cancel`)}),(0,T.jsx)(o,{type:`primary`,loading:t,onClick:r,children:e(`common.save`)})]})}function O(){let e=new Uint8Array(24);return crypto.getRandomValues(e),btoa(String.fromCharCode(...e)).replace(/[+/=]/g,``).slice(0,28)}function k(e,t,n){return(0,T.jsxs)(a,{children:[(0,T.jsx)(s.Text,{code:!0,className:`inline-code-value`,children:e}),(0,T.jsx)(o,{"aria-label":`${n} ${e}`,icon:(0,T.jsx)(v,{}),onClick:()=>void t(e)})]})}function A(e){return e?new Date(e).toLocaleString():`—`}export{E as default};