'use strict';

var errors = require('@backstage/errors');

function reqRes(req) {
  const res = req.res;
  if (!res) {
    throw new Error("No response object found in request");
  }
  return res;
}
const defaultTransform = ({ requested, granted, required, additional }) => {
  return [...requested, ...granted, ...required, ...additional];
};
function splitScope(scope) {
  if (!scope) {
    return [];
  }
  return scope.split(/[\s|,]/).filter(Boolean);
}
class CookieScopeManager {
  constructor(scopeTransform, cookieManager) {
    this.scopeTransform = scopeTransform;
    this.cookieManager = cookieManager;
  }
  static create(options) {
    const { authenticator, config } = options;
    const shouldPersistScopes = authenticator.scopes?.persist ?? authenticator.shouldPersistScopes ?? false;
    const configScopes = typeof config?.getOptional("additionalScopes") === "string" ? splitScope(config.getString("additionalScopes")) : config?.getOptionalStringArray("additionalScopes") ?? [];
    const transform = authenticator?.scopes?.transform ?? defaultTransform;
    const additional = [...configScopes, ...options.additionalScopes ?? []];
    const required = authenticator?.scopes?.required ?? [];
    return new CookieScopeManager(
      (requested, granted) => Array.from(
        new Set(transform({ required, additional, requested, granted }))
      ).join(" "),
      shouldPersistScopes ? options.cookieManager : void 0
    );
  }
  async start(req) {
    const requestScope = splitScope(req.query.scope?.toString());
    const grantedScope = splitScope(this.cookieManager?.getGrantedScopes(req));
    const scope = this.scopeTransform(requestScope, grantedScope);
    if (this.cookieManager) {
      return {
        scope,
        scopeState: { scope }
      };
    }
    return { scope };
  }
  async handleCallback(req, ctx) {
    if (!this.cookieManager) {
      return Array.from(splitScope(ctx.result.session.scope)).join(" ");
    }
    const scope = ctx.state.scope;
    if (scope === void 0) {
      throw new errors.AuthenticationError("No scope found in OAuth state");
    }
    this.cookieManager.setGrantedScopes(reqRes(req), scope, ctx.origin);
    return scope;
  }
  async clear(req) {
    if (this.cookieManager) {
      this.cookieManager.removeGrantedScopes(reqRes(req), req.get("origin"));
    }
  }
  async refresh(req) {
    const requestScope = splitScope(req.query.scope?.toString());
    const grantedScope = splitScope(this.cookieManager?.getGrantedScopes(req));
    const scope = this.scopeTransform(requestScope, grantedScope);
    return {
      scope,
      commit: async (result) => {
        if (this.cookieManager) {
          this.cookieManager.setGrantedScopes(
            reqRes(req),
            scope,
            req.get("origin")
          );
          return scope;
        }
        return Array.from(splitScope(result.session.scope)).join(" ");
      }
    };
  }
}

exports.CookieScopeManager = CookieScopeManager;
//# sourceMappingURL=CookieScopeManager.cjs.js.map
