import { SdkConfig } from "@komi-app/auth-sdk";
import { FLAGS, isOn } from "@komi-app/flags-sdk";

let { komi } = window as any;

// temporary loading for tests
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
  window.env = process.env as any;
}

if (!komi) {
  komi = {};

  Object.defineProperty(window, "komi", {
    value: komi,
    configurable: false,
  });
}

const protocol = location.port === "2000" ? "http://" : "https://";

const apiPrefix = "i"; // TODO: centralize and parametrize

const gatewayUrl = (service: string) => {
  const root = window.env.REACT_APP_ROOT_DOMAIN;

  return `${protocol}${apiPrefix}.${root.replace(/\:[\d]+/, "")}/${service}`;
};

// list of migrated services
const gatewayReady = ["profiles", "auth", "shopify", "affiliate", "analytics"];

// list of services that should be always use the gateway, no matter what flags are set
const gatewayForced = ["analytics"];

const services = {
  assetsService: {
    url: window.env.REACT_APP_ASSET_SERVICE_URL,
    gatewayUrl: gatewayUrl("assets"),
  },
  analytics: {
    url: window.env.REACT_APP_API_ANALYTICS_URL,
    gatewayUrl: gatewayUrl("analytics"),
  },
  auth: {
    url: window.env.REACT_APP_API_AUTH_URL,
    gatewayUrl: gatewayUrl("auth"),
  },
  sales: {
    url: window.env.REACT_APP_API_SALES_URL,
    gatewayUrl: gatewayUrl("sales"),
  },
  profiles: {
    url: window.env.REACT_APP_API_PROFILES_URL,
    gatewayUrl: gatewayUrl("profiles"),
  },
  messaging: {
    url: window.env.REACT_APP_MESSAGING_SERVICE_URL,
    gatewayUrl: gatewayUrl("messaging"),
  },
  edcm: {
    url: window.env.REACT_APP_API_EDCM_URL,
    gatewayUrl: gatewayUrl("edcm"),
  },
  fans: {
    url: window.env.REACT_APP_API_FANS_URL,
    gatewayUrl: gatewayUrl("fans"),
  },
  affiliate: {
    url: window.env.REACT_APP_AFFILIATE_URL,
    gatewayUrl: gatewayUrl("affiliate"),
  },
  shopify: {
    key: window.env.REACT_APP_SHOPIFY_KEY,
    url: window.env.REACT_APP_SHOPIFY_URL,
    gatewayUrl: gatewayUrl("shops"),
  },
};

const config = {
  ...services,
  version: window.env.REACT_APP_SERVICE_VERSION,
  admin: {
    url: window.env.REACT_APP_ADMIN_URL,
  },
  assets: {
    staticUrl: window.env.REACT_APP_STATIC_ASSETS_URL,
  },
  api: {
    url: window.env.REACT_APP_API_URL,
  },
  agora: {
    id: window.env.REACT_APP_AGORA_APP_ID,
  },
  bandsintown: {
    id: window.env.REACT_APP_BANDSINTOWN_APP_ID,
    mask: window.env.REACT_APP_BANDSINTOWN_APP_ID_BASE,
    url: window.env.REACT_APP_BANDSINTOWN_API_URL,
  },
  ci: {
    build: window.env.REACT_APP_CI_BUILD,
    domain: window.env.REACT_APP_CI_ROOT_DOMAIN,
  },
  client: {
    url: window.env.REACT_APP_CONSUMER_URL,
  },
  firebase: {
    apiKey: window.env.REACT_APP_FIREBASE_API_KEY,
    databaseUrl: window.env.REACT_APP_FIREBASE_DATABASE_URL,
  },
  flags: {
    host: window.env.REACT_APP_FEATURE_FLAG_HOST,
    key: window.env.REACT_APP_FEATURE_FLAG_KEY,
  },
  logRocket: {
    id: window.env.REACT_APP_LOG_ROCKET,
  },
  segment: {
    id: window.env.REACT_APP_SEGMENT_APP_ID,
  },
  service: {
    domain: window.env.REACT_APP_ROOT_DOMAIN,
    env: window.env.REACT_APP_NODE_ENV,
    local: window.env.REACT_APP_LOCAL,
    name: window.env.REACT_APP_SERVICE_NAME,
    url: window.env.REACT_APP_PUBLIC_URL,
    assetUrl: window.env.REACT_APP_PUBLIC_URL,
    version: window.env.REACT_APP_SERVICE_VERSION,
  },

  ryeShopify: {
    graphqlApiUrl: window.env.REACT_APP_RYE_GRAPHQL_API_URL,
  },
  shopMyShelf: {
    url: window.env.REACT_APP_SHOP_MY_SHELF_URL,
  },
  spotify: {
    url: window.env.REACT_APP_SPOTIFY_URL,
  },
  stream: {
    key: window.env.REACT_APP_MESSAGE_ENCRYPTION_KEY,
  },
  stripe: {
    key: window.env.REACT_APP_STRIPE_KEY,
  },
  userleap: {
    id: window.env.REACT_APP_USERLEAP_APP_ID,
  },
  OneSchema: {
    clientId: window.env.REACT_APP_ONESCHEMA_CLIENT_ID,
  },
  google: {
    url: window.env.REACT_APP_GOOGLE_API_URL,
    apiKey: window.env.REACT_APP_GOOGLE_API_KEY,
  },
  cello: {
    url: window.env.REACT_APP_CELLO_URL,
  },
  intercom: {
    appId: window.env.REACT_APP_INTERCOM_APP_ID,
  },
  images: {
    cdn: window.env.REACT_APP_IMAGE_CDN_URL,
    bucket: window.env.REACT_APP_IMAGE_BUCKET,
  },
};

if (!komi.talent) {
  Object.defineProperty(komi, "talent", {
    value: Object.freeze(config),
    configurable: false,
  });
}

export default config;

export const getSDKConfig = (key: keyof typeof services): SdkConfig => {
  const { name, version } = config.service;
  const isGatewayEnabled =
    isOn(FLAGS.USE_API_GATEWAY) && gatewayReady.includes(key);
  const useGateway = isGatewayEnabled || gatewayForced.includes(key);

  const authUrl =
    (isGatewayEnabled ? config.auth.gatewayUrl : config.auth.url) || "";

  const targetUrl = gatewayReady.includes(key)
    ? useGateway
      ? services[key].gatewayUrl
      : services[key].url
    : services[key].url;

  return {
    targetUrl,
    serviceName: name,
    serviceVersion: version,
    authUrl,
  };
};
