// 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 CompaniesClient {
    export interface Options extends BaseClientOptions {}

    export interface RequestOptions extends BaseRequestOptions {}
}

/**
 * Everything about your Companies
 */
export class CompaniesClient {
    protected readonly _options: NormalizedClientOptionsWithAuth<CompaniesClient.Options>;

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

    /**
     * You can fetch a single company by passing in `company_id` or `name`.
     *
     *   `https://api.intercom.io/companies?name={name}`
     *
     *   `https://api.intercom.io/companies?company_id={company_id}`
     *
     * You can fetch all companies and filter by `segment_id` or `tag_id` as a query parameter.
     *
     *   `https://api.intercom.io/companies?tag_id={tag_id}`
     *
     *   `https://api.intercom.io/companies?segment_id={segment_id}`
     *
     * @param {Intercom.unstable.RetrieveCompanyRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.retrieveCompany({
     *         name: "my company",
     *         company_id: "12345",
     *         tag_id: "678910",
     *         segment_id: "98765",
     *         page: 1,
     *         per_page: 1
     *     })
     */
    public retrieveCompany(
        request: Intercom.unstable.RetrieveCompanyRequest = {},
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.CompanyList> {
        return core.HttpResponsePromise.fromPromise(this.__retrieveCompany(request, requestOptions));
    }

    private async __retrieveCompany(
        request: Intercom.unstable.RetrieveCompanyRequest = {},
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.CompanyList>> {
        const { name, company_id: companyId, tag_id: tagId, segment_id: segmentId, page, per_page: perPage } = request;
        const _queryParams: Record<string, string | string[] | object | object[] | null> = {};
        if (name != null) {
            _queryParams.name = name;
        }

        if (companyId != null) {
            _queryParams.company_id = companyId;
        }

        if (tagId != null) {
            _queryParams.tag_id = tagId;
        }

        if (segmentId != null) {
            _queryParams.segment_id = segmentId;
        }

        if (page != null) {
            _queryParams.page = page.toString();
        }

        if (perPage != null) {
            _queryParams.per_page = perPage.toString();
        }

        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,
                "companies",
            ),
            method: "GET",
            headers: _headers,
            queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
            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.CompanyList, 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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * You can create or update a company.
     *
     * Companies will be only visible in Intercom when there is at least one associated user.
     *
     * Companies are looked up via `company_id` in a `POST` request, if not found via `company_id`, the new company will be created, if found, that company will be updated.
     *
     * {% admonition type="warning" name="Using `company_id`" %}
     *   You can set a unique `company_id` value when creating a company. However, it is not possible to update `company_id`. Be sure to set a unique value once upon creation of the company.
     * {% /admonition %}
     *
     * @param {unknown} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.BadRequestError}
     * @throws {@link Intercom.unstable.UnauthorizedError}
     *
     * @example
     *     await client.unstable.companies.createOrUpdateCompany({
     *         "key": "value"
     *     })
     */
    public createOrUpdateCompany(
        request?: unknown,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.Company> {
        return core.HttpResponsePromise.fromPromise(this.__createOrUpdateCompany(request, requestOptions));
    }

    private async __createOrUpdateCompany(
        request?: unknown,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.Company>> {
        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,
                "companies",
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: request,
            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.Company, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 400:
                    throw new Intercom.unstable.BadRequestError(_response.error.body as unknown, _response.rawResponse);
                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", "/companies");
    }

    /**
     * You can fetch a single company.
     *
     * @param {Intercom.unstable.RetrieveACompanyByIdRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.retrieveACompanyById({
     *         id: "5f4d3c1c-7b1b-4d7d-a97e-6095715c6632"
     *     })
     */
    public retrieveACompanyById(
        request: Intercom.unstable.RetrieveACompanyByIdRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.Company> {
        return core.HttpResponsePromise.fromPromise(this.__retrieveACompanyById(request, requestOptions));
    }

    private async __retrieveACompanyById(
        request: Intercom.unstable.RetrieveACompanyByIdRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.Company>> {
        const { id } = 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,
                `companies/${core.url.encodePathParam(id)}`,
            ),
            method: "GET",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            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.Company, 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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * You can update a single company using the Intercom provisioned `id`.
     *
     * {% admonition type="warning" name="Using `company_id`" %}
     *   When updating a company it is not possible to update `company_id`. This can only be set once upon creation of the company.
     * {% /admonition %}
     *
     * @param {Intercom.unstable.UpdateCompanyRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.updateCompany({
     *         id: "5f4d3c1c-7b1b-4d7d-a97e-6095715c6632"
     *     })
     */
    public updateCompany(
        request: Intercom.unstable.UpdateCompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.Company> {
        return core.HttpResponsePromise.fromPromise(this.__updateCompany(request, requestOptions));
    }

    private async __updateCompany(
        request: Intercom.unstable.UpdateCompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.Company>> {
        const { id } = 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,
                `companies/${core.url.encodePathParam(id)}`,
            ),
            method: "PUT",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            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.Company, 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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _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", "/companies/{id}");
    }

    /**
     * You can delete a single company.
     *
     * @param {Intercom.unstable.DeleteCompanyRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.deleteCompany({
     *         id: "5f4d3c1c-7b1b-4d7d-a97e-6095715c6632"
     *     })
     */
    public deleteCompany(
        request: Intercom.unstable.DeleteCompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.DeletedCompanyObject> {
        return core.HttpResponsePromise.fromPromise(this.__deleteCompany(request, requestOptions));
    }

    private async __deleteCompany(
        request: Intercom.unstable.DeleteCompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.DeletedCompanyObject>> {
        const { id } = 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,
                `companies/${core.url.encodePathParam(id)}`,
            ),
            method: "DELETE",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            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.DeletedCompanyObject,
                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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * You can fetch a list of all contacts that belong to a company.
     *
     * @param {Intercom.unstable.ListAttachedContactsRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.listAttachedContacts({
     *         id: "5f4d3c1c-7b1b-4d7d-a97e-6095715c6632"
     *     })
     */
    public listAttachedContacts(
        request: Intercom.unstable.ListAttachedContactsRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.CompanyAttachedContacts> {
        return core.HttpResponsePromise.fromPromise(this.__listAttachedContacts(request, requestOptions));
    }

    private async __listAttachedContacts(
        request: Intercom.unstable.ListAttachedContactsRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.CompanyAttachedContacts>> {
        const { id } = 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,
                `companies/${core.url.encodePathParam(id)}/contacts`,
            ),
            method: "GET",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            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.CompanyAttachedContacts,
                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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * You can fetch a list of all segments that belong to a company.
     *
     * @param {Intercom.unstable.ListAttachedSegmentsForCompaniesRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.listAttachedSegmentsForCompanies({
     *         id: "5f4d3c1c-7b1b-4d7d-a97e-6095715c6632"
     *     })
     */
    public listAttachedSegmentsForCompanies(
        request: Intercom.unstable.ListAttachedSegmentsForCompaniesRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.CompanyAttachedSegments> {
        return core.HttpResponsePromise.fromPromise(this.__listAttachedSegmentsForCompanies(request, requestOptions));
    }

    private async __listAttachedSegmentsForCompanies(
        request: Intercom.unstable.ListAttachedSegmentsForCompaniesRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.CompanyAttachedSegments>> {
        const { id } = 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,
                `companies/${core.url.encodePathParam(id)}/segments`,
            ),
            method: "GET",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            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.CompanyAttachedSegments,
                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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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

    /**
     * You can list companies. The company list is sorted by the `last_request_at` field and by default is ordered descending, most recently requested first.
     *
     * Note that the API does not include companies who have no associated users in list responses.
     *
     * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the [Scroll API](https://developers.intercom.com/reference#iterating-over-all-companies).
     * {% admonition type="warning" name="Pagination" %}
     *   You can use pagination to limit the number of results returned. The default is `20` results per page.
     *   See the [pagination section](https://developers.intercom.com/docs/build-an-integration/learn-more/rest-apis/pagination/#pagination-for-list-apis) for more details on how to use the `starting_after` param.
     * {% /admonition %}
     *
     * @param {Intercom.unstable.ListAllCompaniesRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     *
     * @example
     *     await client.unstable.companies.listAllCompanies({
     *         page: 1,
     *         per_page: 1,
     *         order: "desc"
     *     })
     */
    public listAllCompanies(
        request: Intercom.unstable.ListAllCompaniesRequest = {},
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.CompanyList> {
        return core.HttpResponsePromise.fromPromise(this.__listAllCompanies(request, requestOptions));
    }

    private async __listAllCompanies(
        request: Intercom.unstable.ListAllCompaniesRequest = {},
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.CompanyList>> {
        const { page, per_page: perPage, order } = request;
        const _queryParams: Record<string, string | string[] | object | object[] | null> = {};
        if (page != null) {
            _queryParams.page = page.toString();
        }

        if (perPage != null) {
            _queryParams.per_page = perPage.toString();
        }

        if (order != null) {
            _queryParams.order = order;
        }

        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,
                "companies/list",
            ),
            method: "POST",
            headers: _headers,
            queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
            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.CompanyList, 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", "/companies/list");
    }

    /**
     *       The `list all companies` functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
     *
     * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
     * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
     * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
     *
     * {% admonition type="info" name="Scroll Parameter" %}
     *   You can get the first page of companies by simply sending a GET request to the scroll endpoint.
     *   For subsequent requests you will need to use the scroll parameter from the response.
     * {% /admonition %}
     * {% admonition type="danger" name="Scroll network timeouts" %}
     *   Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
     *   "Request failed due to an internal network error. Please restart the scroll operation."
     *   If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
     * {% /admonition %}
     *
     * @param {Intercom.unstable.ScrollOverAllCompaniesRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     *
     * @example
     *     await client.unstable.companies.scrollOverAllCompanies({
     *         scroll_param: "scroll_param"
     *     })
     */
    public scrollOverAllCompanies(
        request: Intercom.unstable.ScrollOverAllCompaniesRequest = {},
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.CompanyScroll | undefined> {
        return core.HttpResponsePromise.fromPromise(this.__scrollOverAllCompanies(request, requestOptions));
    }

    private async __scrollOverAllCompanies(
        request: Intercom.unstable.ScrollOverAllCompaniesRequest = {},
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.CompanyScroll | undefined>> {
        const { scroll_param: scrollParam } = request;
        const _queryParams: Record<string, string | string[] | object | object[] | null> = {};
        if (scrollParam != null) {
            _queryParams.scroll_param = scrollParam;
        }

        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,
                "companies/scroll",
            ),
            method: "GET",
            headers: _headers,
            queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
            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.CompanyScroll | 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, "GET", "/companies/scroll");
    }

    /**
     * You can attach a company to a single contact.
     *
     * @param {Intercom.unstable.AttachContactToACompanyRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.BadRequestError}
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.attachContactToACompany({
     *         id: "id",
     *         company_id: "6762f09a1bb69f9f2193bb34"
     *     })
     *
     * @example
     *     await client.unstable.companies.attachContactToACompany({
     *         id: "id",
     *         company_id: "58a430d35458202d41b1e65b"
     *     })
     *
     * @example
     *     await client.unstable.companies.attachContactToACompany({
     *         id: "id",
     *         company_id: "123"
     *     })
     */
    public attachContactToACompany(
        request: Intercom.unstable.AttachContactToACompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.Company> {
        return core.HttpResponsePromise.fromPromise(this.__attachContactToACompany(request, requestOptions));
    }

    private async __attachContactToACompany(
        request: Intercom.unstable.AttachContactToACompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.Company>> {
        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,
            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,
                `contacts/${core.url.encodePathParam(id)}/companies`,
            ),
            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.Company, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 400:
                    throw new Intercom.unstable.BadRequestError(_response.error.body as unknown, _response.rawResponse);
                case 401:
                    throw new Intercom.unstable.UnauthorizedError(
                        _response.error.body as Intercom.unstable.Error_,
                        _response.rawResponse,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _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", "/contacts/{id}/companies");
    }

    /**
     * You can detach a company from a single contact.
     *
     * @param {Intercom.unstable.DetachContactFromACompanyRequest} request
     * @param {CompaniesClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.unstable.UnauthorizedError}
     * @throws {@link Intercom.unstable.NotFoundError}
     *
     * @example
     *     await client.unstable.companies.detachContactFromACompany({
     *         contact_id: "58a430d35458202d41b1e65b",
     *         id: "58a430d35458202d41b1e65b"
     *     })
     */
    public detachContactFromACompany(
        request: Intercom.unstable.DetachContactFromACompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.unstable.Company> {
        return core.HttpResponsePromise.fromPromise(this.__detachContactFromACompany(request, requestOptions));
    }

    private async __detachContactFromACompany(
        request: Intercom.unstable.DetachContactFromACompanyRequest,
        requestOptions?: CompaniesClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.unstable.Company>> {
        const { contact_id: contactId, id } = 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,
                `contacts/${core.url.encodePathParam(contactId)}/companies/${core.url.encodePathParam(id)}`,
            ),
            method: "DELETE",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            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.Company, 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,
                    );
                case 404:
                    throw new Intercom.unstable.NotFoundError(_response.error.body as unknown, _response.rawResponse);
                default:
                    throw new errors.IntercomError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
            }
        }

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