"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isServicesDirectoryDisabled = void 0;
const arcgis_rest_feature_service_1 = require("@esri/arcgis-rest-feature-service");
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
/**
 * Checks if a server's services directory is disabled. Consider hoisting this to RESTJS
 * @param idOrItem A feature service ID or item
 * @param requestOptions Request options
 * @returns Promise that resolves boolean
 */
const isServicesDirectoryDisabled = async (idOrItem, requestOptions) => {
    let disabled;
    try {
        const item = typeof idOrItem === "string"
            ? await (0, arcgis_rest_portal_1.getItem)(idOrItem, requestOptions)
            : idOrItem;
        if (item.url) {
            let url = (0, arcgis_rest_feature_service_1.parseServiceUrl)(item.url);
            if (item.access !== "public" && requestOptions.authentication) {
                const token = await requestOptions.authentication.getToken(item.url, requestOptions);
                if (token) {
                    url = `${url}?token=${token}`;
                }
            }
            // We use rawResponse to get the status code
            // without having to parse the response body
            const raw = (await (0, arcgis_rest_request_1.request)(url, Object.assign(Object.assign({}, requestOptions), { rawResponse: true, httpMethod: "GET" })));
            disabled = raw.status !== 200;
        }
        else {
            disabled = true;
        }
    }
    catch (_e) {
        disabled = true;
    }
    return disabled;
};
exports.isServicesDirectoryDisabled = isServicesDirectoryDisabled;
//# sourceMappingURL=is-services-directory-disabled.js.map