"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WmsUtilities = void 0;
const Request_1 = require("../../request/Request");
const utils_1 = require("../../request/utils");
/** @packageDocumentation
 * @module Tiles
 */
/** @internal */
class WmsUtilities {
    static getBaseUrl(url) {
        const lastIndex = url.lastIndexOf("?");
        return lastIndex > 0 ? url.slice(0, lastIndex) : url;
    }
    /**
   * fetch XML from HTTP request
   * @param url server URL to address the request
   * @internal
   */
    static async fetchXml(url, credentials) {
        let headers;
        if (credentials && credentials.user && credentials.password) {
            headers = new Headers();
            (0, utils_1.setBasicAuthorization)(headers, credentials);
        }
        let response = await fetch(url, { method: "GET", headers });
        if (!credentials && response.status === 401 && (0, utils_1.headersIncludeAuthMethod)(response.headers, ["ntlm", "negotiate"])) {
            // We got a http 401 challenge, lets try SSO (i.e. Windows Authentication)
            response = await fetch(url, { method: "GET", credentials: "include" });
        }
        if (response.status !== 200)
            throw new Request_1.HttpResponseError(response.status, await response.text());
        return response.text();
    }
}
exports.WmsUtilities = WmsUtilities;
//# sourceMappingURL=WmsUtilities.js.map