// This file was auto-generated by Fern from our API Definition.

import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js";
import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js";
import { mergeHeaders } from "../../../../core/headers.js";
import * as core from "../../../../core/index.js";
import * as environments from "../../../../environments.js";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js";
import * as errors from "../../../../errors/index.js";
import type * as Vapi from "../../../index.js";

export declare namespace CampaignsClient {
    export type Options = BaseClientOptions;

    export interface RequestOptions extends BaseRequestOptions {}
}

export class CampaignsClient {
    protected readonly _options: NormalizedClientOptionsWithAuth<CampaignsClient.Options>;

    constructor(options: CampaignsClient.Options) {
        this._options = normalizeClientOptionsWithAuth(options);
    }

    /**
     * @param {Vapi.CampaignControllerFindAllRequest} request
     * @param {CampaignsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.campaigns.campaignControllerFindAll()
     */
    public campaignControllerFindAll(
        request: Vapi.CampaignControllerFindAllRequest = {},
        requestOptions?: CampaignsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.CampaignPaginatedResponse> {
        return core.HttpResponsePromise.fromPromise(this.__campaignControllerFindAll(request, requestOptions));
    }

    private async __campaignControllerFindAll(
        request: Vapi.CampaignControllerFindAllRequest = {},
        requestOptions?: CampaignsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.CampaignPaginatedResponse>> {
        const {
            id,
            status,
            page,
            sortOrder,
            limit,
            createdAtGt,
            createdAtLt,
            createdAtGe,
            createdAtLe,
            updatedAtGt,
            updatedAtLt,
            updatedAtGe,
            updatedAtLe,
        } = request;
        const _queryParams: Record<string, unknown> = {
            id,
            status: status != null ? status : undefined,
            page,
            sortOrder: sortOrder != null ? sortOrder : undefined,
            limit,
            createdAtGt: createdAtGt != null ? createdAtGt : undefined,
            createdAtLt: createdAtLt != null ? createdAtLt : undefined,
            createdAtGe: createdAtGe != null ? createdAtGe : undefined,
            createdAtLe: createdAtLe != null ? createdAtLe : undefined,
            updatedAtGt: updatedAtGt != null ? updatedAtGt : undefined,
            updatedAtLt: updatedAtLt != null ? updatedAtLt : undefined,
            updatedAtGe: updatedAtGe != null ? updatedAtGe : undefined,
            updatedAtLe: updatedAtLe != null ? updatedAtLe : undefined,
        };
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                "campaign",
            ),
            method: "GET",
            headers: _headers,
            queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.CampaignPaginatedResponse, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/campaign");
    }

    /**
     * @param {Vapi.CreateCampaignDto} request
     * @param {CampaignsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.campaigns.campaignControllerCreate({
     *         name: "Q2 Sales Campaign"
     *     })
     */
    public campaignControllerCreate(
        request: Vapi.CreateCampaignDto,
        requestOptions?: CampaignsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Campaign> {
        return core.HttpResponsePromise.fromPromise(this.__campaignControllerCreate(request, requestOptions));
    }

    private async __campaignControllerCreate(
        request: Vapi.CreateCampaignDto,
        requestOptions?: CampaignsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Campaign>> {
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                "campaign",
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: request,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Campaign, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/campaign");
    }

    /**
     * @param {Vapi.CampaignControllerFindOneRequest} request
     * @param {CampaignsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.campaigns.campaignControllerFindOne({
     *         id: "id"
     *     })
     */
    public campaignControllerFindOne(
        request: Vapi.CampaignControllerFindOneRequest,
        requestOptions?: CampaignsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Campaign> {
        return core.HttpResponsePromise.fromPromise(this.__campaignControllerFindOne(request, requestOptions));
    }

    private async __campaignControllerFindOne(
        request: Vapi.CampaignControllerFindOneRequest,
        requestOptions?: CampaignsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Campaign>> {
        const { id } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `campaign/${core.url.encodePathParam(id)}`,
            ),
            method: "GET",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Campaign, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/campaign/{id}");
    }

    /**
     * @param {Vapi.CampaignControllerRemoveRequest} request
     * @param {CampaignsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.campaigns.campaignControllerRemove({
     *         id: "id"
     *     })
     */
    public campaignControllerRemove(
        request: Vapi.CampaignControllerRemoveRequest,
        requestOptions?: CampaignsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Campaign> {
        return core.HttpResponsePromise.fromPromise(this.__campaignControllerRemove(request, requestOptions));
    }

    private async __campaignControllerRemove(
        request: Vapi.CampaignControllerRemoveRequest,
        requestOptions?: CampaignsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Campaign>> {
        const { id } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `campaign/${core.url.encodePathParam(id)}`,
            ),
            method: "DELETE",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Campaign, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "DELETE", "/campaign/{id}");
    }

    /**
     * @param {Vapi.UpdateCampaignDto} request
     * @param {CampaignsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.campaigns.campaignControllerUpdate({
     *         id: "id"
     *     })
     */
    public campaignControllerUpdate(
        request: Vapi.UpdateCampaignDto,
        requestOptions?: CampaignsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Campaign> {
        return core.HttpResponsePromise.fromPromise(this.__campaignControllerUpdate(request, requestOptions));
    }

    private async __campaignControllerUpdate(
        request: Vapi.UpdateCampaignDto,
        requestOptions?: CampaignsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Campaign>> {
        const { id, ..._body } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `campaign/${core.url.encodePathParam(id)}`,
            ),
            method: "PATCH",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: _body,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Campaign, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "PATCH", "/campaign/{id}");
    }
}
