"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleSubdomainChange = void 0;
exports.getSubdomainKeyword = getSubdomainKeyword;
exports.setSubdomainKeyword = setSubdomainKeyword;
const ArcGISContext_1 = require("../../ArcGISContext");
const TYPEKEYWORD_SUBDOMAIN_PREFIX = "hubsubdomain";
/**
 * Get the subdomain type keyword
 * @param subdomain The subdomain to get the keyword for
 * @returns The subdomain type keyword
 */
function getSubdomainKeyword(subdomain) {
    return `${TYPEKEYWORD_SUBDOMAIN_PREFIX}|${subdomain.toLowerCase()}`;
}
/**
 * Adds/Updates the subdomain typekeyword
 * Returns a new array of keywords
 *
 * @param typeKeywords A collection of typekeywords
 * @param subdomain The subdomain to add/update
 * @returns An updated collection of typekeywords
 */
function setSubdomainKeyword(typeKeywords, subdomain) {
    // remove subdomain entry from array
    const updatedTypekeywords = typeKeywords.filter((entry) => !entry.startsWith(`${TYPEKEYWORD_SUBDOMAIN_PREFIX}|`));
    // now add it
    updatedTypekeywords.push([TYPEKEYWORD_SUBDOMAIN_PREFIX, subdomain.toLowerCase()].join("|"));
    return updatedTypekeywords;
}
/**
 * Update the site properties related to subdomain
 * after the subdomain has been changed.
 * @param site The site to update
 * @param requestOptions
 */
const handleSubdomainChange = (site, requestOptions) => {
    // update the type keyword
    site.typeKeywords = setSubdomainKeyword(site.typeKeywords, site.subdomain);
    // update the URL
    site.url = `${requestOptions.authentication.portal.replace(`/sharing/rest`, ArcGISContext_1.ENTERPRISE_SITES_PATH)}/#/${site.subdomain}`;
};
exports.handleSubdomainChange = handleSubdomainChange;
//# sourceMappingURL=subdomains.js.map