// 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 StructuredOutputsClient {
    export type Options = BaseClientOptions;

    export interface RequestOptions extends BaseRequestOptions {}
}

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

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

    /**
     * @param {Vapi.StructuredOutputControllerFindAllRequest} request
     * @param {StructuredOutputsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.structuredOutputs.structuredOutputControllerFindAll()
     */
    public structuredOutputControllerFindAll(
        request: Vapi.StructuredOutputControllerFindAllRequest = {},
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.StructuredOutputPaginatedResponse> {
        return core.HttpResponsePromise.fromPromise(this.__structuredOutputControllerFindAll(request, requestOptions));
    }

    private async __structuredOutputControllerFindAll(
        request: Vapi.StructuredOutputControllerFindAllRequest = {},
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.StructuredOutputPaginatedResponse>> {
        const {
            id,
            name,
            page,
            sortOrder,
            limit,
            createdAtGt,
            createdAtLt,
            createdAtGe,
            createdAtLe,
            updatedAtGt,
            updatedAtLt,
            updatedAtGe,
            updatedAtLe,
        } = request;
        const _queryParams: Record<string, unknown> = {
            id,
            name,
            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,
                "structured-output",
            ),
            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.StructuredOutputPaginatedResponse,
                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", "/structured-output");
    }

    /**
     * @param {Vapi.CreateStructuredOutputDto} request
     * @param {StructuredOutputsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.structuredOutputs.structuredOutputControllerCreate({
     *         name: "name",
     *         schema: {
     *             type: "string"
     *         }
     *     })
     */
    public structuredOutputControllerCreate(
        request: Vapi.CreateStructuredOutputDto,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.StructuredOutput> {
        return core.HttpResponsePromise.fromPromise(this.__structuredOutputControllerCreate(request, requestOptions));
    }

    private async __structuredOutputControllerCreate(
        request: Vapi.CreateStructuredOutputDto,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.StructuredOutput>> {
        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,
                "structured-output",
            ),
            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.StructuredOutput, 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", "/structured-output");
    }

    /**
     * @param {Vapi.StructuredOutputControllerFindOneRequest} request
     * @param {StructuredOutputsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.structuredOutputs.structuredOutputControllerFindOne({
     *         id: "id"
     *     })
     */
    public structuredOutputControllerFindOne(
        request: Vapi.StructuredOutputControllerFindOneRequest,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.StructuredOutput> {
        return core.HttpResponsePromise.fromPromise(this.__structuredOutputControllerFindOne(request, requestOptions));
    }

    private async __structuredOutputControllerFindOne(
        request: Vapi.StructuredOutputControllerFindOneRequest,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.StructuredOutput>> {
        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,
                `structured-output/${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.StructuredOutput, 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", "/structured-output/{id}");
    }

    /**
     * @param {Vapi.StructuredOutputControllerRemoveRequest} request
     * @param {StructuredOutputsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.structuredOutputs.structuredOutputControllerRemove({
     *         id: "id"
     *     })
     */
    public structuredOutputControllerRemove(
        request: Vapi.StructuredOutputControllerRemoveRequest,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.StructuredOutput> {
        return core.HttpResponsePromise.fromPromise(this.__structuredOutputControllerRemove(request, requestOptions));
    }

    private async __structuredOutputControllerRemove(
        request: Vapi.StructuredOutputControllerRemoveRequest,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.StructuredOutput>> {
        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,
                `structured-output/${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.StructuredOutput, 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", "/structured-output/{id}");
    }

    /**
     * @param {Vapi.UpdateStructuredOutputDto} request
     * @param {StructuredOutputsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.structuredOutputs.structuredOutputControllerUpdate({
     *         id: "id",
     *         schemaOverride: "schemaOverride"
     *     })
     */
    public structuredOutputControllerUpdate(
        request: Vapi.UpdateStructuredOutputDto,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.StructuredOutput> {
        return core.HttpResponsePromise.fromPromise(this.__structuredOutputControllerUpdate(request, requestOptions));
    }

    private async __structuredOutputControllerUpdate(
        request: Vapi.UpdateStructuredOutputDto,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.StructuredOutput>> {
        const { id, schemaOverride, ..._body } = request;
        const _queryParams: Record<string, unknown> = {
            schemaOverride,
        };
        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,
                `structured-output/${core.url.encodePathParam(id)}`,
            ),
            method: "PATCH",
            headers: _headers,
            contentType: "application/json",
            queryParameters: { ..._queryParams, ...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.StructuredOutput, 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", "/structured-output/{id}");
    }

    /**
     * @param {Vapi.StructuredOutputRunDto} request
     * @param {StructuredOutputsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.structuredOutputs.structuredOutputControllerRun({
     *         callIds: ["callIds"]
     *     })
     */
    public structuredOutputControllerRun(
        request: Vapi.StructuredOutputRunDto,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.StructuredOutput> {
        return core.HttpResponsePromise.fromPromise(this.__structuredOutputControllerRun(request, requestOptions));
    }

    private async __structuredOutputControllerRun(
        request: Vapi.StructuredOutputRunDto,
        requestOptions?: StructuredOutputsClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.StructuredOutput>> {
        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,
                "structured-output/run",
            ),
            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.StructuredOutput, 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", "/structured-output/run");
    }
}
