// src/tangle/index.ts
function buildConsentUrl(input) {
  const base = input.endpoint.replace(/\/+$/, "");
  const params = new URLSearchParams({
    client_id: input.clientId,
    redirect_uri: input.redirectUri,
    scope: input.scopes.join(" "),
    state: input.state,
    response_type: "code"
  });
  if (input.connectionId) params.set("connection_id", input.connectionId);
  return `${base}/cross-site/app-consent?${params.toString()}`;
}
function createBrokerTokenProvider(opts) {
  const now = opts.now ?? (() => Date.now());
  const skew = opts.refreshSkewMs ?? 3e4;
  let cached = null;
  let inflight = null;
  async function mint() {
    const t = await opts.client.mintBrokerToken({
      clientId: opts.clientId,
      clientSecret: opts.clientSecret,
      grantId: opts.grantId,
      ttlSeconds: opts.ttlSeconds
    });
    cached = { token: t.accessToken, expiresAt: now() + t.expiresIn * 1e3 };
    return t.accessToken;
  }
  return {
    async getToken() {
      if (cached && now() < cached.expiresAt - skew) return cached.token;
      if (inflight) return inflight;
      inflight = mint().finally(() => {
        inflight = null;
      });
      return inflight;
    },
    invalidate() {
      cached = null;
    }
  };
}

export {
  buildConsentUrl,
  createBrokerTokenProvider
};
//# sourceMappingURL=chunk-YGUNTIT5.js.map