// 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 * as Vapi from "../../../index.js";

export declare namespace ProviderResourcesClient {
    export type Options = BaseClientOptions;

    export interface RequestOptions extends BaseRequestOptions {}
}

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

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

    /**
     * @param {Vapi.ProviderResourceControllerGetProviderResourcesPaginatedRequest} request
     * @param {ProviderResourcesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.providerResources.providerResourceControllerGetProviderResourcesPaginated({
     *         provider: "cartesia",
     *         resourceName: "pronunciation-dictionary"
     *     })
     */
    public providerResourceControllerGetProviderResourcesPaginated(
        request: Vapi.ProviderResourceControllerGetProviderResourcesPaginatedRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.ProviderResourcePaginatedResponse> {
        return core.HttpResponsePromise.fromPromise(
            this.__providerResourceControllerGetProviderResourcesPaginated(request, requestOptions),
        );
    }

    private async __providerResourceControllerGetProviderResourcesPaginated(
        request: Vapi.ProviderResourceControllerGetProviderResourcesPaginatedRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.ProviderResourcePaginatedResponse>> {
        const {
            provider,
            resourceName,
            id,
            resourceId,
            page,
            sortOrder,
            limit,
            createdAtGt,
            createdAtLt,
            createdAtGe,
            createdAtLe,
            updatedAtGt,
            updatedAtLt,
            updatedAtGe,
            updatedAtLe,
        } = request;
        const _queryParams: Record<string, unknown> = {
            id,
            resourceId,
            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,
                `provider/${core.url.encodePathParam(provider)}/${core.url.encodePathParam(resourceName)}`,
            ),
            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.ProviderResourcePaginatedResponse,
                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",
            "/provider/{provider}/{resourceName}",
        );
    }

    /**
     * @param {Vapi.ProviderResourceControllerCreateProviderResourceRequest} request
     * @param {ProviderResourcesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.providerResources.providerResourceControllerCreateProviderResource({
     *         provider: "cartesia",
     *         resourceName: "pronunciation-dictionary"
     *     })
     */
    public providerResourceControllerCreateProviderResource(
        request: Vapi.ProviderResourceControllerCreateProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.ProviderResource> {
        return core.HttpResponsePromise.fromPromise(
            this.__providerResourceControllerCreateProviderResource(request, requestOptions),
        );
    }

    private async __providerResourceControllerCreateProviderResource(
        request: Vapi.ProviderResourceControllerCreateProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.ProviderResource>> {
        const { provider, resourceName } = 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,
                `provider/${core.url.encodePathParam(provider)}/${core.url.encodePathParam(resourceName)}`,
            ),
            method: "POST",
            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.ProviderResource, 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",
            "/provider/{provider}/{resourceName}",
        );
    }

    /**
     * @param {Vapi.ProviderResourceControllerGetProviderResourceRequest} request
     * @param {ProviderResourcesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Vapi.NotFoundError}
     *
     * @example
     *     await client.providerResources.providerResourceControllerGetProviderResource({
     *         provider: "cartesia",
     *         resourceName: "pronunciation-dictionary",
     *         id: "id"
     *     })
     */
    public providerResourceControllerGetProviderResource(
        request: Vapi.ProviderResourceControllerGetProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.ProviderResource> {
        return core.HttpResponsePromise.fromPromise(
            this.__providerResourceControllerGetProviderResource(request, requestOptions),
        );
    }

    private async __providerResourceControllerGetProviderResource(
        request: Vapi.ProviderResourceControllerGetProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.ProviderResource>> {
        const { provider, resourceName, 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,
                `provider/${core.url.encodePathParam(provider)}/${core.url.encodePathParam(resourceName)}/${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.ProviderResource, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 404:
                    throw new Vapi.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.VapiError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * @param {Vapi.ProviderResourceControllerDeleteProviderResourceRequest} request
     * @param {ProviderResourcesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Vapi.NotFoundError}
     *
     * @example
     *     await client.providerResources.providerResourceControllerDeleteProviderResource({
     *         provider: "cartesia",
     *         resourceName: "pronunciation-dictionary",
     *         id: "id"
     *     })
     */
    public providerResourceControllerDeleteProviderResource(
        request: Vapi.ProviderResourceControllerDeleteProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.ProviderResource> {
        return core.HttpResponsePromise.fromPromise(
            this.__providerResourceControllerDeleteProviderResource(request, requestOptions),
        );
    }

    private async __providerResourceControllerDeleteProviderResource(
        request: Vapi.ProviderResourceControllerDeleteProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.ProviderResource>> {
        const { provider, resourceName, 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,
                `provider/${core.url.encodePathParam(provider)}/${core.url.encodePathParam(resourceName)}/${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.ProviderResource, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 404:
                    throw new Vapi.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.VapiError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * @param {Vapi.ProviderResourceControllerUpdateProviderResourceRequest} request
     * @param {ProviderResourcesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Vapi.NotFoundError}
     *
     * @example
     *     await client.providerResources.providerResourceControllerUpdateProviderResource({
     *         provider: "cartesia",
     *         resourceName: "pronunciation-dictionary",
     *         id: "id"
     *     })
     */
    public providerResourceControllerUpdateProviderResource(
        request: Vapi.ProviderResourceControllerUpdateProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.ProviderResource> {
        return core.HttpResponsePromise.fromPromise(
            this.__providerResourceControllerUpdateProviderResource(request, requestOptions),
        );
    }

    private async __providerResourceControllerUpdateProviderResource(
        request: Vapi.ProviderResourceControllerUpdateProviderResourceRequest,
        requestOptions?: ProviderResourcesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.ProviderResource>> {
        const { provider, resourceName, 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,
                `provider/${core.url.encodePathParam(provider)}/${core.url.encodePathParam(resourceName)}/${core.url.encodePathParam(id)}`,
            ),
            method: "PATCH",
            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.ProviderResource, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 404:
                    throw new Vapi.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.VapiError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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