"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchOrg = fetchOrg;
const get_prop_1 = require("../objects/get-prop");
const getPortalBaseFromOrgUrl_1 = require("../urls/getPortalBaseFromOrgUrl");
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
/**
 * Fetches the portal for a given org.
 *
 * @param orgId
 * @param options request options
 * @returns
 */
function fetchOrg(orgId, options) {
    let orgPortalUrl = (0, get_prop_1.getProp)(options, "portal") ||
        (0, get_prop_1.getProp)(options, "authentication.portal") ||
        "www.arcgis.com";
    // if we got the value from authentication, we need to strip off the
    // "/sharing/rest" part of the url to get the base portal url because
    // getPortalBaseFromOrgUrl does not know how to handle enterprise urls.
    orgPortalUrl = orgPortalUrl.split("/sharing/rest")[0];
    // In order to get the correct response, we must pass options.portal
    // as a base portal url (e.g., www.arcgis.com, qaext.arcgis.com, etc)
    // **not** an org portal (i.e. org.maps.arcgis.com).
    const basePortalUrl = `${(0, getPortalBaseFromOrgUrl_1.getPortalBaseFromOrgUrl)(orgPortalUrl)}/sharing/rest`;
    return (0, arcgis_rest_portal_1.getPortal)(orgId, Object.assign(Object.assign({}, options), { portal: basePortalUrl }));
}
//# sourceMappingURL=fetch-org.js.map