"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureUniqueDomainName = ensureUniqueDomainName;
const get_unique_domain_name_portal_1 = require("./get-unique-domain-name-portal");
const get_unique_domain_name_1 = require("./get-unique-domain-name");
const _ensure_safe_domain_length_1 = require("./_ensure-safe-domain-length");
const strip_protocol_1 = require("../../urls/strip-protocol");
const api_1 = require("../../api");
/**
 * Given a subdomain, ensure that we have a unique hostname
 * incrementing if needed
 * @param {String} subdomain Subdomain to unique-ify
 * @param {IHubRequestOptions} hubRequestOptions
 */
function ensureUniqueDomainName(subdomain, hubRequestOptions) {
    let prms;
    if (hubRequestOptions.isPortal) {
        prms = (0, get_unique_domain_name_portal_1.getUniqueDomainNamePortal)(subdomain, hubRequestOptions);
    }
    else {
        const baseDomain = `${hubRequestOptions.portalSelf.urlKey}.${(0, strip_protocol_1.stripProtocol)((0, api_1.getHubApiUrl)(hubRequestOptions))}`;
        prms = (0, get_unique_domain_name_1.getUniqueDomainName)(subdomain, baseDomain, hubRequestOptions);
    }
    return prms.then((uniqueDomain) => {
        return (0, _ensure_safe_domain_length_1._ensureSafeDomainLength)(uniqueDomain, hubRequestOptions.portalSelf.urlKey);
    });
}
//# sourceMappingURL=ensure-unique-domain-name.js.map