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

    export interface RequestOptions extends BaseRequestOptions {}
}

/**
 * With the AI Content APIs, you can create and manage External Pages and Content Import Sources for your Fin Content Library.
 *
 * &nbsp;
 *
 * *External Pages* are pages that you want Fin to be able to answer questions about. The API for External Pages is a great way to ingest into your Fin Content Library pages that are not publicly accessible and hence can't be crawled by Intercom.
 *
 * &nbsp;
 *
 * *Content Import Sources* are the sources of those pages, and they are used to determine the default audience for the pages (configured via the UI). You should create a Content Import Source for each source of External Pages that you want to ingest into your Fin Content Library.
 *
 * &nbsp;
 *
 * You can then iterate through the content from that source via its API and POST it to the External Pages endpoint. That endpoint has an *external_id* parameter which allows you to specify the identifier from the source. The endpoint will then either create a new External Page or update an existing one as appropriate.",
 */
export class AiContentClient {
    protected readonly _options: NormalizedClientOptionsWithAuth<AiContentClient.Options>;

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

    /**
     * You can retrieve a list of all content import sources for a workspace.
     *
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.listContentImportSources()
     */
    public listContentImportSources(
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ContentImportSourcesList> {
        return core.HttpResponsePromise.fromPromise(this.__listContentImportSources(requestOptions));
    }

    private async __listContentImportSources(
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ContentImportSourcesList>> {
        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,
                "ai/content_import_sources",
            ),
            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.ContentImportSourcesList, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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", "/ai/content_import_sources");
    }

    /**
     * You can create a new content import source by sending a POST request to this endpoint.
     *
     * @param {Intercom.CreateContentImportSourceRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.createContentImportSource({
     *         url: "https://www.example.com"
     *     })
     */
    public createContentImportSource(
        request: Intercom.CreateContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ContentImportSource> {
        return core.HttpResponsePromise.fromPromise(this.__createContentImportSource(request, requestOptions));
    }

    private async __createContentImportSource(
        request: Intercom.CreateContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ContentImportSource>> {
        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,
                "ai/content_import_sources",
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: { ...request, sync_behavior: "api" },
            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.ContentImportSource, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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", "/ai/content_import_sources");
    }

    /**
     * @param {Intercom.GetContentImportSourceRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.getContentImportSource({
     *         source_id: "source_id"
     *     })
     */
    public getContentImportSource(
        request: Intercom.GetContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ContentImportSource> {
        return core.HttpResponsePromise.fromPromise(this.__getContentImportSource(request, requestOptions));
    }

    private async __getContentImportSource(
        request: Intercom.GetContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ContentImportSource>> {
        const { source_id: sourceId } = 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,
                `ai/content_import_sources/${core.url.encodePathParam(sourceId)}`,
            ),
            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.ContentImportSource, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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",
            "/ai/content_import_sources/{source_id}",
        );
    }

    /**
     * You can update an existing content import source.
     *
     * @param {Intercom.UpdateContentImportSourceRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.updateContentImportSource({
     *         source_id: "source_id",
     *         sync_behavior: "api",
     *         url: "https://www.example.com"
     *     })
     */
    public updateContentImportSource(
        request: Intercom.UpdateContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ContentImportSource> {
        return core.HttpResponsePromise.fromPromise(this.__updateContentImportSource(request, requestOptions));
    }

    private async __updateContentImportSource(
        request: Intercom.UpdateContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ContentImportSource>> {
        const { source_id: sourceId, ..._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,
                `ai/content_import_sources/${core.url.encodePathParam(sourceId)}`,
            ),
            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.ContentImportSource, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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",
            "/ai/content_import_sources/{source_id}",
        );
    }

    /**
     * You can delete a content import source by making a DELETE request this endpoint. This will also delete all external pages that were imported from this source.
     *
     * @param {Intercom.DeleteContentImportSourceRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.deleteContentImportSource({
     *         source_id: "source_id"
     *     })
     */
    public deleteContentImportSource(
        request: Intercom.DeleteContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<void> {
        return core.HttpResponsePromise.fromPromise(this.__deleteContentImportSource(request, requestOptions));
    }

    private async __deleteContentImportSource(
        request: Intercom.DeleteContentImportSourceRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<void>> {
        const { source_id: sourceId } = 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,
                `ai/content_import_sources/${core.url.encodePathParam(sourceId)}`,
            ),
            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: undefined, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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,
            "DELETE",
            "/ai/content_import_sources/{source_id}",
        );
    }

    /**
     * You can retrieve a list of all external pages for a workspace.
     *
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.listExternalPages()
     */
    public listExternalPages(
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ExternalPagesList> {
        return core.HttpResponsePromise.fromPromise(this.__listExternalPages(requestOptions));
    }

    private async __listExternalPages(
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ExternalPagesList>> {
        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,
                "ai/external_pages",
            ),
            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.ExternalPagesList, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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", "/ai/external_pages");
    }

    /**
     * You can create a new external page by sending a POST request to this endpoint. If an external page already exists with the specified source_id and external_id, it will be updated instead.
     *
     * @param {Intercom.CreateExternalPageRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.createExternalPage({
     *         title: "Test",
     *         html: "<html><body><h1>Test</h1></body></html>",
     *         url: "https://www.example.com",
     *         source_id: 44,
     *         external_id: "abc1234"
     *     })
     */
    public createExternalPage(
        request: Intercom.CreateExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ExternalPage> {
        return core.HttpResponsePromise.fromPromise(this.__createExternalPage(request, requestOptions));
    }

    private async __createExternalPage(
        request: Intercom.CreateExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ExternalPage>> {
        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,
                "ai/external_pages",
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: { ...request, locale: "en" },
            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.ExternalPage, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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", "/ai/external_pages");
    }

    /**
     * You can retrieve an external page.
     *
     * @param {Intercom.GetExternalPageRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.getExternalPage({
     *         page_id: "page_id"
     *     })
     */
    public getExternalPage(
        request: Intercom.GetExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ExternalPage> {
        return core.HttpResponsePromise.fromPromise(this.__getExternalPage(request, requestOptions));
    }

    private async __getExternalPage(
        request: Intercom.GetExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ExternalPage>> {
        const { page_id: pageId } = 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,
                `ai/external_pages/${core.url.encodePathParam(pageId)}`,
            ),
            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.ExternalPage, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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", "/ai/external_pages/{page_id}");
    }

    /**
     * You can update an existing external page (if it was created via the API).
     *
     * @param {Intercom.UpdateExternalPageRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.updateExternalPage({
     *         page_id: "page_id",
     *         title: "Test",
     *         html: "<html><body><h1>Test</h1></body></html>",
     *         url: "https://www.example.com",
     *         source_id: 47,
     *         external_id: "5678"
     *     })
     */
    public updateExternalPage(
        request: Intercom.UpdateExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ExternalPage> {
        return core.HttpResponsePromise.fromPromise(this.__updateExternalPage(request, requestOptions));
    }

    private async __updateExternalPage(
        request: Intercom.UpdateExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ExternalPage>> {
        const { page_id: pageId, ..._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,
                `ai/external_pages/${core.url.encodePathParam(pageId)}`,
            ),
            method: "PUT",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: { ..._body, locale: "en" },
            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.ExternalPage, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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", "/ai/external_pages/{page_id}");
    }

    /**
     * Sending a DELETE request for an external page will remove it from the content library UI and from being used for AI answers.
     *
     * @param {Intercom.DeleteExternalPageRequest} request
     * @param {AiContentClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @throws {@link Intercom.UnauthorizedError}
     *
     * @example
     *     await client.aiContent.deleteExternalPage({
     *         page_id: "page_id"
     *     })
     */
    public deleteExternalPage(
        request: Intercom.DeleteExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): core.HttpResponsePromise<Intercom.ExternalPage> {
        return core.HttpResponsePromise.fromPromise(this.__deleteExternalPage(request, requestOptions));
    }

    private async __deleteExternalPage(
        request: Intercom.DeleteExternalPageRequest,
        requestOptions?: AiContentClient.RequestOptions,
    ): Promise<core.WithRawResponse<Intercom.ExternalPage>> {
        const { page_id: pageId } = 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,
                `ai/external_pages/${core.url.encodePathParam(pageId)}`,
            ),
            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.ExternalPage, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new Intercom.UnauthorizedError(
                        _response.error.body as Intercom.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,
            "DELETE",
            "/ai/external_pages/{page_id}",
        );
    }
}
