"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignRequestsManager = exports.CreateSignRequestHeaders = exports.GetSignRequestsHeaders = exports.GetSignRequestByIdHeaders = exports.ResendSignRequestHeaders = exports.CancelSignRequestHeaders = exports.CreateSignRequestOptionals = exports.GetSignRequestByIdOptionals = exports.ResendSignRequestOptionals = exports.CancelSignRequestOptionals = void 0;
const signRequest_1 = require("../schemas/signRequest");
const signRequests_1 = require("../schemas/signRequests");
const signRequestCreateRequest_1 = require("../schemas/signRequestCreateRequest");
const network_1 = require("../networking/network");
const fetchOptions_1 = require("../networking/fetchOptions");
const utils_1 = require("../internal/utils");
const utils_2 = require("../internal/utils");
class CancelSignRequestOptionals {
    headers = new CancelSignRequestHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.CancelSignRequestOptionals = CancelSignRequestOptionals;
class ResendSignRequestOptionals {
    headers = new ResendSignRequestHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.ResendSignRequestOptionals = ResendSignRequestOptionals;
class GetSignRequestByIdOptionals {
    headers = new GetSignRequestByIdHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.GetSignRequestByIdOptionals = GetSignRequestByIdOptionals;
class CreateSignRequestOptionals {
    headers = new CreateSignRequestHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.CreateSignRequestOptionals = CreateSignRequestOptionals;
class CancelSignRequestHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.CancelSignRequestHeaders = CancelSignRequestHeaders;
class ResendSignRequestHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.ResendSignRequestHeaders = ResendSignRequestHeaders;
class GetSignRequestByIdHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.GetSignRequestByIdHeaders = GetSignRequestByIdHeaders;
class GetSignRequestsHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.GetSignRequestsHeaders = GetSignRequestsHeaders;
class CreateSignRequestHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.CreateSignRequestHeaders = CreateSignRequestHeaders;
class SignRequestsManager {
    auth;
    networkSession = new network_1.NetworkSession({});
    constructor(fields) {
        if (fields.auth !== undefined) {
            this.auth = fields.auth;
        }
        if (fields.networkSession !== undefined) {
            this.networkSession = fields.networkSession;
        }
    }
    /**
       * Cancels a sign request.
       * @param {string} signRequestId The ID of the signature request.
      Example: "33243242"
       * @param {CancelSignRequestOptionalsInput} optionalsInput
       * @returns {Promise<SignRequest>}
       */
    async cancelSignRequest(signRequestId, optionalsInput = {}) {
        const optionals = new CancelSignRequestOptionals({
            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/sign_requests/', (0, utils_2.toString)(signRequestId), '/cancel'),
            method: 'POST',
            headers: headersMap,
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, signRequest_1.deserializeSignRequest)(response.data),
            rawData: response.data,
        };
    }
    /**
       * Resends a signature request email to all outstanding signers.
       * @param {string} signRequestId The ID of the signature request.
      Example: "33243242"
       * @param {ResendSignRequestOptionalsInput} optionalsInput
       * @returns {Promise<undefined>}
       */
    async resendSignRequest(signRequestId, optionalsInput = {}) {
        const optionals = new ResendSignRequestOptionals({
            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/sign_requests/', (0, utils_2.toString)(signRequestId), '/resend'),
            method: 'POST',
            headers: headersMap,
            responseFormat: 'no_content',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return void 0;
    }
    /**
       * Gets a sign request by ID.
       * @param {string} signRequestId The ID of the signature request.
      Example: "33243242"
       * @param {GetSignRequestByIdOptionalsInput} optionalsInput
       * @returns {Promise<SignRequest>}
       */
    async getSignRequestById(signRequestId, optionalsInput = {}) {
        const optionals = new GetSignRequestByIdOptionals({
            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/sign_requests/', (0, utils_2.toString)(signRequestId)),
            method: 'GET',
            headers: headersMap,
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, signRequest_1.deserializeSignRequest)(response.data),
            rawData: response.data,
        };
    }
    /**
     * Gets signature requests created by a user. If the `sign_files` and/or
     * `parent_folder` are deleted, the signature request will not return in the list.
     * @param {GetSignRequestsQueryParams} queryParams Query parameters of getSignRequests method
     * @param {GetSignRequestsHeadersInput} headersInput Headers of getSignRequests method
     * @param {CancellationToken} cancellationToken Token used for request cancellation.
     * @returns {Promise<SignRequests>}
     */
    async getSignRequests(queryParams = {}, headersInput = new GetSignRequestsHeaders({}), cancellationToken) {
        const headers = new GetSignRequestsHeaders({
            extraHeaders: headersInput.extraHeaders,
        });
        const queryParamsMap = (0, utils_1.prepareParams)({
            ['marker']: (0, utils_2.toString)(queryParams.marker),
            ['limit']: (0, utils_2.toString)(queryParams.limit),
            ['senders']: queryParams.senders
                ? queryParams.senders.map(utils_2.toString).join(',')
                : undefined,
            ['shared_requests']: (0, utils_2.toString)(queryParams.sharedRequests),
        });
        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/sign_requests'),
            method: 'GET',
            params: queryParamsMap,
            headers: headersMap,
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, signRequests_1.deserializeSignRequests)(response.data),
            rawData: response.data,
        };
    }
    /**
     * Creates a signature request. This involves preparing a document for signing and
     * sending the signature request to signers.
     * @param {SignRequestCreateRequest} requestBody Request body of createSignRequest method
     * @param {CreateSignRequestOptionalsInput} optionalsInput
     * @returns {Promise<SignRequest>}
     */
    async createSignRequest(requestBody, optionalsInput = {}) {
        const optionals = new CreateSignRequestOptionals({
            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/sign_requests'),
            method: 'POST',
            headers: headersMap,
            data: (0, signRequestCreateRequest_1.serializeSignRequestCreateRequest)(requestBody),
            contentType: 'application/json',
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, signRequest_1.deserializeSignRequest)(response.data),
            rawData: response.data,
        };
    }
}
exports.SignRequestsManager = SignRequestsManager;
//# sourceMappingURL=signRequests.js.map