// 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, mergeOnlyDefinedHeaders } 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 Intercom from "../../../../../index.js";

export declare namespace TicketTypeAttributesClient {
    export interface Options extends BaseClientOptions {}

    export interface RequestOptions extends BaseRequestOptions {}
}

/**
 * Everything about your ticket type attributes
 */
export class TicketTypeAttributesClient {
    protected readonly _options: NormalizedClientOptionsWithAuth<TicketTypeAttributesClient.Options>;

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

    /**
     * You can create a new attribute for a ticket type.
     *
     * @param {Intercom.unstable.CreateTicketTypeAttributeRequest} request
     * @param {TicketTypeAttributesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     *
     * @example
     *     await client.unstable.ticketTypeAttributes.createTicketTypeAttribute({
     *         ticket_type_id: "ticket_type_id",
     *         name: "Attribute Title",
     *         description: "Attribute Description",
     *         data_type: "string",
     *         required_to_create: false
     *     })
     */
    public createTicketTypeAttribute(
        request: Intercom.unstable.CreateTicketTypeAttributeRequest,
        requestOptions?: TicketTypeAttributesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.TicketTypeAttribute | undefined> {
        return core.HttpResponsePromise.fromPromise(this.__createTicketTypeAttribute(request, requestOptions));
    }

    private async __createTicketTypeAttribute(
        request: Intercom.unstable.CreateTicketTypeAttributeRequest,
        requestOptions?: TicketTypeAttributesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.TicketTypeAttribute | undefined>> {
        const { ticket_type_id: ticketTypeId, ..._body } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions?.version }),
            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.IntercomEnvironment.UsProduction,
                `ticket_types/${core.url.encodePathParam(ticketTypeId)}/attributes`,
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: _body,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 20) * 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 Intercom.unstable.TicketTypeAttribute | undefined,
                rawResponse: _response.rawResponse,
            };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.unstable.UnauthorizedError(
                        _response.error.body as Intercom.unstable.Error_,
                        _response.rawResponse,
                    );
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

        return handleNonStatusCodeError(
            _response.error,
            _response.rawResponse,
            "POST",
            "/ticket_types/{ticket_type_id}/attributes",
        );
    }

    /**
     * You can update an existing attribute for a ticket type.
     *
     * @param {Intercom.unstable.UpdateTicketTypeAttributeRequest} request
     * @param {TicketTypeAttributesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     *
     * @example
     *     await client.unstable.ticketTypeAttributes.updateTicketTypeAttribute({
     *         ticket_type_id: "ticket_type_id",
     *         id: "id",
     *         description: "New Attribute Description"
     *     })
     */
    public updateTicketTypeAttribute(
        request: Intercom.unstable.UpdateTicketTypeAttributeRequest,
        requestOptions?: TicketTypeAttributesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.TicketTypeAttribute | undefined> {
        return core.HttpResponsePromise.fromPromise(this.__updateTicketTypeAttribute(request, requestOptions));
    }

    private async __updateTicketTypeAttribute(
        request: Intercom.unstable.UpdateTicketTypeAttributeRequest,
        requestOptions?: TicketTypeAttributesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.TicketTypeAttribute | undefined>> {
        const { ticket_type_id: ticketTypeId, id, ..._body } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions?.version }),
            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.IntercomEnvironment.UsProduction,
                `ticket_types/${core.url.encodePathParam(ticketTypeId)}/attributes/${core.url.encodePathParam(id)}`,
            ),
            method: "PUT",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: _body,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 20) * 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 Intercom.unstable.TicketTypeAttribute | undefined,
                rawResponse: _response.rawResponse,
            };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.unstable.UnauthorizedError(
                        _response.error.body as Intercom.unstable.Error_,
                        _response.rawResponse,
                    );
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

        return handleNonStatusCodeError(
            _response.error,
            _response.rawResponse,
            "PUT",
            "/ticket_types/{ticket_type_id}/attributes/{id}",
        );
    }
}
