import { ConfigReader } from '@backstage/config';

function createLocalBaseUrl(fullUrl) {
  const url = new URL(fullUrl);
  url.protocol = document.location.protocol;
  url.hostname = document.location.hostname;
  url.port = document.location.port;
  return url.toString().replace(/\/$/, "");
}
function overrideBaseUrlConfigs(inputConfigs) {
  const urlConfigReader = ConfigReader.fromConfigs(inputConfigs);
  const appBaseUrl = urlConfigReader.getOptionalString("app.baseUrl");
  const backendBaseUrl = urlConfigReader.getOptionalString("backend.baseUrl");
  let configs = inputConfigs;
  let newBackendBaseUrl = void 0;
  let newAppBaseUrl = void 0;
  if (appBaseUrl && backendBaseUrl) {
    const appOrigin = new URL(appBaseUrl).origin;
    const backendOrigin = new URL(backendBaseUrl).origin;
    if (appOrigin === backendOrigin) {
      const maybeNewBackendBaseUrl = createLocalBaseUrl(backendBaseUrl);
      if (backendBaseUrl !== maybeNewBackendBaseUrl) {
        newBackendBaseUrl = maybeNewBackendBaseUrl;
      }
    }
  }
  if (appBaseUrl) {
    const maybeNewAppBaseUrl = createLocalBaseUrl(appBaseUrl);
    if (appBaseUrl !== maybeNewAppBaseUrl) {
      newAppBaseUrl = maybeNewAppBaseUrl;
    }
  }
  if (newAppBaseUrl || newBackendBaseUrl) {
    configs = configs.concat({
      data: {
        app: newAppBaseUrl && {
          baseUrl: newAppBaseUrl
        },
        backend: newBackendBaseUrl && {
          baseUrl: newBackendBaseUrl
        }
      },
      context: "relative-resolver"
    });
  }
  return configs;
}

export { overrideBaseUrlConfigs };
//# sourceMappingURL=overrideBaseUrlConfigs.esm.js.map
