import{i as e,n as t}from"./lit-BrYIQVuc.js";import{L as n}from"./index-DDypcwfe.js";var r={weixin:`微信`,wecom:`企业微信`,feishu:`飞书`,dingtalk:`钉钉`},i={weixin:`接入微信对话`,wecom:`接入企业微信对话`,feishu:`接入飞书对话`,dingtalk:`接入钉钉对话`},a={weixin:`https://gw.alicdn.com/imgextra/i4/O1CN01GsAob11fkfDWVIb3R_!!6000000004045-2-tps-400-400.png`,wecom:`https://gw.alicdn.com/imgextra/i1/O1CN01HWtzmr1hkK9beQICJ_!!6000000004315-2-tps-400-400.png`,dingtalk:`https://gw.alicdn.com/imgextra/i4/O1CN01g1u9vB1KdEreWzDdv_!!6000000001186-2-tps-400-400.png`,feishu:`https://gw.alicdn.com/imgextra/i4/O1CN01jsn08m225euyUoaFN_!!6000000007069-2-tps-400-400.png`},o={weixin:{successStatus:`confirmed`,successCredentialKey:`bot_token`,pollInterval:2e3,credentialFields:[`bot_token`]},wecom:{successStatus:`success`,successCredentialKey:`bot_id`,pollInterval:3e3,credentialFields:[`bot_id`,`secret`]},dingtalk:{successStatus:`success`,successCredentialKey:`client_id`,pollInterval:5e3,credentialFields:[`client_id`,`client_secret`]},feishu:{successStatus:``,successCredentialKey:``,pollInterval:0,credentialFields:[]}},s=[`weixin`,`wecom`,`feishu`,`dingtalk`];async function c(e,t){if(!(!e.client||!e.connected)){e.cnChannelsLoading=!0,e.cnChannelsError=null,t==null||t();try{e.cnChannelsData=(await e.client.request(`channels.cn.config.list`,{})).channels??null}catch(t){e.cnChannelsError=String(t)}finally{e.cnChannelsLoading=!1,t==null||t()}}}async function l(e,t,n,r){e.client&&(await e.client.request(`channels.cn.config.set`,{channel:t,config:n}),await c(e,r))}async function u(e,t,n){if(e.client){f(e),e.cnQrcodeChannel=t,e.cnQrcodeImg=null,e.cnQrcodeToken=null,e.cnQrcodeLoading=!0,n==null||n();try{let r=await e.client.request(`channels.cn.qrcode.get`,{channel:t});if(!r.qrcode_img){e.cnQrcodeLoading=!1,n==null||n();return}e.cnQrcodeImg=r.qrcode_img,e.cnQrcodeToken=r.poll_token,d(e,t,r.poll_token,n)}catch{e.cnQrcodeImg=null}finally{e.cnQrcodeLoading=!1,n==null||n()}}}function d(e,t,n,r){let i=o[t],a=async()=>{if(!(!e.client||e.cnQrcodeChannel!==t||e.cnQrcodeToken!==n)){try{let a=await e.client.request(`channels.cn.qrcode.status`,{channel:t,token:n});if(a.status===i.successStatus&&a.credentials[i.successCredentialKey]){e.cnQrcodeImg=null,e.cnQrcodeToken=null,e.cnQrcodeChannel=null;let n={enabled:!0};for(let e of i.credentialFields)a.credentials[e]!==void 0&&(n[e]=a.credentials[e]);e.cnConnecting=t,r==null||r();try{await l(e,t,n,r)}finally{e.cnConnecting=null,r==null||r()}return}if(a.status===`expired`){e.cnQrcodeImg=null,e.cnQrcodeToken=null,r==null||r();return}}catch{}e.cnQrcodePollTimer=setTimeout(a,i.pollInterval)}};e.cnQrcodePollTimer=setTimeout(a,i.pollInterval)}function f(e){e.cnQrcodePollTimer!==null&&(clearTimeout(e.cnQrcodePollTimer),e.cnQrcodePollTimer=null)}function p(t,o,s){var c;let d=!!(((c=o.cnChannelsData)==null?void 0:c[t])??{enabled:!1}).enabled,f=o.cnConnecting===t,p=r[t],m=i[t];return e`
    <div class="cn-channel-card">
      <div class="cn-channel-card__top">
        <div class="cn-channel-card__icon-wrap">
          <img src=${a[t]} alt=${t} class="cn-channel-card__icon" />
        </div>
        <div class="cn-channel-card__info">
          <div class="cn-channel-card__name">${p}</div>
          <div class="cn-channel-card__label">${m}</div>
        </div>
      </div>
      <div class="cn-channel-card__divider"></div>
      <div class="cn-channel-card__footer">
        <div class="cn-channel-card__status">
          <span class="cn-channel-card__dot ${d?`cn-channel-card__dot--on`:``}"></span>
          <span
            class="cn-channel-card__status-text ${d?`cn-channel-card__status-text--on`:``}"
          >
            ${d?`已连接`:`未连接`}
          </span>
        </div>
        ${f?e`<span class="cn-channel-card__spinner">${n.loader}</span>`:d?e`
                <button
                  class="cn-channel-card__btn cn-channel-card__btn--disconnect"
                  @click=${async()=>{o.cnConnecting=t,s==null||s();try{await l(o,t,{enabled:!1},s)}finally{o.cnConnecting=null,s==null||s()}}}
                >
                  断开连接
                </button>
              `:e`
                <button
                  class="cn-channel-card__btn cn-channel-card__btn--connect"
                  @click=${()=>{if(t===`feishu`){var e;let t=((e=o.cnChannelsData)==null?void 0:e.feishu)??{};o.cnFeishuFormAppId=typeof t.app_id==`string`?t.app_id:``,o.cnFeishuFormAppSecret=typeof t.app_secret==`string`?t.app_secret:``,o.cnFeishuOpen=!0,s==null||s()}else u(o,t,s)}}
                >
                  连接
                </button>
              `}
      </div>
    </div>
  `}function m(r,i){let a=r.cnQrcodeChannel;if(!a)return t;let o=a===`weixin`?`微信扫码登录`:a===`wecom`?`企业微信扫码授权`:`钉钉扫码授权`,s=a===`weixin`?`请使用微信扫描二维码登录`:`请使用对应的 App 扫描二维码完成授权`,c=()=>{f(r),r.cnQrcodeChannel=null,r.cnQrcodeImg=null,r.cnQrcodeToken=null,i==null||i()};return e`
    <div class="cn-modal-backdrop" @click=${c}>
      <div class="cn-modal" @click=${e=>e.stopPropagation()}>
        <div class="cn-modal__header">
          <span class="cn-modal__title">${o}</span>
          <button class="cn-modal__close" @click=${c}>${n.x}</button>
        </div>
        <div class="cn-modal__body cn-modal__body--qr">
          ${r.cnQrcodeLoading||!r.cnQrcodeImg?e`
                <div class="cn-qr-placeholder">
                  <span class="cn-qr-placeholder__spinner">${n.loader}</span>
                </div>
              `:e`
                <img
                  src=${`data:image/png;base64,`+r.cnQrcodeImg}
                  alt="QR"
                  class="cn-qr-img"
                />
              `}
          <p class="cn-modal__hint">${s}</p>
          ${r.cnQrcodeImg&&!r.cnQrcodeLoading?e`
                <button class="btn" @click=${()=>{u(r,a,i)}} ?disabled=${r.cnQrcodeLoading}>
                  重新获取二维码
                </button>
              `:t}
        </div>
      </div>
    </div>
  `}function h(r,i){if(!r.cnFeishuOpen)return t;let a=()=>{r.cnFeishuOpen=!1,r.cnFeishuFormAppId=``,r.cnFeishuFormAppSecret=``,i==null||i()};return e`
    <div class="cn-modal-backdrop" @click=${a}>
      <div class="cn-modal" @click=${e=>e.stopPropagation()}>
        <div class="cn-modal__header">
          <span class="cn-modal__title">连接飞书</span>
          <button class="cn-modal__close" @click=${a}>${n.x}</button>
        </div>
        <div class="cn-modal__body">
          <div class="cn-form-field">
            <label class="cn-form-label">App ID</label>
            <input
              class="cn-form-input"
              type="text"
              placeholder="cli_xxxxxxxx"
              .value=${r.cnFeishuFormAppId}
              @input=${e=>{r.cnFeishuFormAppId=e.target.value}}
            />
          </div>
          <div class="cn-form-field">
            <label class="cn-form-label">App Secret</label>
            <input
              class="cn-form-input"
              type="password"
              placeholder="应用密钥"
              .value=${r.cnFeishuFormAppSecret}
              @input=${e=>{r.cnFeishuFormAppSecret=e.target.value}}
            />
          </div>
        </div>
        <div class="cn-modal__footer">
          <button class="btn" @click=${a}>取消</button>
          <button class="btn primary" @click=${async()=>{let e=r.cnFeishuFormAppId.trim(),t=r.cnFeishuFormAppSecret.trim();if(!(!e||!t)){r.cnFeishuSaving=!0,i==null||i();try{await l(r,`feishu`,{enabled:!0,app_id:e,app_secret:t},i),r.cnFeishuOpen=!1,r.cnFeishuFormAppId=``,r.cnFeishuFormAppSecret=``}finally{r.cnFeishuSaving=!1,i==null||i()}}}} ?disabled=${r.cnFeishuSaving}>
            ${r.cnFeishuSaving?`保存中…`:`保存并连接`}
          </button>
        </div>
      </div>
    </div>
  `}function g(r,i){return r.activePanelId===`cnChannels`?(!r.cnChannelsData&&!r.cnChannelsLoading&&c(r,i),e`
    <div class="cn-channels-panel">
      <div class="cn-channels-panel__header">
        <div class="cn-channels-panel__header-left">
          <span class="cn-channels-panel__title">消息渠道</span>
          <span class="cn-channels-panel__sub">连接微信、企业微信等消息平台</span>
        </div>
        <button class="cn-channels-panel__close" @click=${()=>{f(r),r.activePanelId=null,r.cnQrcodeChannel=null,r.cnQrcodeImg=null,r.cnQrcodeToken=null,i==null||i()}} aria-label="关闭渠道面板">
          ${n.x}
        </button>
      </div>

      <div class="cn-channels-panel__body">
        ${r.cnChannelsLoading&&!r.cnChannelsData?e`<div class="cn-channels-panel__loading">${n.loader} 加载中…</div>`:r.cnChannelsError?e`<div class="callout danger">${r.cnChannelsError}</div>`:e`
                <div class="cn-channels-grid">
                  ${s.map(e=>p(e,r,i))}
                </div>
                <p class="cn-channels-panel__note">使用多渠道需保持 SealSeek 客户端在线状态</p>
              `}
      </div>
    </div>

    ${m(r,i)}
    ${h(r,i)}
  `):t}export{c as loadCnChannels,g as renderCnChannelsPanel,u as startCnQrcode,f as stopCnQrcodePoll};
//# sourceMappingURL=channels.cn-Dno2PIIq.js.map