"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailAliasesManager = exports.DeleteUserEmailAliasByIdHeaders = exports.CreateUserEmailAliasHeaders = exports.GetUserEmailAliasesHeaders = exports.DeleteUserEmailAliasByIdOptionals = exports.CreateUserEmailAliasOptionals = exports.GetUserEmailAliasesOptionals = void 0;
exports.serializeCreateUserEmailAliasRequestBody = serializeCreateUserEmailAliasRequestBody;
exports.deserializeCreateUserEmailAliasRequestBody = deserializeCreateUserEmailAliasRequestBody;
const emailAliases_1 = require("../schemas/emailAliases");
const emailAlias_1 = require("../schemas/emailAlias");
const errors_1 = require("../box/errors");
const network_1 = require("../networking/network");
const fetchOptions_1 = require("../networking/fetchOptions");
const utils_1 = require("../internal/utils");
const utils_2 = require("../internal/utils");
const json_1 = require("../serialization/json");
const json_2 = require("../serialization/json");
class GetUserEmailAliasesOptionals {
    headers = new GetUserEmailAliasesHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.GetUserEmailAliasesOptionals = GetUserEmailAliasesOptionals;
class CreateUserEmailAliasOptionals {
    headers = new CreateUserEmailAliasHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.CreateUserEmailAliasOptionals = CreateUserEmailAliasOptionals;
class DeleteUserEmailAliasByIdOptionals {
    headers = new DeleteUserEmailAliasByIdHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.DeleteUserEmailAliasByIdOptionals = DeleteUserEmailAliasByIdOptionals;
class GetUserEmailAliasesHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.GetUserEmailAliasesHeaders = GetUserEmailAliasesHeaders;
class CreateUserEmailAliasHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.CreateUserEmailAliasHeaders = CreateUserEmailAliasHeaders;
class DeleteUserEmailAliasByIdHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.DeleteUserEmailAliasByIdHeaders = DeleteUserEmailAliasByIdHeaders;
class EmailAliasesManager {
    auth;
    networkSession = new network_1.NetworkSession({});
    constructor(fields) {
        if (fields.auth !== undefined) {
            this.auth = fields.auth;
        }
        if (fields.networkSession !== undefined) {
            this.networkSession = fields.networkSession;
        }
    }
    /**
       * Retrieves all email aliases for a user. The collection
       * does not include the primary login for the user.
       * @param {string} userId The ID of the user.
      Example: "12345"
       * @param {GetUserEmailAliasesOptionalsInput} optionalsInput
       * @returns {Promise<EmailAliases>}
       */
    async getUserEmailAliases(userId, optionalsInput = {}) {
        const optionals = new GetUserEmailAliasesOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.baseUrl, '/2.0/users/', (0, utils_2.toString)(userId), '/email_aliases'),
            method: 'GET',
            headers: headersMap,
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, emailAliases_1.deserializeEmailAliases)(response.data),
            rawData: response.data,
        };
    }
    /**
       * Adds a new email alias to a user account..
       * @param {string} userId The ID of the user.
      Example: "12345"
       * @param {CreateUserEmailAliasRequestBody} requestBody Request body of createUserEmailAlias method
       * @param {CreateUserEmailAliasOptionalsInput} optionalsInput
       * @returns {Promise<EmailAlias>}
       */
    async createUserEmailAlias(userId, requestBody, optionalsInput = {}) {
        const optionals = new CreateUserEmailAliasOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.baseUrl, '/2.0/users/', (0, utils_2.toString)(userId), '/email_aliases'),
            method: 'POST',
            headers: headersMap,
            data: serializeCreateUserEmailAliasRequestBody(requestBody),
            contentType: 'application/json',
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, emailAlias_1.deserializeEmailAlias)(response.data),
            rawData: response.data,
        };
    }
    /**
       * Removes an email alias from a user.
       * @param {string} userId The ID of the user.
      Example: "12345"
       * @param {string} emailAliasId The ID of the email alias.
      Example: "23432"
       * @param {DeleteUserEmailAliasByIdOptionalsInput} optionalsInput
       * @returns {Promise<undefined>}
       */
    async deleteUserEmailAliasById(userId, emailAliasId, optionalsInput = {}) {
        const optionals = new DeleteUserEmailAliasByIdOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.baseUrl, '/2.0/users/', (0, utils_2.toString)(userId), '/email_aliases/', (0, utils_2.toString)(emailAliasId)),
            method: 'DELETE',
            headers: headersMap,
            responseFormat: 'no_content',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return void 0;
    }
}
exports.EmailAliasesManager = EmailAliasesManager;
function serializeCreateUserEmailAliasRequestBody(val) {
    return { ['email']: val.email };
}
function deserializeCreateUserEmailAliasRequestBody(val) {
    if (!(0, json_2.sdIsMap)(val)) {
        throw new errors_1.BoxSdkError({
            message: 'Expecting a map for "CreateUserEmailAliasRequestBody"',
        });
    }
    if (val.email == void 0) {
        throw new errors_1.BoxSdkError({
            message: 'Expecting "email" of type "CreateUserEmailAliasRequestBody" to be defined',
        });
    }
    if (!(0, json_1.sdIsString)(val.email)) {
        throw new errors_1.BoxSdkError({
            message: 'Expecting string for "email" of type "CreateUserEmailAliasRequestBody"',
        });
    }
    const email = val.email;
    return { email: email };
}
//# sourceMappingURL=emailAliases.js.map