import{Fo as e}from"./auth-profiles-qXC1L8hs.js";import{t}from"./command-format-CLxIPuLR.js";import{c as n,g as r}from"./session-key-C7F_iqYg.js";import{lt as i,r as a,t as o,x as s,y as c}from"./plugins-CCU8jDti.js";import{n as l}from"./issue-format-CXMe5xzG.js";function u(e){let t=e.accountId?.trim()||`default`;return[d(e),t].join(`|`)}function d(e){let t=Array.isArray(e.roles)?Array.from(new Set(e.roles.map(e=>e.trim()).filter(Boolean).toSorted())):[];return[e.channel,e.peer?.kind??``,e.peer?.id??``,e.guildId??``,e.teamId??``,t.join(`,`)].join(`|`)}function f(e){return!e.incoming.match.accountId?.trim()||e.existing.match.accountId?.trim()||n(e.existing.agentId)!==e.normalizedIncomingAgentId?!1:d(e.existing.match)===d(e.incoming.match)}function p(e){let t=e.match,n=[t.channel];return t.accountId&&n.push(`accountId=${t.accountId}`),t.peer&&n.push(`peer=${t.peer.kind}:${t.peer.id}`),t.guildId&&n.push(`guild=${t.guildId}`),t.teamId&&n.push(`team=${t.teamId}`),n.join(` `)}function m(e,t){let r=[...s(e)],i=(e.bindings??[]).filter(e=>!c(e)),a=new Map;for(let e of r){let t=u(e.match);a.has(t)||a.set(t,n(e.agentId))}let o=[],l=[],d=[],p=[];for(let e of t){let t=n(e.agentId),i=u(e.match),s=a.get(i);if(s){s===t?d.push(e):p.push({binding:e,existingAgentId:s});continue}let c=r.findIndex(n=>f({existing:n,incoming:e,normalizedIncomingAgentId:t}));if(c>=0){let n=r[c];if(!n)continue;let i=u(n.match),o={...n,agentId:t,match:{...n.match,accountId:e.match.accountId?.trim()}};r[c]=o,a.delete(i),a.set(u(o.match),t),l.push(o);continue}a.set(i,t),o.push({...e,agentId:t})}return o.length===0&&l.length===0?{config:e,added:o,updated:l,skipped:d,conflicts:p}:{config:{...e,bindings:[...r,...o,...i]},added:o,updated:l,skipped:d,conflicts:p}}function h(e,t){let r=s(e),i=(e.bindings??[]).filter(e=>!c(e)),a=new Set,o=[],l=[],d=[];for(let e of t){let t=n(e.agentId),i=u(e.match),s=-1,c=null;for(let e=0;e<r.length;e+=1){if(a.has(e))continue;let o=r[e];if(!o||u(o.match)!==i)continue;let l=n(o.agentId);if(l===t){s=e;break}c=l}if(s>=0){let e=r[s];e&&(a.add(s),o.push(e));continue}if(c){d.push({binding:e,existingAgentId:c});continue}l.push(e)}if(a.size===0)return{config:e,removed:o,missing:l,conflicts:d};let f=[...r.filter((e,t)=>!a.has(t)),...i];return{config:{...e,bindings:f.length>0?f:void 0},removed:o,missing:l,conflicts:d}}function g(e,t){let n=o(t);return n?i({plugin:n,cfg:e}):r}function _(e){let t=e.explicitAccountId?.trim();if(t)return t;let n=o(e.channel),r=n?.setup?.resolveBindingAccountId?.({cfg:e.config,agentId:e.agentId});if(r?.trim())return r.trim();if(n?.meta.forceAccountBinding)return g(e.config,e.channel)}function v(e){let t=[],r=n(e.agentId);for(let n of e.selection){let i={channel:n},a=_({channel:n,config:e.config,agentId:r,explicitAccountId:e.accountIds?.[n]});a&&(i.accountId=a),t.push({type:`route`,agentId:r,match:i})}return t}function y(e){let t=[],r=[],i=e.specs??[],o=n(e.agentId);for(let n of i){let i=n?.trim();if(!i)continue;let[s,c]=i.split(`:`,2),l=a(s);if(!l){r.push(`Unknown channel "${s}".`);continue}let u=c?.trim();if(c!==void 0&&!u){r.push(`Invalid binding "${i}" (empty account id).`);continue}u=_({channel:l,config:e.config,agentId:o,explicitAccountId:u});let d={channel:l};u&&(d.accountId=u),t.push({type:`route`,agentId:o,match:d})}return{bindings:t,errors:r}}async function b(n){let r=await e();if(r.exists&&!r.valid){let e=r.issues.length>0?l(r.issues,`-`).join(`
`):`Unknown validation issue.`;return n.error(`Config invalid:\n${e}`),n.error(`Fix the config or run ${t(`openclaw doctor`)}.`),n.exit(1),null}return r.config}export{y as a,p as i,m as n,h as o,v as r,b as t};
