"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addSiteDomains = addSiteDomains;
const get_prop_1 = require("../../objects/get-prop");
const add_domain_1 = require("./add-domain");
/**
 * Given a Site Model, register the domains with the Domain Service.
 *
 * For Portal, this will return a sparse entry that contains just the portal clientKey
 *
 * This should only be used when creating a site. To update domains related
 * to a site, use the `addDomain` and `removeDomain` functions directly
 *
 * @param {Object} model site model
 * @param {IHubRequestOptions} hubRequestOptions
 */
function addSiteDomains(model, hubRequestOptions) {
    if (hubRequestOptions.isPortal) {
        // For enterprise we don't register the domain, but we do return a sparse entry
        // that contains just the portal clientKey so that the caller can use it.
        return Promise.resolve([{ clientKey: "arcgisonline" }]);
    }
    else {
        const props = ["defaultHostname", "customHostname"];
        return Promise.all(props.reduce((acc, prop) => {
            const hostname = (0, get_prop_1.getProp)(model, `data.values.${prop}`);
            if (hostname) {
                const domainOpts = {
                    hostname,
                    clientKey: model.data.values.clientId,
                    orgId: hubRequestOptions.portalSelf.id,
                    orgTitle: hubRequestOptions.portalSelf.name,
                    orgKey: hubRequestOptions.portalSelf.urlKey,
                    siteId: model.item.id,
                    siteTitle: model.item.title,
                    sslOnly: true,
                };
                acc.push((0, add_domain_1.addDomain)(domainOpts, hubRequestOptions));
            }
            return acc;
        }, []));
    }
}
//# sourceMappingURL=addSiteDomains.js.map