"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addDomain = addDomain;
const _get_auth_header_1 = require("./_get-auth-header");
const _get_domain_service_url_1 = require("./_get-domain-service-url");
const _check_status_and_parse_json_1 = require("./_check-status-and-parse-json");
/**
 * Create an entry in the domain system
 * @param {IHubDomain} domainEntry Domain hash to be stored
 * @param {IHubRequestOptions} hubRequestOptions
 */
function addDomain(domainEntry, hubRequestOptions) {
    if (hubRequestOptions.isPortal) {
        throw new Error(`addDomain is not available in ArcGIS Enterprise. Instead, edit the hubdomain typekeyword on the item`);
    }
    const headers = (0, _get_auth_header_1._getAuthHeader)(hubRequestOptions);
    headers["Content-Type"] = "application/json";
    const url = `${(0, _get_domain_service_url_1._getDomainServiceUrl)(hubRequestOptions.hubApiUrl)}`;
    // handle case of siteTitle being numeric
    const title = domainEntry.siteTitle;
    if (typeof title === "number") {
        domainEntry.siteTitle = title.toString();
    }
    // update client key to empty string if we have one, as it won't pass schema validation
    if (domainEntry.clientKey) {
        domainEntry.clientKey = "";
    }
    return fetch(url, {
        method: "POST",
        headers,
        mode: "cors",
        body: JSON.stringify(domainEntry),
    }).then(_check_status_and_parse_json_1._checkStatusAndParseJson);
}
//# sourceMappingURL=add-domain.js.map