var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);

// src/strategies/passport/passport-insta-oauth2/insta-auth-strategy-factory-provider.ts
import { inject } from "@loopback/core";
import { HttpErrors } from "@loopback/rest";
import { HttpsProxyAgent } from "https-proxy-agent";
import {
  Strategy
} from "passport-instagram";

// src/strategies/keys.ts
import { BindingKey } from "@loopback/core";
var Strategies;
((Strategies2) => {
  let Passport;
  ((Passport2) => {
    Passport2.LOCAL_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.localPassword"
    );
    Passport2.LOCAL_PASSWORD_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.localPassword"
    );
    Passport2.OTP_AUTH_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.otpAuth"
    );
    Passport2.OTP_VERIFIER = BindingKey.create("bleco.passport.verifier.otpAuth");
    Passport2.CLIENT_PASSWORD_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.clientPassword"
    );
    Passport2.OAUTH2_CLIENT_PASSWORD_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.oauth2ClientPassword"
    );
    Passport2.BEARER_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.bearer"
    );
    Passport2.BEARER_TOKEN_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.bearerToken"
    );
    Passport2.RESOURCE_OWNER_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.resourceOwnerPassword"
    );
    Passport2.RESOURCE_OWNER_PASSWORD_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.resourceOwnerPassword"
    );
    Passport2.GOOGLE_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.googleOauth2"
    );
    Passport2.GOOGLE_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.googleOauth2"
    );
    Passport2.AZURE_AD_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.azureAd"
    );
    Passport2.AZURE_AD_VERIFIER = BindingKey.create("bleco.passport.verifier.azureAd");
    Passport2.KEYCLOAK_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.keycloak"
    );
    Passport2.KEYCLOAK_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.keycloak"
    );
    Passport2.INSTAGRAM_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.instagramOauth2"
    );
    Passport2.INSTAGRAM_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.instagramOauth2"
    );
    Passport2.FACEBOOK_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.facebookOauth2"
    );
    Passport2.FACEBOOK_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.facebookOauth2"
    );
    Passport2.APPLE_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.appleOauth2"
    );
    Passport2.APPLE_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.appleOauth2"
    );
    Passport2.COGNITO_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.cognitoOauth2"
    );
    Passport2.COGNITO_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.cognitoOauth2"
    );
    Passport2.SAML_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.saml"
    );
    Passport2.SAML_VERIFIER = BindingKey.create("bleco.passport.verifier.saml");
  })(Passport = Strategies2.Passport || (Strategies2.Passport = {}));
})(Strategies || (Strategies = {}));

// src/strategies/passport/passport-insta-oauth2/insta-auth-strategy-factory-provider.ts
var InstagramAuthStrategyFactoryProvider = class {
  constructor(verifierInstagramAuth) {
    this.verifierInstagramAuth = verifierInstagramAuth;
  }
  value() {
    return (options, verifier) => this.getInstagramAuthStrategyVerifier(options, verifier);
  }
  getInstagramAuthStrategyVerifier(options, verifierPassed) {
    const verifyFn = verifierPassed ?? this.verifierInstagramAuth;
    let strategy;
    if (options && options.passReqToCallback === true) {
      strategy = new Strategy(
        options,
        // eslint-disable-next-line @typescript-eslint/no-misused-promises
        async (req, accessToken, refreshToken, profile, cb) => {
          try {
            const user = await verifyFn(
              accessToken,
              refreshToken,
              profile,
              cb,
              req
            );
            if (!user) {
              throw new HttpErrors.Unauthorized(
                "Invalid Credentials" /* InvalidCredentials */
              );
            }
            cb(void 0, user);
          } catch (err) {
            cb(err);
          }
        }
      );
    } else {
      strategy = new Strategy(
        options,
        // eslint-disable-next-line @typescript-eslint/no-misused-promises
        async (accessToken, refreshToken, profile, cb) => {
          try {
            const user = await verifyFn(accessToken, refreshToken, profile, cb);
            if (!user) {
              throw new HttpErrors.Unauthorized(
                "Invalid Credentials" /* InvalidCredentials */
              );
            }
            cb(void 0, user);
          } catch (err) {
            cb(err);
          }
        }
      );
    }
    this._setupProxy(strategy);
    return strategy;
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  _setupProxy(strategy) {
    let httpsProxyAgent;
    if (process.env["https_proxy"]) {
      httpsProxyAgent = new HttpsProxyAgent(process.env["https_proxy"]);
      strategy._oauth2.setAgent(httpsProxyAgent);
    } else if (process.env["HTTPS_PROXY"]) {
      httpsProxyAgent = new HttpsProxyAgent(process.env["HTTPS_PROXY"]);
      strategy._oauth2.setAgent(httpsProxyAgent);
    }
  }
};
InstagramAuthStrategyFactoryProvider = __decorateClass([
  __decorateParam(0, inject(Strategies.Passport.INSTAGRAM_OAUTH2_VERIFIER))
], InstagramAuthStrategyFactoryProvider);
export {
  InstagramAuthStrategyFactoryProvider
};
//# sourceMappingURL=insta-auth-strategy-factory-provider.js.map